简体   繁体   English

ios图表条形图图例中包含多个系列(标签)

[英]Have more than one series (label) in ios-charts bar chart legend

I followed a tutorial to create a chart in my project. 我按照教程在项目中创建图表。 I was successful in creating the chart, however, I am now trying to initialize it with two different series (or labels in the legend), to reflect two different types of data (The equivalent of having two lines in the line chart). 我已成功创建了图表,但是现在我尝试使用两个不同的系列(或图例中的标签)对其进行初始化,以反映两种不同类型的数据(折线图中具有两条线)。 I tried initializing the chart's data function with an array of Data Sets, but it wouldn't allow it. 我尝试使用一组数据集初始化图表的数据功能,但不允许这样做。 Can somebody provide some assistance? 有人可以提供一些帮助吗? Here is my code: 这是我的代码:

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)
}

Find a picture of the chart attached (Where it says Units Sold is where I'd like the labels to be). 查找所附图表的图片(上面写着“已售出的商品”就是我想要的标签)。 图表

Found out that there is a special method for Chart Data that uses Data Sets (with an S) instead of Data Set and that way you can build with multiple series! 发现有一种特殊的图表数据方法,它使用数据集(带有S)而不是数据集,并且您可以使用多个序列进行构建!

Thanks anyway! 不管怎么说,还是要谢谢你!

Well, One way is to create different datasets for each expense and income. 嗯,一种方法是为每种费用和收入创建不同的数据集。 Eg: 例如:

    let incomeDataSet = BarChartDataSet(values: incomeAmountValues, label: "Budget")
    let expensesDataSet = BarChartDataSet(values: expensesValues, label: "Expenses")
    let data = BarChartData(dataSets: [incomeDataSet,expensesDataSet])

Note: incomeAmountValues and expensesValues are Arrays of BarChartDataEntry 注意:incomeAmountValues和expensesValues是BarChartDataEntry的数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM