2

I have tried to update the data in my barChart, when i passed another dataSet which contains more data points everything goes well, but when i passed another dataSet with a lower data points, the app crashes.

I have also tried putting the barchartView.notifyDataSetChanged() after the line chart.DataSet.color but still crashes, maybe is the wrong place, but i have tried putting the notifyDataSetChanged in many places.

I let you my code, what i do when change the dataSet values is just calling the setChart method and pass the new values array as parameter.

func setChart(values: [Double]) {

    let formato:BarChartFormatter = BarChartFormatter()
    formato.setArray(self.listadoLabels)
    let xaxis:XAxis = XAxis()

    barChartView.noDataText = "You need to provide data for the chart."

    max = values.maxElement()

    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<values.count {

        let dataEntry = BarChartDataEntry(x: Double(i), y:values[i])
        dataEntries.append(dataEntry)

        formato.stringForValue(Double(i), axis: xaxis)
    }

    xaxis.valueFormatter = formato

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units")
    chartDataSet.colors = ChartColorTemplates.joyful()
    barChartView.notifyDataSetChanged()


    barChartView.fitBars = true 
    barChartView.xAxis.valueFormatter = xaxis.valueFormatter
    barChartView.xAxis.labelPosition = .Bottom
    if(values.count > 5){barChartView.zoom(scaleX: 17.0, scaleY: 1.0, x: 0, y: 0)}

    barChartView.xAxis.granularityEnabled = true
    barChartView.xAxis.granularity = 1.0

    barChartView.leftAxis.axisMinimum = 0
    barChartView.leftAxis.axisMaximum = max != nil ? max + (2 * max/10) : 5
    barChartView.rightAxis.enabled = false
    barChartView.drawValueAboveBarEnabled = true
    barChartView.descriptionText = ""
    barChartView.xAxis.drawGridLinesEnabled = false
    barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)

    let chartData = BarChartData()
    chartData.addDataSet(chartDataSet)
    barChartView.data?.notifyDataChanged()
    barChartView.data = chartData

}

The console shows "fatal error: index out of range".

Hope someone can help, thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

0 Answers0