使用折线图创建多条线
Creating Multiple Lines with Line Chart
我正在使用代码示例和建议在我的折线图中添加两条或更多条线。我很难这样做,因为 chtChart.data
将图表数据添加到图表并导致更新。应该如何添加额外的行?在我下面的代码中,chtChart.data = data1
被替换为 data2
。
let line1 = LineChartDataSet(values: lineChartEntry, label: "CH4")
let line2 = LineChartDataSet(values: avgValue, label: "Warnings")
line1.colors = [NSUIColor.blue]
line2.colors = [NSUIColor.gray]
let data1 = LineChartData()
let data2 = LineChartData()
data1.addDataSet(line1)
data2.addDataSet(line2)
chtChart.data = data1
chtChart.data = data2
要在一张图表上显示多条线,您需要添加多个数据集的 LineChartData
实例。
将您的代码替换为:
let line1 = LineChartDataSet(values: lineChartEntry, label: "CH4")
let line2 = LineChartDataSet(values: avgValue, label: "Warnings")
line1.colors = [NSUIColor.blue]
line2.colors = [NSUIColor.gray]
let data = LineChartData(dataSets: [line1, line2])
chtChart.data = data
我正在使用代码示例和建议在我的折线图中添加两条或更多条线。我很难这样做,因为 chtChart.data
将图表数据添加到图表并导致更新。应该如何添加额外的行?在我下面的代码中,chtChart.data = data1
被替换为 data2
。
let line1 = LineChartDataSet(values: lineChartEntry, label: "CH4")
let line2 = LineChartDataSet(values: avgValue, label: "Warnings")
line1.colors = [NSUIColor.blue]
line2.colors = [NSUIColor.gray]
let data1 = LineChartData()
let data2 = LineChartData()
data1.addDataSet(line1)
data2.addDataSet(line2)
chtChart.data = data1
chtChart.data = data2
要在一张图表上显示多条线,您需要添加多个数据集的 LineChartData
实例。
将您的代码替换为:
let line1 = LineChartDataSet(values: lineChartEntry, label: "CH4")
let line2 = LineChartDataSet(values: avgValue, label: "Warnings")
line1.colors = [NSUIColor.blue]
line2.colors = [NSUIColor.gray]
let data = LineChartData(dataSets: [line1, line2])
chtChart.data = data