如何使用“图表”创建空标签?

时间:2018-11-07 09:53:30

标签: ios swift charts linechart

以下是使用“图表”进行快速4.0显示折线图。但是最后一个标签将无法正常显示,我想添加空标签的x轴来解决此问题。

enter image description here

我尝试在chartView.xAxis.valueFormatter中添加“”,但没有添加x轴。

以下是我的代码:

x1 = [10.0, 10.0, 11.0, 11.0, 12.0, 12.0, 13.0, 13.0, 14.0, 14.0, 15.0, 15.0, 16.0]
x2 = [20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0, 23.5, 24.0, 24.5, 25.0, 25.5, 26.0]
times = ["8:00"," ","9:00"," ","10:00"," ","11:00"," ","12:00"," ","13:00"," ","14:00"]

setChart()

func setChart(){

    let lineDefault = UIColor(red: 140.0/255.0, green: 234.0/255.0, blue: 255.0/255.0, alpha: 1.0)

    let data = LineChartData()
    var lineChartEntry1 = [ChartDataEntry]()

    for i in 0..<x1.count {
        lineChartEntry1.append(ChartDataEntry(x: Double(i), y: Double(x1[i])))
    }
    let line1 = LineChartDataSet(values: lineChartEntry1, label: "a")
    line1.drawCirclesEnabled = false
    line1.drawValuesEnabled = false
    line1.lineWidth = 2
    line1.setColor(lineDefault)
    data.addDataSet(line1)
    if (x2.count > 0) {
        var lineChartEntry2 = [ChartDataEntry]()
        for i in 0..<x2.count {
            lineChartEntry2.append(ChartDataEntry(x: Double(i), y: Double(x2[i])))
        }
        let line2 = LineChartDataSet(values: lineChartEntry2, label: "b")
        line2.drawCirclesEnabled = false
        line2.drawValuesEnabled = false
        line2.lineWidth = 2
        line2.setColor(UIColor.yellow)
        data.addDataSet(line2)
    }

    let chartFormatter = LineChartFormatter(labels: times)
    let xAxis = XAxis()
    xAxis.valueFormatter = chartFormatter
    mapLineChartView.xAxis.valueFormatter = xAxis.valueFormatter
    mapLineChartView.data = data


}

private class LineChartFormatter: NSObject, IAxisValueFormatter {

    var labels: [String] = []

    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return labels[Int(value)]
    }

    init(labels: [String]) {
        super.init()
        self.labels = labels
    }
}

如何在Swift 4.0中使用“图表”创建空白标签?

我的环境:

  

图表版本:3.1.1

     

Xcode版本:10.0

     

快速版本:4.0

1 个答案:

答案 0 :(得分:3)

解决方案与issue #2608

相同
x1 = [10.0, 10.0, 11.0, 11.0, 12.0, 12.0, 13.0, 13.0, 14.0, 14.0, 15.0, 15.0, 16.0, 16.0]
x2 = [20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0, 23.5, 24.0, 24.5, 25.0, 25.5, 26.0, 26.0]
times = ["8:00"," ","9:00"," ","10:00"," ","11:00"," ","12:00"," ","13:00"," ","14:00"," "]

enter image description here