在 ios-charts 条形图图例中有多个系列(标签)
Have more than one series (label) in ios-charts bar chart legend
我按照教程在我的项目中创建了一个图表。我成功地创建了图表,但是,我现在正尝试用两个不同的系列(或图例中的标签)对其进行初始化,以反映两种不同类型的数据(相当于折线图中有两条线)。我尝试用数据集数组初始化图表的数据函数,但它不允许这样做。有人可以提供一些帮助吗?这是我的代码:
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)
}
找到所附图表的图片(上面写着“已售出单位”的地方就是我想要标签的地方)。
发现图表数据有一种特殊方法,它使用数据集(带有 S)而不是数据集,这样您就可以构建多个系列!
谢谢!
嗯,一种方法是为每项支出和收入创建不同的数据集。例如:
let incomeDataSet = BarChartDataSet(values: incomeAmountValues, label: "Budget")
let expensesDataSet = BarChartDataSet(values: expensesValues, label: "Expenses")
let data = BarChartData(dataSets: [incomeDataSet,expensesDataSet])
注意:incomeAmountValues 和 expensesValues 是 BarChartDataEntry 的数组
我按照教程在我的项目中创建了一个图表。我成功地创建了图表,但是,我现在正尝试用两个不同的系列(或图例中的标签)对其进行初始化,以反映两种不同类型的数据(相当于折线图中有两条线)。我尝试用数据集数组初始化图表的数据函数,但它不允许这样做。有人可以提供一些帮助吗?这是我的代码:
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)
}
找到所附图表的图片(上面写着“已售出单位”的地方就是我想要标签的地方)。
发现图表数据有一种特殊方法,它使用数据集(带有 S)而不是数据集,这样您就可以构建多个系列!
谢谢!
嗯,一种方法是为每项支出和收入创建不同的数据集。例如:
let incomeDataSet = BarChartDataSet(values: incomeAmountValues, label: "Budget")
let expensesDataSet = BarChartDataSet(values: expensesValues, label: "Expenses")
let data = BarChartData(dataSets: [incomeDataSet,expensesDataSet])
注意:incomeAmountValues 和 expensesValues 是 BarChartDataEntry 的数组