我按照教程在我的项目中创建了一个图表。我成功创建了图表,但是,我现在尝试用两个不同的系列(或图例中的标签)初始化它,以反映两种不同类型的数据(相当于在折线图中有两行)。我尝试使用数据集数组初始化图表的数据函数,但它不允许这样做。有人可以提供一些帮助吗?这是我的代码:
labels = ["Income", "Expenses"] // This are now two values on the x-axis, I would like for them to be two different labels.
let totals = [986.25, 871.41]
setChart(labels, values: totals)
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "Please run the calculation to provide data for the charts"
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
barChartView.data = chartData
chartDataSet.colors = ChartColorTemplates.colorful()
barChartView.animate(yAxisDuration: 1.0, easingOption: .EaseInBounce)
}
答案 0 :(得分:0)
发现图表数据有一种特殊的方法,它使用数据集(带有S)代替数据集,这样你可以用多个系列构建!
非常感谢!
答案 1 :(得分:0)
嗯,一种方法是为每种费用和收入创建不同的数据集。例如:
let incomeDataSet = BarChartDataSet(values: incomeAmountValues, label: "Budget")
let expensesDataSet = BarChartDataSet(values: expensesValues, label: "Expenses")
let data = BarChartData(dataSets: [incomeDataSet,expensesDataSet])
注意:incomeAmountValues和expensesValues是BarChartDataEntry的数组