Search code examples
swiftchartsbar-chartlinechartios-charts

Combined Charts. Swift 2.2


I am trying to create a lineChart ontop of a Bar chart. I have the following code so far:

class AnalysisViewController: UIViewController, ChartViewDelegate {
func setChart(xValues: [String], valuesBarChart: [Double], valuesLineChart: [Double]) {
    barChartView.descriptionText = ""
    barChartView.noDataText = "You need to provide data for the chart."

    var yValsBarChart: [BarChartDataEntry] = []
    var yValsLineChart : [ChartDataEntry] = [ChartDataEntry]()

    for i in 0..<xValues.count {

        yValsBarChart.append(BarChartDataEntry(value: valuesBarChart[i], xIndex: i))
        yValsLineChart.append(ChartDataEntry(value: valuesLineChart[i] - 1, xIndex: i))
    }

    let lineChartDataSet = LineChartDataSet(yVals: yValsLineChart, label: nil)
    let barChartDataSet = BarChartDataSet(yVals: yValsBarChart, label: nil)

    let data: CombinedChartData = CombinedChartData(xVals: xValues)

    data.barData = BarChartData(xVals: xValues, dataSets: [barChartDataSet])
    data.lineData = LineChartData(xVals: xValues, dataSets: [lineChartDataSet])

    barChartView.data = data

    barChartView.leftAxis.customAxisMin = 0

    UniversalStatic.data.generateColoursForGraph(budgetName.count)
    barChartDataSet.colors = UniversalStatic.data.colourArrayForGraph


    setDesignChart()
}
}

I am getting the following error on the terminal:

Could not cast value of type 'Charts.CombinedChartData' (0x3d5d70) to 'Charts.BarChartData' (0x3cfc54).

as well as:

enter image description here

I don't really know what i have done wrong as I haven't touched the 'BarChartView' where the error is appearing.

Any help would be appreciated :)


Solution

  • As i initially created the BarChart first and then tried to add the line chart, i forgot to change the view from a BarChartView to a CombinedChartView!

    enter image description here