隐藏折线图限制线。斯威夫特2

时间:2016-04-07 10:07:11

标签: swift linechart ios-charts

我正在使用图表框架(danielgindi / Charts)。

我有一段显示1,2& 3.根据选择,折线图应显示折线图中的线条数量。 我有一个switch语句,它取决于显示该行数量的段中提供的数字。例如。在case1中,显示1行,表示情况2,显示2行等。&这可以按预期工作。

当我尝试在此switch语句中添加限制行并从较高的情况切换到较低的情况时,例如3到2,第3行不会消失&第1行和第2行似乎有重复。

有没有在switch语句中隐藏限制线?

我有以下代码:

 func setChart(xValues: [String], valuesLineChart: [[Double]], limitLines: [Double]) {
    chartView.descriptionText = ""
    chartView.noDataText = "You need to provide data for the chart."

    print("valuesLineChart has \(valuesLineChart.count) lines")

    var dataSets : [LineChartDataSet] = [LineChartDataSet]()

    switch valuesLineChart.count {

    case 1 : print("1 within switch")

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<xValues.count {
        yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
    }

    let set1 = LineChartDataSet(yVals: yVals1, label: nil)
    dataSets.append(set1)

    //limit lines
    let limitSet1 = ChartLimitLine(limit: limitLines[0], label: "switch1, limit1")
    chartView.rightAxis.addLimitLine(limitSet1)

    case 2 :print("2 within switch")

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<xValues.count {
        yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
    }

    var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<xValues.count {
        yVals2.append(ChartDataEntry(value: valuesLineChart[1][i], xIndex: i))
    }

    let set1 = LineChartDataSet(yVals: yVals1, label: nil)
    let set2 = LineChartDataSet(yVals: yVals2, label: nil)

    dataSets.append(set1)
    dataSets.append(set2)

    //limit lines
    let limitSet1 = ChartLimitLine(limit: limitLines[0], label: "")
    let limitSet2 = ChartLimitLine(limit: limitLines[1], label: "")

    chartView.rightAxis.addLimitLine(limitSet1)
    chartView.rightAxis.addLimitLine(limitSet2)


    case 3 :print("3 within switch")

    var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<xValues.count {
        yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
    }

    var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<xValues.count {
        yVals2.append(ChartDataEntry(value: valuesLineChart[1][i], xIndex: i))
    }

    var yVals3 : [ChartDataEntry] = [ChartDataEntry]()
    for i in 0..<xValues.count {
        yVals3.append(ChartDataEntry(value: valuesLineChart[2][i], xIndex: i))
    }

    let set1 = LineChartDataSet(yVals: yVals1, label: nil)
    let set2 = LineChartDataSet(yVals: yVals2, label: nil)
    let set3 = LineChartDataSet(yVals: yVals3, label: nil)

    dataSets.append(set1)
    dataSets.append(set2)
    dataSets.append(set3)

    //limit lines
    let limitSet1 = ChartLimitLine(limit: limitLines[0], label: "")
    let limitSet2 = ChartLimitLine(limit: limitLines[1], label: "")
    let limitSet3 = ChartLimitLine(limit: limitLines[2], label: "")

    chartView.rightAxis.addLimitLine(limitSet1)
    chartView.rightAxis.addLimitLine(limitSet2)
    chartView.rightAxis.addLimitLine(limitSet3)
    }

    let data: CombinedChartData = CombinedChartData(xVals: xValues)
    data.lineData = LineChartData(xVals: xValues, dataSets: dataSets)
    chartView.data = data
}

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。当段正在选择不同的数字时,我正在调用setChart函数导致limitLines的重复。

为了解决这个问题,我在调用setChart函数之前删除了限制行;

var limitSet1 : ChartLimitLine?
var limitSet2 : ChartLimitLine?
var limitSet3 : ChartLimitLine?
var limitLineArray : [Double]

func removeLimitLines {
    switch (limitLineArray.count) {
            case 1: print("there was 1 limit line, need to remove this from chart view")
            chartView.rightAxis.removeLimitLine(limitSet1!)
            case 2: print("there was 2 limit line, need to remove this from chart view")
            chartView.rightAxis.removeLimitLine(limitSet1!)
            chartView.rightAxis.removeLimitLine(limitSet2!)
            case 3: print("there was 3 limit line, need to remove this from chart view")
            chartView.rightAxis.removeLimitLine(limitSet1!)
            chartView.rightAxis.removeLimitLine(limitSet2!)
            chartView.rightAxis.removeLimitLine(limitSet3!)
    }
}

func setChart(xValues: [String], valuesLineChart: [[Double]], limitLines: [Double]) {
chartView.descriptionText = ""
chartView.noDataText = "You need to provide data for the chart."

print("valuesLineChart has \(valuesLineChart.count) lines")

var dataSets : [LineChartDataSet] = [LineChartDataSet]()

switch valuesLineChart.count {

case 1 : print("1 within switch")

var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
    yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
}

let set1 = LineChartDataSet(yVals: yVals1, label: nil)
dataSets.append(set1)

//limit lines
limitSet1 = ChartLimitLine(limit: limitLines[0], label: "switch1, limit1")
chartView.rightAxis.addLimitLine(limitSet1)

case 2 :print("2 within switch")

var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
    yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
}

var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
    yVals2.append(ChartDataEntry(value: valuesLineChart[1][i], xIndex: i))
}

let set1 = LineChartDataSet(yVals: yVals1, label: nil)
let set2 = LineChartDataSet(yVals: yVals2, label: nil)

dataSets.append(set1)
dataSets.append(set2)

//limit lines
limitSet1 = ChartLimitLine(limit: limitLines[0], label: "")
limitSet2 = ChartLimitLine(limit: limitLines[1], label: "")

chartView.rightAxis.addLimitLine(limitSet1)
chartView.rightAxis.addLimitLine(limitSet2)


case 3 :print("3 within switch")

var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
    yVals1.append(ChartDataEntry(value: valuesLineChart[0][i], xIndex: i))
}

var yVals2 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
    yVals2.append(ChartDataEntry(value: valuesLineChart[1][i], xIndex: i))
}

var yVals3 : [ChartDataEntry] = [ChartDataEntry]()
for i in 0..<xValues.count {
    yVals3.append(ChartDataEntry(value: valuesLineChart[2][i], xIndex: i))
}

let set1 = LineChartDataSet(yVals: yVals1, label: nil)
let set2 = LineChartDataSet(yVals: yVals2, label: nil)
let set3 = LineChartDataSet(yVals: yVals3, label: nil)

dataSets.append(set1)
dataSets.append(set2)
dataSets.append(set3)

//limit lines
limitSet1 = ChartLimitLine(limit: limitLines[0], label: "")
limitSet2 = ChartLimitLine(limit: limitLines[1], label: "")
limitSet3 = ChartLimitLine(limit: limitLines[2], label: "")

chartView.rightAxis.addLimitLine(limitSet1)
chartView.rightAxis.addLimitLine(limitSet2)
chartView.rightAxis.addLimitLine(limitSet3)
}

let data: CombinedChartData = CombinedChartData(xVals: xValues)
data.lineData = LineChartData(xVals: xValues, dataSets: dataSets)
chartView.data = data
}

现在,我在setChart函数之前调用removeLimitLines,它按预期工作。

编辑:

您也可以直接致电:

chartView.leftAxis.removeAllLimitLines()