简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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