4

I'm currently using the iOS Charts library, and I'm trying to scale the width of the bar when using a BarChartView, but I'm having a difficult ensuring the width remains almost the same regardless of how many entries there are in the chart.

According to an answer provided here: Changing width of the bar in bar chart

one of the developers stated that the bar's width will shrink with an increasing number of entries.

I have calculated a zoom level factor that I am satisfied with based on the number of entries using a zoomMultiplier like so:

func createBarChart(dataPoints: [Date], values: [Double])
{
    var dataEntries: [BarChartDataEntry] = []

    for index in 0..<dataPoints.count
    {
        let dataEntry = BarChartDataEntry(x: Double(index),
                                                  y: values[index])

        dataEntries.append(dataEntry)
    }

    barChartView.xAxis.valueFormatter = self

    barChartView.xAxis.granularity = 1

    barChartView.xAxis.setLabelCount(dataPoints.count, force: false)

    barChartView.animate(yAxisDuration: 1.0, easingOption: .easeInOutQuad)

    switch dataPoints.count
    {
        case 1...6:
            zoomXMultiplier = 0.0
            barWidthMultiplier = 0.3
            break

        default:
            zoomXMultiplier = CGFloat(dataPoints.count) * 0.168
            barWidthMultiplier = Double(dataPoints.count) * 0.0014 - Double(zoomXMultiplier) * 0.02
            break
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "")
    let chartData = BarChartData(dataSet: chartDataSet)

    barChartView.data = chartData

    barChartView.zoom(scaleX: zoomXMultiplier, scaleY: 0.0, x: 0, y: 0)

    barChartView.barData?.barWidth = barWidthMultiplier
}

However, I can't seem to get the barWidthMultiplier just right, the width of the bar noticeable scales down/up with decreasing/increasing number of data point entries.

Can someone provide any suggestions to help me resolve this issue if possible?

Thanks!

DevB2F
  • 4,674
  • 4
  • 36
  • 60
Pangu
  • 3,721
  • 11
  • 53
  • 120

0 Answers0