Search code examples
iosios-charts

Start line chart's y-axis bars from the bottom even if that area is negative (ios charts)


Maybe it's not the best title, but here is the explanation with images:

  • This is what I currently have: enter image description here

  • This is what I want: enter image description here

Sorry for the bad mock, but you get the idea.

All the values that are negative, the "bars" that are starting from 0 downwards, I want them to start from the bottom upwards.

Upon reading the docs I think I have to set the leftAxis.axisMinValue, but it doesn't affect the chart in any way.

// create dataSet (LineChartDataSet)

leftAxis.axisMinValue = dataSet.yMin

// create LineChartData and set to chart's data property

Maybe I'm missing something, it's not the right thing to set, or it's a bug. I did a bit of debugging and found that the axisMinValue is initially 0, and it gets set correctly to the passed value.


Solution

  • Okay, I figured it out:

    You need to set a custom FillFormatter on the dataSet (dataSet.fillFormatter)

    class FillFormatter: ChartFillFormatter {
        func getFillLinePosition(dataSet dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat {
            return CGFloat(dataProvider.getAxis(.Left).axisMinValue)
        }
    }