user831098
user831098

Reputation: 1823

Charts 3.0 - Can not show decimal in BarChart - iOS Swift

I had store my value in core data and display it at BarChart. My category amount is in 8.5 and 420.10.

But the display at Bar Chart is 8 and 420. How should I set the value in decimal?

enter image description here

var dataEntries: [BarChartDataEntry] = []

   for i in 0..<dataPoints.count {
     let value = values[i] as Double
     print("value=\(value)")
     let dataEntry = BarChartDataEntry(x: Double(i), y:value)
     dataEntries.append(dataEntry)
 }

let chartDataSet = BarChartDataSet(values: dataEntries, label: "Total Amount")
chartDataSet.colors = [.red, .yellow, .green, .blue, .brown]
chartDataSet.colors = ChartColorTemplates.joyful()
barChartView.xAxis.labelPosition = .bottom
barChartView.chartDescription?.text = ""
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.leftAxis.drawLabelsEnabled = false
barChartView.leftAxis.granularityEnabled = true

let chartData = BarChartData()
chartData.addDataSet(chartDataSet)
barChartView.data = chartData

Upvotes: 2

Views: 1680

Answers (2)

user831098
user831098

Reputation: 1823

I get the answer:

let format = NumberFormatter()
format.numberStyle = .decimal
let formatter = DefaultValueFormatter(formatter: format)
chartData.setValueFormatter(formatter)

Upvotes: 7

Tj3n
Tj3n

Reputation: 9943

Try using NSNumberFormatter:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
barChartView.leftAxis.valueFormatter = numberFormatter

Try see which is the bar value class and apply it there, probably will have

Upvotes: 0

Related Questions