2017-02-20 47 views
0

我想这些双打转换为整数下面这个图:如何将这些双打转换为iOS iOS Bar Chart上的整数?

enter image description here

我按照其他职位的建议,并创造了YAxisValueFormatter()实现IAxisValueFormatter但这并不影响上的价值酒吧,只有轴。

这是我的图表代码:

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 

} 

这是我YAxisValueFormatter类:

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))! 
} 
} 

回答

1

给了chartDataSet自己的valueFormatter

This Q&A展示了如何创建valueFormatter你需要。然后设置的charDataSetvalueFormatter到:

chartDataSet.valueFormatter = DigitValueFormatter() 
+0

如果我得到我得到的错误 - '不能指定类型的“YAxisValueFormatter”值键入“IValueFormatter? – RDowns

+0

关键是配置和设置'chartDataSet''valueFormatter'。我猜测它可以使用与轴相同的格式化程序,但显然这不是真的。如果你说'chartDataSet.valueFormatter = YAxisValueFormatter()。numF​​ormatter',会发生什么? – vacawama

+0

不幸的是同样的错误 – RDowns