IOS图表使用Swift X-Axis问题

时间:2017-03-30 09:32:00

标签: ios charts swift3 ios-charts

我最近升级到最新版本的IOS Charts和X-Code(8.3),我遇到了IOS Charts和我的x轴问题。我的x-Axis支持显示日期,但是由于升级我不能让它工作,我只得到0到6.我相信我知道为什么,这是因为我似乎无法设置X-轴值不再。

这里是升级前的原始代码:

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

    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Total Items")
    let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)

    lineChartDataSet.colors = [UIColor(red: 27.0/255.0, green: 114.0/255.0, blue: 216.0/255.0, alpha: 1.0)]
    lineChartView.xAxis.labelPosition = .bottom
    lineChartView.xAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
    lineChartView.legend.enabled = false
    lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInOutQuad)
    lineChartView.chartDescription?.text = ""
    lineChartView.rightAxis.enabled = false
    lineChartView.leftAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
    lineChartDataSet.drawValuesEnabled = false
    lineChartDataSet.drawCirclesEnabled = false
    lineChartDataSet.lineWidth = 2.0
    lineChartView.leftAxis.valueFormatter = NumberFormatter()
    lineChartView.leftAxis.valueFormatter.minimumFractionDigits = 0
    lineChartView.isUserInteractionEnabled = false
    lineChartView.data = lineChartData

}

所以这总是很好,因为升级的东西已经改变,所以我的代码现在是:

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

    var dataEntries: [ChartDataEntry] = []

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

    let lineChartDataSet = LineChartDataSet(values: dataEntries, label: "Total Items")
    let lineChartData = LineChartData(dataSet: lineChartDataSet)

    lineChartDataSet.colors = [UIColor(red: 27.0/255.0, green: 114.0/255.0, blue: 216.0/255.0, alpha: 1.0)]
    lineChartView.xAxis.labelPosition = .bottom
    lineChartView.xAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
    lineChartView.legend.enabled = false
    lineChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInOutQuad)
    lineChartView.chartDescription?.text = ""
    lineChartView.rightAxis.enabled = false
    lineChartView.leftAxis.labelFont = UIFont(name: "AvenirNext-Medium", size: 8.0)!
    lineChartDataSet.drawValuesEnabled = false
    lineChartDataSet.drawCirclesEnabled = false
    lineChartDataSet.lineWidth = 2.0
    lineChartView.leftAxis.valueFormatter = NumberFormatter() as? IAxisValueFormatter
    //lineChartView.leftAxis.valueFormatter.minimumFractionDigits = 0
    lineChartView.isUserInteractionEnabled = false
    lineChartView.data = lineChartData

}

所以在我在lineChartData中设置x轴值之前,但现在我不能这样做,所以我设定的x轴数据没有被设置。有人可以帮助我再次在我的x轴上显示我的日期吗?

由于

戴夫

1 个答案:

答案 0 :(得分:1)

您需要使用stringForValue方法实现IAxisValueFormatter协议,并将其设置为xAxis.valueFormatter的委托

我看到两种方式。

  1. 您只需将IAxisValueFormatter协议添加到ViewController,并在ViewController中实现stringForValue。

    class ViewController: UIViewController, IAxisValueFormatter {
    
        // your code ...
    
        // implement delegate method
        func stringForValue(_ dataPointIndex: Double, axis: AxisBase?) -> String {
    
            return someDataPoints[Int(dataPointIndex)]
        }
    
        func setChart(dataPoints: [String], values: [Double]){
            // your code ...
    
            // Set delegate 
            lineChartView.xAxis.valueFormatter = self
        }
    
        // your code ...
    }
    
  2. 或者您可以为IAxisValueFormatter协议创建specilaized委托类。

    private class DataPointFormatter: NSObject, IAxisValueFormatter {
    
        var dataPoints: [String] = []
    
        func stringForValue(_ value: Double, axis: AxisBase?) -> String {
            return dataPoints[Int(value)]
        }
    
        init(dataPoints: [String]) {
            super.init()
            self.dataPoints = dataPoints
        }
    }
    

    然后在setChart函数中设置委托

    func setChart(dataPoints: [String], values: [Double]){
        // your code ...
    
        lineChartView.xAxis.valueFormatter = DataPointFormatter(dataPoints: dataPoints)
    }