swift: reload view for displaying new data in chart

11,159

From their documentation is seems that you need to notify the change in data sets:

 // EXAMPLE 1
 // add entries to the "data" object
 exampleData.addEntry(...);
 chart.notifyDataSetChanged(); // let the chart know it's data changed
 chart.invalidate(); // refresh

 // EXAMPLE 2
 // add entries to "dataSet" object
 dataSet.addEntry(...);
 exampleData.notifyDataChanged(); // let the data know a dataSet changed
 chart.notifyDataSetChanged(); // let the chart know it's data changed
 chart.invalidate(); // refresh

For more information check out their wiki.


note: from repo README:

Currently there's no need for documentation for the iOS/tvOS/OSX version, as the API is 95% the same as on Android. You can read the official MPAndroidChart documentation here: Wiki

so when you click the link and see "MPAndroidChart" don't be alarmed :)


You can also try:

chart.data.notifyDataChanged()
chart.notifyDataSetChanged()

This is taken from their example project in objective-c:

  LineChartDataSet *set1 = nil;
    if (_chartView.data.dataSetCount > 0)
    {
        set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
        set1.values = values;
        [_chartView.data notifyDataChanged];
        [_chartView notifyDataSetChanged];
    }
Share:
11,159
mostworld77
Author by

mostworld77

Updated on August 16, 2022

Comments

  • mostworld77
    mostworld77 over 1 year

    im using the chart-framework 'Charts', however it has no reload function for the chart with new data. It can only display the chart once with loading the viewController. But i want to reload the view/chart with a selection from a table. (the table is in other viewcontroller)

    in my case, i have a ViewController with a view inside and data are in two arrays: x and y

    func setChartData(x: [Double], y: [Double])
    

    I tried to call the function but it doesnt display it. If the function is called from viewWillAppear() the chart is only showing, if i switch to another viewcontroller and switch back

    how can i solve this

    self.view!.setNeedsDisplay() within the function is not working.

    My viewcontroller:

    override func viewWillAppear(animated: Bool) {
        setChartData(resultID_statistics, y: resultWeight_statistics)
    
    }
    
    func setChartData(x: [Double], y: [Double])  {
    
        // 1 - creating an array of data entries
        var yVals1 : [ChartDataEntry] = [ChartDataEntry]()
        for var i = 0; i < x.count; i++ {
            yVals1.append(ChartDataEntry(value: y[i], xIndex: i))
        }
    
        // 2 - create a data set with our array
        let set1: LineChartDataSet = LineChartDataSet(yVals: yVals1, label: "First Set")
        set1.axisDependency = .Left // Line will correlate with left axis values
        set1.setColor(UIColor.redColor().colorWithAlphaComponent(0.5)) // our line's opacity is 50%
        set1.setCircleColor(UIColor.redColor()) // our circle will be dark red
        set1.lineWidth = 2.0
        set1.circleRadius = 6.0 // the radius of the node circle
        set1.fillAlpha = 65 / 255.0
        set1.fillColor = UIColor.redColor()
        set1.highlightColor = UIColor.whiteColor()
        set1.drawCircleHoleEnabled = true
    
        //3 - create an array to store our LineChartDataSets
        var dataSets : [LineChartDataSet] = [LineChartDataSet]()
        dataSets.append(set1)
    
        //4 - pass our months in for our x-axis label value along with our dataSets
        let data: LineChartData = LineChartData(xVals: x, dataSets: dataSets)
        data.setValueTextColor(UIColor.whiteColor())
    
        //5 - finally set our data
        self.lineChartView.data = data
    
        data.notifyDataChanged()
        lineChartView.notifyDataSetChanged()
    
    }
    
    • mostworld77
      mostworld77 over 7 years
      I tried to add the values into global arrays, and call the function from viewWillAppear override func viewWillAppear(animated: Bool) { setChartData(resultID_statistics, y: resultWeight_statistics) }
    • Ganesh Kumar
      Ganesh Kumar over 7 years
      it doesn't reload when calling from viewwillappear?
    • mostworld77
      mostworld77 over 7 years
      no it doesnt, only if i switch to other VC and switch back
  • mostworld77
    mostworld77 over 7 years
    i cant call the function invalidate(), because not exists? i tried this functions without results: lineChartView.notifyDataSetChanged() lineChartView.setNeedsDisplay()
  • mostworld77
    mostworld77 over 7 years
    thank you for the try, but the chart is not updating with chart.data.notifyDataChanged() chart.notifyDataSetChanged().
  • random
    random over 7 years
    @mostworld77 can you post more of your code implementation
  • random
    random over 7 years
    self.lineChartView.data.notifyDataChanged() along with self.lineChartView.notifyDataChanged() didn't work correct?
  • mostworld77
    mostworld77 over 7 years
    the method: self.lineChartView.notifyDataChanged() dont exists, do you mean self.lineChartView.notifyDataSetChanged() ? also dont work
  • random
    random over 7 years
    @mostworld77 correct. Do you have access to that method for self.lineChartView.data?
  • mostworld77
    mostworld77 over 7 years