ivnsch / SwiftCharts

Easy to use and highly customizable charts library for iOS
Apache License 2.0
2.53k stars 410 forks source link

Bar chart axis cut off #331

Closed martheli closed 6 years ago

martheli commented 6 years ago

I have the following function used to set up a simple bar chart. When it loads, the top of the Y axis values get cut off. Does the view scale with the data?

fullsizeoutput_8

  func setChart(data:[(String,Double)], chartView: UIView) {

            let max = data.max(by: {$0.1 < $1.1 } )

            let chartConfig = BarsChartConfig(
                valsAxisConfig: ChartAxisConfig(from: 0, to: (max?.1)!, by: 10)
            )

            let frame = chartView.frame

            let chart = BarsChart(
                frame: frame,
                chartConfig: chartConfig,
                xTitle: "X axis",
                yTitle: "Y axis",
                bars: data,
                color: UIColor.red,
                barWidth: 20
            )

            self.view.addSubview(chart.view)
            self.chart = chart

        }
teldridge11 commented 6 years ago

@martheli You need to add padding in ChartSettings. Something like:

var chartSettings = ChartSettings()
chartSettings.leading = 10
chartSettings.top = 10
chartSettings.trailing = 10
chartSettings.bottom = 10

let chartConfig = BarsChartConfig(
    chartSettings: chartSettings,
    valsAxisConfig: ChartAxisConfig(from: 0, to: (max?.1)!, by: 10)
)
martheli commented 6 years ago

After making those changes I am getting error now below. I can't set chart config if its of type ChartConfigXY, it is expecting type BarsChartConfig.


  var chartSettings = ChartSettings()
            chartSettings.leading = 10
            chartSettings.top = 10
            chartSettings.trailing = 10
            chartSettings.bottom = 10

let chartConfig = ChartConfigXY(
                chartSettings: chartSettings,
                xAxisConfig: ChartAxisConfig(from: 0, to: (max?.1)!, by: 20),
                yAxisConfig: ChartAxisConfig(from: 0, to: (max?.1)!, by: 20)
            )

            let frame = chartView.frame

            let chart = BarsChart(
                frame: frame,
                chartConfig: chartConfig, // Error here
                xTitle: xTitle,
                yTitle: "# of devices",
                bars: data,
                color: UIColor.red,
                barWidth: 20
            )
martheli commented 6 years ago

nvm I figured it out. Correct code should look like this for me:

 var chartSettings = ChartSettings()
            chartSettings.leading = 10
            chartSettings.top = 10
            chartSettings.trailing = 10
            chartSettings.bottom = 10

            let chartConfig = BarsChartConfig(
                chartSettings: chartSettings,
                valsAxisConfig: ChartAxisConfig(from: 0, to: (max?.1)!, by: 20)
            )

            let frame = chartView.frame

            let chart = BarsChart(
                frame: frame,
                chartConfig: chartConfig,
                xTitle: xTitle,
                yTitle: "# of devices",
                bars: data,
                color: UIColor.red,
                barWidth: 20
            )