iOS Charts ValueFormatter
Almost there, just need to add the following class:
class ChartValueFormatter: NSObject, IValueFormatter {
fileprivate var numberFormatter: NumberFormatter?
convenience init(numberFormatter: NumberFormatter) {
self.init()
self.numberFormatter = numberFormatter
}
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
guard let numberFormatter = numberFormatter
else {
return ""
}
return numberFormatter.string(for: value)!
}
}
Now use this as the number formatter:
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.locale = Locale.current
let valuesNumberFormatter = ChartValueFormatter(numberFormatter: numberFormatter)
lineChartDataSet.valueFormatter = valuesNumberFormatter
lineChartDataSet.valueFont = lineChartDataSet.valueFont.withSize(chartFontPointSize)
lineChartDataSet.valueFormatter = DefaultValueFormatter(decimals: 2)
This worked for me:
Swift 5
let valFormatter = NumberFormatter()
valFormatter.numberStyle = .currency
valFormatter.maximumFractionDigits = 2
valFormatter.currencySymbol = "$"
lineChartPrice.leftAxis.valueFormatter = DefaultAxisValueFormatter(formatter: valFormatter)