1

I want to convert these doubles to integers on this graph below:

enter image description here

I have followed the advice of other posts and created a YAxisValueFormatter() that implements IAxisValueFormatter but this doesn't affect the values on the bars, only the axis.

This is my chart code:

func setChart(dataPoints: [String], values: [Double]){

    let formato:BarChartFormatter = BarChartFormatter()
    formato.setValues(values: dataPoints)
    let xaxis:XAxis = XAxis()

    let xAxis : XAxis = self.barChartView.xAxis;
    barChartView.noDataText = "you need to provide some data for the chart."

    var dataEntries: [BarChartDataEntry] = Array()


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

    xaxis.valueFormatter = formato

    barChartView.xAxis.valueFormatter = xaxis.valueFormatter

     xAxis.labelFont = UIFont(name: "Avenir", size: 14.0)!
     xAxis.labelTextColor = UIColor.white


    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Games Played")
    let chartData = BarChartData(dataSets: [chartDataSet])
    chartDataSet.colors = ChartColorTemplates.material()

    barChartView.xAxis.labelPosition = .bottom
    barChartView.xAxis.drawGridLinesEnabled = false
    barChartView.xAxis.valueFormatter = xaxis.valueFormatter
    barChartView.chartDescription?.enabled = false
    barChartView.legend.enabled = true
    barChartView.rightAxis.enabled = false
    barChartView.data = chartData

    barChartView.leftAxis.enabled = true
    barChartView.legend.enabled = false
    barChartView.chartDescription?.text = ""

    chartData.addDataSet(chartDataSet)
    barChartView.data = chartData

    chartData.setDrawValues(true)
    chartDataSet.drawValuesEnabled = true
    barChartView.barData?.setValueFont(UIFont(name: "Avenir", size: 12.0))
    barChartView.barData?.setValueTextColor(UIColor.white)

    barChartView.xAxis.granularity = 1.0

    barChartView.leftAxis.drawGridLinesEnabled = false
    barChartView.rightAxis.drawGridLinesEnabled = false
    barChartView.xAxis.drawGridLinesEnabled = false
    barChartView.leftAxis.labelFont = UIFont(name: "Avenir", size: 12.0)!
    barChartView.leftAxis.labelTextColor = UIColor.white
    barChartView.leftAxis.axisMinimum = 0
    barChartView.leftAxis.valueFormatter = YAxisValueFormatter()

    self.barChartView.xAxis.labelPosition = XAxis.LabelPosition.bottom

}

And this is my YAxisValueFormatter class:

class YAxisValueFormatter: NSObject, IAxisValueFormatter {

let numFormatter: NumberFormatter

override init() {
    numFormatter = NumberFormatter()
    numFormatter.minimumFractionDigits = 0
    numFormatter.maximumFractionDigits = 0

    // if number is less than 1 add 0 before decimal
    numFormatter.minimumIntegerDigits = 0 // how many digits do want before decimal
    numFormatter.paddingPosition = .beforePrefix
    numFormatter.paddingCharacter = "0"
}

/// Called when a value from an axis is formatted before being drawn.
///
/// For performance reasons, avoid excessive calculations and memory allocations inside this method.
///
/// - returns: The customized label that is drawn on the axis.
/// - parameter value:           the value that is currently being drawn
/// - parameter axis:            the axis that the value belongs to
///

public func stringForValue(_ value: Double, axis: AxisBase?) -> String     {
    return numFormatter.string(from: NSNumber(floatLiteral: value))!
}
}
RDowns
  • 651
  • 1
  • 10
  • 35

1 Answers1

2

Give the chartDataSet its own valueFormatter:

This Q&A shows how to create the valueFormatter you need. Then set the valueFormatter of charDataSet to:

chartDataSet.valueFormatter = DigitValueFormatter()
Community
  • 1
  • 1
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • If i do get i get the error- `Cannot assign value of type 'YAxisValueFormatter' to type 'IValueFormatter?' – RDowns Feb 20 '17 at 13:53
  • The key is to configure and set the `chartDataSet` `valueFormatter`. I was guessing that it could use the same formatter as the axis, but that apparently isn't true. What happens if you say `chartDataSet.valueFormatter = YAxisValueFormatter().numFormatter` ? – vacawama Feb 20 '17 at 14:03
  • same error unfortunately – RDowns Feb 20 '17 at 14:09
  • The same error, or is it saying something like `Cannot assign value of type 'NumberFormatter' to type 'IValueFormatter'`? – vacawama Feb 20 '17 at 14:11
  • ah yes , my bad, sorry the error reads `Cannot assign value of type 'NumberFormatter' to type 'IValueFormatter' – RDowns Feb 20 '17 at 14:15
  • OK, well we know it wants an `IValueFormatter`, so create one. `let formatter = IValueFormatter()`. Then try using autocompletion to see what properties `IValueFormatter` has. Type `formatter.` and you should get a list of options. Perhaps you can set max and min fractional digits on it, and then assign `chartDataSet.valueFormatter = formatter`. – vacawama Feb 20 '17 at 14:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136161/discussion-between-rdowns-and-vacawama). – RDowns Feb 20 '17 at 14:23
  • That link fixed my problem. Thanks very much – RDowns Feb 20 '17 at 14:38
  • You're welcome. Please upvote the link answer as well. – vacawama Feb 20 '17 at 14:41