合并图表。 Swift 2.2

时间:2016-04-01 14:46:55

标签: swift charts bar-chart linechart ios-charts

我正在尝试在条形图上创建一个lineChart。到目前为止,我有以下代码:

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

我在终端上收到以下错误:

无法转换类型&#39; Charts.CombinedChartData&#39;的值(0x3d5d70)到&#39; Charts.BarChartData&#39; (0x3cfc54)。

以及:

enter image description here

我不知道自己做错了什么,因为我还没有触及过BarChartView&#39;出现错误的地方。

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

当我最初创建BarChart然后尝试添加折线图时,我忘记将视图从BarChartView更改为CombinedChartView!

enter image description here