Search code examples
iosswiftmpandroidchartios-charts

iOS Charts custom labels: integer values on Bubble Chart


I want to show the values displayed inside the bubbles as whole number Ints. For example: instead of "22.0" I just want "22".

The answer here doesn't work with the new iOS Charts because it requires an IValueFormatter instead of a NumberFormatter:

let numberFormatter = NumberFormatter()
numberFormatter.generatesDecimalNumbers = false
chartData.setValueFormatter(numberFormatter)

Error Message:

Cannot convert value of type 'NumberFormatter' to expected argument type 'IValueFormatter?'

Is there any way to solve this problem?


Solution

  • With the new charts, you still use numberformatter and then just convert at the end. Reference the code below.

    let format = NumberFormatter()
    format.generatesDecimalNumbers = false   
    let formatter = DefaultValueFormatter(formatter: format)
    chartData.leftAxis.valueFormatter = (formatter as? IAxisValueFormatter)
    

    Hope this helps!