Search code examples
iosswiftios-charts

Setting iOS-Charts Float to Integer YAxis


I tried setting yAxis to integer using NSNumberFormatter,

yAxis.valueFormatter = NSNumberFormatter()
yAxis.valueFormatter.minimumFractionDigits = 0

but the problem here is I am getting duplicate yAxis values - 0, 1, 2, 2, 3, 4 and 5. How this can be eliminated?

enter image description here

These are the issues I am facing. I tried setting

    barChart.leftAxis.axisMaximum = 5.0
    barChart.leftAxis.axisMinimum = 0.0
    barChart.leftAxis.customAxisMin = 0
    barChart.leftAxis.customAxisMax = 5.0

but not helping any suggestions?


Solution

  • This cannot be avoided, because you don't allow fraction digit.

    minimumFractionDigits = 0 means no decimal if possible, like 2.1 and 2.0, they will result in 2.1 and 2 ( if maximumFractionDigits = 1)

    You need to give the information what's the raw value and what's the value of maximumFractionDigits

    EDIT:

    the y axis label is calculated by default via internal func computeAxisValues

    it calculate the interval based on your max and min value, and the label count.

    if _yAxis.isForceLabelsEnabled is enabled, then it will read labelCount and just use (max-min)/(labelCount-1) to calculate the interval. It is disabled by default.

    When isForceLabelsEnabled is false, it has a small algorithm to calculate what's the proper way to get a good looking number.

    Take a look at internal func computeAxisValues to find out what meets your need.

    Update for your second part of question:

    1. you should not touch axisMaximum nor min
    2. enable forceLabelsEnabled
    3. change yAxis.labelCount to 6 or 5 or whatever you like
    4. enable xAxis.wordWrapEnabled