隐藏折线图限制线。 Swift 2
Hide Line Chart Limit Lines. Swift 2
我正在使用图表框架 (danielgindi/Charts)。
我有一个显示 1,2 和 3 的线段。根据选择,折线图应在折线图中显示该数量的线。
我有一个 switch 语句,它取决于显示该行数的段中提供的数字。例如。在案例 1 中,显示了 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
}
我找到了解决问题的方法。当段选择不同的数字时,我回忆起导致 limitLines 重复的 setChart 函数。
为了解决这个问题,我在调用 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()
我正在使用图表框架 (danielgindi/Charts)。
我有一个显示 1,2 和 3 的线段。根据选择,折线图应在折线图中显示该数量的线。 我有一个 switch 语句,它取决于显示该行数的段中提供的数字。例如。在案例 1 中,显示了 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
}
我找到了解决问题的方法。当段选择不同的数字时,我回忆起导致 limitLines 重复的 setChart 函数。
为了解决这个问题,我在调用 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()