Search code examples
swiftios-charts

How to set up iOS charts y-axis with min, max value and a fixed step between the grid lines?


I'm just in the learning phase of using ios-charts. I would like to change the x-axis grid to fixed values. My plotted y-values are just int numbers like 1, 2, 3,..., 10. Nevertheless, the left y-axis shows values like 6.3, 9.1, etc., depending on my zoom level. The second question is, how to set up the x-axis in order to show the labels 1,5,10,15,....40?

Is there any way to influence the step size like e.g. in Excel?

// zoom y-axis to min/max value
lineChart.leftAxis.customAxisMin = max(0.0, lineChart.data!.yMin - 1.0)
lineChart.leftAxis.customAxisMax = min(10.0, lineChart.data!.yMax + 1.0)
lineChart.leftAxis.startAtZeroEnabled = false

Chart (min = 6.0 and max = 10.0): The grid start at 6.3 instead of 6.0.

enter image description here

Chart (min = 7.0 and max = 10.0): The grid starts as expected with 7.0.

enter image description here

What's going wrong here?


Solution

  • I solved the issue just by setting the correct labelCount.

    // zoom y-axis to min/max value
    lineChart.leftAxis.customAxisMin = max(0.0, lineChart.data!.yMin - 1.0)
    lineChart.leftAxis.customAxisMax = min(10.0, lineChart.data!.yMax + 1.0)
    lineChart.leftAxis.labelCount = Int(lineChart.leftAxis.customAxisMax lineChart.leftAxis.customAxisMin)
    lineChart.leftAxis.startAtZeroEnabled = false