Search code examples
iosswiftxcodeios-charts

Horizontal Bar chart not moving to X


Today i finally got my autolayout constrainst animation to work, and after that i noticed, that the Horizontal bar chart isnt behaving like it used to be.

I have a Horizontal Bar chart populated with data in Descending order (the most should be on top) but the problem is, whenever i try to move the view back to the first entry (the biggest value) using moveViewToX the chart just doesn't do anything it always shows the last added entry, so its scrolled to the end.

chart scrolled to last entry

I'd like to have it start from the top, i mean from the first entry.

the desired chart

I have tried the moveViewToX function, but it still has no effect. Here's the code for setting the data:

let sortedData = chartData.sorted(by: { $0.money < $1.money })

    for i in 0..<sortedData.count {
        let dataEntry = BarChartDataEntry(x: Double(i), y: Double(sortedData[i].money))
        dataEntries.append(dataEntry)
        labels2.append(sortedData[i].Name)
    }

Here is the setup for the chart:

let chartDataSet = BarChartDataSet(entries: dataEntries, label: "kategoriak")
    chartDataSet.colors = [UIColor.red]

    let kerekitöFormatter = KerekitöNumberFormatter(showZeros: false, showMoney: true)
    chartDataSet.valueFormatter = kerekitöFormatter
    chartDataSet.valueFont = chartDataSet.valueFont.withSize(10)
    chartDataSet.valueTextColor = .white

    let horChartData = BarChartData(dataSet: chartDataSet)

    horizontalChartView.xAxis.valueFormatter = BarChartXaxisFormatter(labels: labels2, truncMode: true)

    horizontalChartView.data = horChartData
    horizontalChartView.leftAxis.axisMinimum = 0

    horizontalChartView.leftAxis.valueFormatter = YAxisValueFormatter()

    horizontalChartView.legend.enabled = false
    horizontalChartView.xAxis.labelPosition = .bottom

    horizontalChartView.setVisibleXRangeMaximum(5)
    horizontalChartView.moveViewToX(Double(sortedData.count))
    horizontalChartView.drawValueAboveBarEnabled = false
    horizontalChartView.xAxis.granularityEnabled = true
    //horizontalChartView.setExtraOffsets (left: 0.0, top: 0.0, right:0.0, bottom: 0.0)
    horizontalChartView.xAxis.granularity = 1
    horizontalChartView.doubleTapToZoomEnabled = false


    if #available(iOS 13.0, *) {
        horizontalChartView.backgroundColor = .systemBackground
        horizontalChartView.xAxis.labelTextColor = .label
        horizontalChartView.leftAxis.labelTextColor = .label
        horizontalChartView.gridBackgroundColor = .label

    }

    let rightAxis = horizontalChartView.rightAxis
    rightAxis.drawGridLinesEnabled = false
    rightAxis.enabled = false

I hope someone can help. Looking forward to your answers.


Solution

  • After hours of research and trying i figured it out. For some strange reason if you use moveViewToX it does not respond, but when you use moveViewTo(float xValue, float yValue, AxisDependency axis) it works.

    So, to be short, i added this line of code at the end of setting up the chart.

    horizontalChartView.moveViewTo(xValue: Double(sortedData.count), yValue: sortedData.first!.money, axis: .left)
    

    Hope this helps you in the future!