iOS-Charts Float to Integer YAxis

拈花ヽ惹草 提交于 2019-12-18 05:56:39

问题


I'm building a chart using iOS-charts

I'm trying to convert the floats into int, but iOS-charts only allows for Floats in the data entry:

      let result = ChartDataEntry(value: Float(month), xIndex: i)

Does anyone know the method for making sure only ints are used?


回答1:


You just need to adjust the NSNumberFormatter...

Example:

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

NSNumberFormatter is a very powerful class, you can do much much more with it. :-)




回答2:


For Swift 3.0 and Charts 3.0 you need to enable and set granularity for the axis.

Example:

barChart.leftAxis.granularityEnabled = true
barChart.leftAxis.granularity = 1.0



回答3:


This worked for me. However, the y-axis labels will round when displaying small numbers, so if you are charting, say 1 or 2, the chart will display 0 twice. (See here: Force BarChart Y axis labels to be integers?)

let numberFormatter = NSNumberFormatter()
numberFormatter.generatesDecimalNumbers = false
chartDataSet.valueFormatter = numberFormatter
// Converting to Int for small numbers rounds weird
//barChartView.rightAxis.valueFormatter = numberFormatter
//barChartView.leftAxis.valueFormatter = numberFormatter



回答4:


You can try this one, it worked nicely for me

let formatter = NSNumberFormatter()
formatter.numberStyle = .NoStyle
formatter.locale = NSLocale(localeIdentifier: "es_CL")
BarChartView.leftAxis.valueFormatter = formatter



回答5:


Swift4.2 ; Xcode 10.0 :

let numberFormatter = NumberFormatter()
numberFormatter.generatesDecimalNumbers = false
barChart.leftAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: numberFormatter)


来源:https://stackoverflow.com/questions/31318858/ios-charts-float-to-integer-yaxis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!