4

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?

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Morpheus78
  • 870
  • 1
  • 9
  • 21

2 Answers2

10

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
Pravalika
  • 145
  • 1
  • 1
  • 15
Morpheus78
  • 870
  • 1
  • 9
  • 21
  • Thanks for posting this & answering your own question, it really helped me out and could do with a bit more documentation in the examples! For info, my graph is displaying temperatures and so I only wan the Y axis to show 10ºC, 20ºC and 30ºC. – Litome Oct 22 '15 at 10:07
  • Anychance you solved your 2nd question as well: ""The second question is, how to setup the x-axis in order to show the labels 1,5,10,15,....40? I'm displaying time on the x Axis and would like a label every 2 hours e.g.: 2am, 4am, 6am, 8am, 10am, noon, 2pm, 4pm, 6pm, 8pm, 10pm, midnight... – Litome Oct 22 '15 at 11:04
  • The x values are string. So you can format any string you like and add them to the x values array. – Morpheus78 Nov 15 '15 at 20:39
0

Swift 4.2 and above:

startAtZeroEnabled - This property is deprecated - Use axisMinimum instead.

 open var axisMinValue: Double
     {
     get { return axisMinimum }
     set { axisMinimum = newValue }
 }
lineChartView.leftAxis.axisMinimum = 0
lineChartView.leftAxis.axisMaximum = 10.0
Pravalika
  • 145
  • 1
  • 1
  • 15