Showing/Hiding 行与折线图
Showing/Hiding lines with LineChart
我有以下代码使用 ChartsRealm 在我的图表上显示线条。我怎样才能 show/hide 图表上的单独线条?
let line1 = LineChartDataSet(values: lineChartEntry1, label: "CH4")
let line2 = LineChartDataSet(values: lineChartEntry2, label: "O2")
let line3 = LineChartDataSet(values: lineChartEntry3, label: "H2S")
line1.colors = [NSUIColor.blue]
line2.colors = [NSUIColor.green]
line3.colors = [NSUIColor.red]
let data = LineChartData()
data.addDataSet(line1)
data.addDataSet(line2)
data.addDataSet(line3)
chtChart.data = (data)
因此,在图表库中没有 hide/show 数据集的方法。
但是我们可以在不刷新页面的情况下按逻辑进行。
请检查以下代码是否相同:
创建 LineChartView 扩展:
extension LineChartView {
func setLineChartData(xValues: [String], labels: [String],dataSets:[Array<Double>],colors:[UIColor]) {
var dataSetsArray: [LineChartDataSet] = []
let dataSetCount = self.lineData?.dataSets.count
if dataSetCount != nil && dataSetCount! > 0 {
self.data = nil
}
for i in 0..<dataSets.count {
let yVals = dataSets[i]
var dataEntries: [ChartDataEntry] = []
for i in 0..<yVals.count {
let dataEntry = ChartDataEntry(x: Double(i), y: yVals[i])
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: labels[i])
chartDataSet.highlightEnabled = true
chartDataSet.drawHorizontalHighlightIndicatorEnabled = false
chartDataSet.highlightColor = .blue
chartDataSet.highlightLineWidth = 1
chartDataSet.lineWidth = 2.0
chartDataSet.mode = .horizontalBezier
chartDataSet.drawCircleHoleEnabled = true
chartDataSet.drawValuesEnabled = true
chartDataSet.axisDependency = .left
chartDataSet.circleHoleColor = colors[i]
chartDataSet.highlightColor = colors[i]
chartDataSet.colors = [colors[i]]
//Add Data Set Into Sets array
dataSetsArray.append(chartDataSet)
}
let chartData = LineChartData(dataSets: dataSetsArray)
self.data = chartData
self.animate(xAxisDuration: 1.0,easingOption: .easeInExpo)
}
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
}
}
通过将一种方法添加到 LineChartView
扩展中并根据您的要求或用户行为调用它。
我从我的分段更改方法中调用了它,如下所示:
@IBAction func segmentValueChanged(_ sender: Any) {
switch self.lineSegment.selectedSegmentIndex {
case 0:
let colors = [UIColor.red,UIColor.green,UIColor.blue]
lineChartView.setLineChartData(xValues: months,
labels: ["Monthly Sales","Quarterly Sales","Yearly Sales"],
dataSets: [unitsSold,unitsSold1,unitsSold2],
colors: colors)
break
case 1:
lineChartView.setLineChartData(xValues: months,
labels: ["Monthly Sales"],
dataSets: [unitsSold],
colors: [UIColor.red])
break
case 2:
lineChartView.setLineChartData(xValues: months,
labels: ["Quarterly Sales"],
dataSets: [unitsSold1],
colors: [UIColor.green])
break
case 3:
lineChartView.setLineChartData(xValues: months,
labels: ["Yearly Sales"],
dataSets: [unitsSold2],
colors: [UIColor.blue])
break
default:
break
}
}
您可以在数据集中传递您想要的不同数据。
检查以下屏幕以获取输出参考:
我的细分是全部 | 每月 | 每季度 | 每年
希望这能帮助您实现您的要求!
我有以下代码使用 ChartsRealm 在我的图表上显示线条。我怎样才能 show/hide 图表上的单独线条?
let line1 = LineChartDataSet(values: lineChartEntry1, label: "CH4")
let line2 = LineChartDataSet(values: lineChartEntry2, label: "O2")
let line3 = LineChartDataSet(values: lineChartEntry3, label: "H2S")
line1.colors = [NSUIColor.blue]
line2.colors = [NSUIColor.green]
line3.colors = [NSUIColor.red]
let data = LineChartData()
data.addDataSet(line1)
data.addDataSet(line2)
data.addDataSet(line3)
chtChart.data = (data)
因此,在图表库中没有 hide/show 数据集的方法。
但是我们可以在不刷新页面的情况下按逻辑进行。
请检查以下代码是否相同:
创建 LineChartView 扩展:
extension LineChartView {
func setLineChartData(xValues: [String], labels: [String],dataSets:[Array<Double>],colors:[UIColor]) {
var dataSetsArray: [LineChartDataSet] = []
let dataSetCount = self.lineData?.dataSets.count
if dataSetCount != nil && dataSetCount! > 0 {
self.data = nil
}
for i in 0..<dataSets.count {
let yVals = dataSets[i]
var dataEntries: [ChartDataEntry] = []
for i in 0..<yVals.count {
let dataEntry = ChartDataEntry(x: Double(i), y: yVals[i])
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: labels[i])
chartDataSet.highlightEnabled = true
chartDataSet.drawHorizontalHighlightIndicatorEnabled = false
chartDataSet.highlightColor = .blue
chartDataSet.highlightLineWidth = 1
chartDataSet.lineWidth = 2.0
chartDataSet.mode = .horizontalBezier
chartDataSet.drawCircleHoleEnabled = true
chartDataSet.drawValuesEnabled = true
chartDataSet.axisDependency = .left
chartDataSet.circleHoleColor = colors[i]
chartDataSet.highlightColor = colors[i]
chartDataSet.colors = [colors[i]]
//Add Data Set Into Sets array
dataSetsArray.append(chartDataSet)
}
let chartData = LineChartData(dataSets: dataSetsArray)
self.data = chartData
self.animate(xAxisDuration: 1.0,easingOption: .easeInExpo)
}
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
}
}
通过将一种方法添加到 LineChartView
扩展中并根据您的要求或用户行为调用它。
我从我的分段更改方法中调用了它,如下所示:
@IBAction func segmentValueChanged(_ sender: Any) {
switch self.lineSegment.selectedSegmentIndex {
case 0:
let colors = [UIColor.red,UIColor.green,UIColor.blue]
lineChartView.setLineChartData(xValues: months,
labels: ["Monthly Sales","Quarterly Sales","Yearly Sales"],
dataSets: [unitsSold,unitsSold1,unitsSold2],
colors: colors)
break
case 1:
lineChartView.setLineChartData(xValues: months,
labels: ["Monthly Sales"],
dataSets: [unitsSold],
colors: [UIColor.red])
break
case 2:
lineChartView.setLineChartData(xValues: months,
labels: ["Quarterly Sales"],
dataSets: [unitsSold1],
colors: [UIColor.green])
break
case 3:
lineChartView.setLineChartData(xValues: months,
labels: ["Yearly Sales"],
dataSets: [unitsSold2],
colors: [UIColor.blue])
break
default:
break
}
}
您可以在数据集中传递您想要的不同数据。
检查以下屏幕以获取输出参考:
我的细分是全部 | 每月 | 每季度 | 每年
希望这能帮助您实现您的要求!