4

I experience a number of issues with the horizontal bar chart since I updated to the last version of ios-chart (3.0.0).

First thing is that I had labels for the x axis on the charts. With the new version of ios-charts I had to write my own formatter nevertheless only every second label get displayed now. The second problem is that the bar does not reach the value indicated on the y-axis. What I mean by that is that if I give the bar a value of 4 the height of the bar is actually not long enough to reach the 4.0 label on the y-axis. Those 2 issues are illustrated in this picture.

picture of the problems

Here is my code for the value formatter:

@objc(BarChartFormatter)

public class BarChartFormatter: NSObject, IAxisValueFormatter{

var labelArray : [String] = ["text1", "text2", "text3", "text4", "text5", "text6", "text7", "text8", "text9", "text10"]


public func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    return labelArray[Int(value)]
}

}

And here is the code for the bar chart

horizontalBarChartView.noDataText = "You need to provide data for the chart."
    horizontalBarChartView.isUserInteractionEnabled = false
    horizontalBarChartView.drawGridBackgroundEnabled = false
    horizontalBarChartView.drawValueAboveBarEnabled = false
    horizontalBarChartView.legend.enabled = false
    horizontalBarChartView.drawBarShadowEnabled = false
    horizontalBarChartView.chartDescription?.text = ""
    horizontalBarChartView.setExtraOffsets(left: 0, top: 0, right: 0, bottom: 0)

    // Right y axis
    let barRightAxis = horizontalBarChartView.leftAxis
    barRightAxis.drawGridLinesEnabled = false
    barRightAxis.enabled = false

    let formato:BarChartFormatter = BarChartFormatter()
    let xaxis:XAxis = XAxis()

    // Left y axis
    let barLeftAxis = horizontalBarChartView.rightAxis
    let count = 5
    barLeftAxis.setLabelCount(count, force: true)
    barLeftAxis.drawGridLinesEnabled = false
    barLeftAxis.axisLineWidth = 2
    barLeftAxis.axisLineColor = UIColor.white
    barLeftAxis.labelTextColor = UIColor.white
    barLeftAxis.axisMinimum = 0.0
    barLeftAxis.axisMaximum = 4.0
    barLeftAxis.granularityEnabled = true
    barLeftAxis.granularity = 1.0
    barLeftAxis.decimals = 0

    // x axis
    let barXaXis = horizontalBarChartView.xAxis
    barXaXis.drawGridLinesEnabled = false
    barXaXis.axisLineColor = UIColor.white
    barXaXis.axisLineWidth = 2
    barXaXis.labelTextColor = UIColor.white
    barXaXis.labelPosition = .bottomInside
    barXaXis.granularityEnabled = true
    barXaXis.granularity = 1.0
    barXaXis.decimals = 0

    var dataEntries: [BarChartDataEntry] = []
    var colors: [UIColor] = []

    let theScoreVec = [1.0, 2.0, 3.0, 4.0, 3.0, 0.0, 2.0, 0.0, 1.0, 4.0]
    for i in 0..<10 {
        let theScore = theScoreVec[i]
        if (theScore == 0.0){
            colors.append(UIColor.clear)
            let dataEntry = BarChartDataEntry(x: Double(i), y : Double(4.0))
            dataEntries.append(dataEntry)
        } else {
            colors.append(UIColor.red)
            let dataEntry = BarChartDataEntry(x: Double(i), y: Double(theScore))
            dataEntries.append(dataEntry)
        }
    }
    xaxis.valueFormatter = formato
    barXaXis.valueFormatter = xaxis.valueFormatter

    for i in 0..<10{
        formato.stringForValue(Double(i), axis: xaxis)

    }
    xaxis.valueFormatter = formato
    barXaXis.valueFormatter = xaxis.valueFormatter

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Completion")
    chartDataSet.colors = colors
    let chartData = BarChartData(dataSet: chartDataSet)
    chartData.setDrawValues(false)
    horizontalBarChartView.data = chartData

Here I also use some kind of hack because when one enters a value of 0.0 for a BarChartDataEntry the bars and the x axis have a significant offset. Therefore I create bars with a fake value and give them a transparent color.

Do one you had a similar issue with the library and know where it comes from ? Or maybe has even solved it ?

Thanks in advance for the help.

M.R.E.D.
  • 41
  • 3

0 Answers0