Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create empty label by using “Charts”?

Following is using “Charts” for swift 4.0 to show line chart. But the last label will not show normally , and i want to add empty label x-axis to fix this issue.

enter image description here

I try to add " " to chartView.xAxis.valueFormatter, but it doesn't add an x-axis.

following is my code:

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

How to create empty label by using “Charts” for swift 4.0?

My Environment:

Charts version: 3.1.1

Xcode version:10.0

Swift version:4.0

like image 652
Peggy Avatar asked Oct 16 '22 11:10

Peggy


1 Answers

the solution is same with 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

like image 195
Peggy Avatar answered Nov 15 '22 05:11

Peggy