Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BarChart doesn't refresh after new dataSet #1590

Closed
ajimenezjulio opened this issue Sep 30, 2016 · 5 comments
Closed

BarChart doesn't refresh after new dataSet #1590

ajimenezjulio opened this issue Sep 30, 2016 · 5 comments

Comments

@ajimenezjulio
Copy link

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.

@liuxuan30
Copy link
Member

your code seems fine. However, we need to know where it says "fatal error: index out of range"

@pmairoldi
Copy link
Collaborator

You should be calling notifyDataChange after all data has been set so after barChartView.data = chartData

@liuxuan30
Copy link
Member

liuxuan30 commented Oct 13, 2016

@petester42 it's called in data's setter, so don't have to call it again. unless the data is nil:

    /// The data for the chart
    open var data: ChartData?
    {
        get
        {
            return _data
        }
        set
        {
            _data = newValue
            _offsetsCalculated = false

            if _data == nil
            {
                return
            }

            // calculate how many digits are needed
            setupDefaultFormatter(min: _data!.getYMin(), max: _data!.getYMax())

            for set in _data!.dataSets
            {
                if set.needsFormatter || set.valueFormatter === _defaultValueFormatter
                {
                    set.valueFormatter = _defaultValueFormatter
                }
            }

            // let the chart know there is new data
            notifyDataSetChanged()
        }
    }

@pmairoldi
Copy link
Collaborator

Hmm true. I always just call reload. Makes everything easier.

@danielgindi
Copy link
Collaborator

@ajimenezjulio there's no point in calling notifyDataChanged before setting data. It should happen after, and setting data calls it anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants