如何使用 Daniel Gindis 图表库在同一轴上绘制相同类型的图表(在折线图中)?
How to have charts of the same type (ie LineChart) on the same axis using Daniel Gindi's chart library?
我有一个图表,可以在同一轴上显示多个折线图和散点图。它们都将具有相同的 x 轴。我似乎不能在同一轴内放置多个折线图。
我试过实现 CombinedChartView。我创建了一个函数,该函数接收相应数据集的数组。
func customiseChart (rule1x: [Double], rule1y: [Double], rule2x: [Double], rule2y: [Double], rule3x: [Double], rule3y: [Double], rule4x: [Double], rule4y: [Double], rule5x: [Double], rule5y: [Double], rule6x: [Double], rule6y: [Double], rule7x: [Double], rule7y: [Double], suppressedSparksX: [Double], suppressedSparksY: [Double], exsuppressedSparksX: [Double], exsuppressedSparksY: [Double], CombinedChartView: CombinedChartView, labelRule1: String, labelRule2: String, labelRule3: String, labelRule4: String, labelRule5: String, labelRule6: String, labelRule7: String, labelSuppressed: String, labelExsuppressed: String) {
//1) set ChartDataEntry for all the graphs
var rule1ChartEntries: [ChartDataEntry] = []
for i in 0..<rule1y.count {
let entries = ChartDataEntry(x: rule1x[i], y: rule1y[i])
rule1ChartEntries.append(entries)
}
. . .
var rule7ChartEntries: [ChartDataEntry] = []
for i in 0..<rule7y.count {
let entries = ChartDataEntry(x: rule7x[i], y: rule7y[i])
rule7ChartEntries.append(entries)
}
var suppressedScatterChartEntries: [ChartDataEntry] = []
for i in 0..<suppressedSparksY.count {
let entries = ChartDataEntry(x: suppressedSparksX[i], y: suppressedSparksY[i])
suppressedScatterChartEntries.append(entries)
}
var exsuppressedScatterChartEntries: [ChartDataEntry] = []
for i in 0..<exsuppressedSparksY.count {
let entries = ChartDataEntry(x: exsuppressedSparksX[i], y: exsuppressedSparksY[i])
exsuppressedScatterChartEntries.append(entries)
}
//2) set ChartDataSet for all the charts
let rule1DataSet = LineChartDataSet(entries: rule1ChartEntries, label: labelRule1)
rule1DataSet.colors = [NSUIColor.black]
let rule1data = LineChartData()
rule1data.addDataSet(rule1DataSet)
rule1DataSet.drawCirclesEnabled = false
rule1DataSet.drawValuesEnabled = false
let rule2DataSet = LineChartDataSet(entries: rule2ChartEntries, label: labelRule2)
rule2DataSet.colors = [NSUIColor.darkGray]
let rule2data = LineChartData()
rule2data.addDataSet(rule2DataSet)
rule2DataSet.drawCirclesEnabled = false
rule2DataSet.drawValuesEnabled = false
. . .
let rule7DataSet = LineChartDataSet(entries: rule7ChartEntries, label: labelRule7)
rule7DataSet.colors = [NSUIColor.yellow]
let rule7data = LineChartData()
rule7data.addDataSet(rule7DataSet)
rule7DataSet.drawCirclesEnabled = false
rule7DataSet.drawValuesEnabled = false
let suppressedSparkDataSet = ScatterChartDataSet(entries: suppressedScatterChartEntries, label: labelSuppressed)
suppressedSparkDataSet.colors = [NSUIColor.red]
let suppressedData = ScatterChartData()
suppressedData.addDataSet(suppressedSparkDataSet)
let exsuppressedSparkDataSet = ScatterChartDataSet(entries: exsuppressedScatterChartEntries, label: labelExsuppressed)
exsuppressedSparkDataSet.colors = [NSUIColor.blue]
let exsuppressedData = ScatterChartData()
exsuppressedData.addDataSet(exsuppressedSparkDataSet)
// 3) set combinedData
let combinedData = CombinedChartData()
combinedData.lineData = rule1data
combinedData.lineData = rule2data
combinedData.lineData = rule3data
combinedData.lineData = rule4data
combinedData.lineData = rule5data
combinedData.lineData = rule6data
combinedData.lineData = rule7data
combinedData.scatterData = suppressedData
combinedData.scatterData = exsuppressedData
//4) assign all the data into the combined chart
CombinedChartView.data = combinedData
}
我希望所有图表都显示在图表上。但是,只显示最后声明的 lineChart 和 ScatterChart,即图表上只有 rule7data 和 exsuppressedData。图表的其余部分未显示。
而不是这样做
let rule1data = LineChartData()
rule1data.addDataSet(rule1DataSet)
您可以将多个数据集添加到一个 LineChartData
所以你可以这样做:
let allLineDatas = LineChartData(dataSets: [rule1DataSet, rule2DataSet,....])
combinedData.lineData = allLineDatas
其他人也一样。
combinedData.scatterData = allScatterDatas
你可以下载他们的示例工程和运行模拟器,你会在那里找到各种例子,然后你可以找到你需要集成的例子的代码
基本上,您创建新的变量。您创建所需图表类型的数据集类型的数组。
var allLineChartDataSets: [LineChartDataSet] = [LineChartDataSet]()
var allScatterDataSets: [ScatterChartDataSet] = [ScatterChartDataSet]()
//1) set ChartDataEntry for all the graphs
var rule1ChartEntries: [ChartDataEntry] = []
for i in 0..<rule1y.count {
let entries = ChartDataEntry(x: rule1x[i], y: rule1y[i])
rule1ChartEntries.append(entries)
. . .
然后,您将个人数据集附加到上面声明的 DataSets 数组中,如下所示:
//2) set ChartDataSet for all the charts
let rule1DataSet = LineChartDataSet(entries: rule1ChartEntries, label: labelRule1)
rule1DataSet.colors = [NSUIColor.black]
let rule1data = LineChartData()
rule1data.addDataSet(rule1DataSet)
rule1DataSet.drawCirclesEnabled = false
rule1DataSet.drawValuesEnabled = false
allLineChartDataSets.append(rule1DataSet)
. . .
也为散点图或您选择的任何其他图表执行此操作。
最后,
// 3) set combinedData
let lineChartDataSets = LineChartData(dataSets: allLineChartDataSets)
let scatterChartDataSets = ScatterChartData(dataSets: allScatterDataSets)
let combinedData = CombinedChartData()
combinedData.lineData = lineChartDataSets
combinedData.scatterData = scatterChartDataSets
//4) assign all the data into the combined chart
CombinedChartView.data = combinedData
我有一个图表,可以在同一轴上显示多个折线图和散点图。它们都将具有相同的 x 轴。我似乎不能在同一轴内放置多个折线图。
我试过实现 CombinedChartView。我创建了一个函数,该函数接收相应数据集的数组。
func customiseChart (rule1x: [Double], rule1y: [Double], rule2x: [Double], rule2y: [Double], rule3x: [Double], rule3y: [Double], rule4x: [Double], rule4y: [Double], rule5x: [Double], rule5y: [Double], rule6x: [Double], rule6y: [Double], rule7x: [Double], rule7y: [Double], suppressedSparksX: [Double], suppressedSparksY: [Double], exsuppressedSparksX: [Double], exsuppressedSparksY: [Double], CombinedChartView: CombinedChartView, labelRule1: String, labelRule2: String, labelRule3: String, labelRule4: String, labelRule5: String, labelRule6: String, labelRule7: String, labelSuppressed: String, labelExsuppressed: String) {
//1) set ChartDataEntry for all the graphs
var rule1ChartEntries: [ChartDataEntry] = []
for i in 0..<rule1y.count {
let entries = ChartDataEntry(x: rule1x[i], y: rule1y[i])
rule1ChartEntries.append(entries)
}
. . .
var rule7ChartEntries: [ChartDataEntry] = []
for i in 0..<rule7y.count {
let entries = ChartDataEntry(x: rule7x[i], y: rule7y[i])
rule7ChartEntries.append(entries)
}
var suppressedScatterChartEntries: [ChartDataEntry] = []
for i in 0..<suppressedSparksY.count {
let entries = ChartDataEntry(x: suppressedSparksX[i], y: suppressedSparksY[i])
suppressedScatterChartEntries.append(entries)
}
var exsuppressedScatterChartEntries: [ChartDataEntry] = []
for i in 0..<exsuppressedSparksY.count {
let entries = ChartDataEntry(x: exsuppressedSparksX[i], y: exsuppressedSparksY[i])
exsuppressedScatterChartEntries.append(entries)
}
//2) set ChartDataSet for all the charts
let rule1DataSet = LineChartDataSet(entries: rule1ChartEntries, label: labelRule1)
rule1DataSet.colors = [NSUIColor.black]
let rule1data = LineChartData()
rule1data.addDataSet(rule1DataSet)
rule1DataSet.drawCirclesEnabled = false
rule1DataSet.drawValuesEnabled = false
let rule2DataSet = LineChartDataSet(entries: rule2ChartEntries, label: labelRule2)
rule2DataSet.colors = [NSUIColor.darkGray]
let rule2data = LineChartData()
rule2data.addDataSet(rule2DataSet)
rule2DataSet.drawCirclesEnabled = false
rule2DataSet.drawValuesEnabled = false
. . .
let rule7DataSet = LineChartDataSet(entries: rule7ChartEntries, label: labelRule7)
rule7DataSet.colors = [NSUIColor.yellow]
let rule7data = LineChartData()
rule7data.addDataSet(rule7DataSet)
rule7DataSet.drawCirclesEnabled = false
rule7DataSet.drawValuesEnabled = false
let suppressedSparkDataSet = ScatterChartDataSet(entries: suppressedScatterChartEntries, label: labelSuppressed)
suppressedSparkDataSet.colors = [NSUIColor.red]
let suppressedData = ScatterChartData()
suppressedData.addDataSet(suppressedSparkDataSet)
let exsuppressedSparkDataSet = ScatterChartDataSet(entries: exsuppressedScatterChartEntries, label: labelExsuppressed)
exsuppressedSparkDataSet.colors = [NSUIColor.blue]
let exsuppressedData = ScatterChartData()
exsuppressedData.addDataSet(exsuppressedSparkDataSet)
// 3) set combinedData
let combinedData = CombinedChartData()
combinedData.lineData = rule1data
combinedData.lineData = rule2data
combinedData.lineData = rule3data
combinedData.lineData = rule4data
combinedData.lineData = rule5data
combinedData.lineData = rule6data
combinedData.lineData = rule7data
combinedData.scatterData = suppressedData
combinedData.scatterData = exsuppressedData
//4) assign all the data into the combined chart
CombinedChartView.data = combinedData
}
我希望所有图表都显示在图表上。但是,只显示最后声明的 lineChart 和 ScatterChart,即图表上只有 rule7data 和 exsuppressedData。图表的其余部分未显示。
而不是这样做
let rule1data = LineChartData()
rule1data.addDataSet(rule1DataSet)
您可以将多个数据集添加到一个 LineChartData
所以你可以这样做:
let allLineDatas = LineChartData(dataSets: [rule1DataSet, rule2DataSet,....])
combinedData.lineData = allLineDatas
其他人也一样。
combinedData.scatterData = allScatterDatas
你可以下载他们的示例工程和运行模拟器,你会在那里找到各种例子,然后你可以找到你需要集成的例子的代码
基本上,您创建新的变量。您创建所需图表类型的数据集类型的数组。
var allLineChartDataSets: [LineChartDataSet] = [LineChartDataSet]()
var allScatterDataSets: [ScatterChartDataSet] = [ScatterChartDataSet]()
//1) set ChartDataEntry for all the graphs
var rule1ChartEntries: [ChartDataEntry] = []
for i in 0..<rule1y.count {
let entries = ChartDataEntry(x: rule1x[i], y: rule1y[i])
rule1ChartEntries.append(entries)
. . .
然后,您将个人数据集附加到上面声明的 DataSets 数组中,如下所示:
//2) set ChartDataSet for all the charts
let rule1DataSet = LineChartDataSet(entries: rule1ChartEntries, label: labelRule1)
rule1DataSet.colors = [NSUIColor.black]
let rule1data = LineChartData()
rule1data.addDataSet(rule1DataSet)
rule1DataSet.drawCirclesEnabled = false
rule1DataSet.drawValuesEnabled = false
allLineChartDataSets.append(rule1DataSet)
. . .
也为散点图或您选择的任何其他图表执行此操作。
最后,
// 3) set combinedData
let lineChartDataSets = LineChartData(dataSets: allLineChartDataSets)
let scatterChartDataSets = ScatterChartData(dataSets: allScatterDataSets)
let combinedData = CombinedChartData()
combinedData.lineData = lineChartDataSets
combinedData.scatterData = scatterChartDataSets
//4) assign all the data into the combined chart
CombinedChartView.data = combinedData