swift: reload view for displaying new data in chart
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];
}
mostworld77
Updated on August 16, 2022Comments
-
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 over 7 yearsI 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 over 7 yearsit doesn't reload when calling from viewwillappear?
-
mostworld77 over 7 yearsno it doesnt, only if i switch to other VC and switch back
-
-
mostworld77 over 7 yearsi cant call the function invalidate(), because not exists? i tried this functions without results: lineChartView.notifyDataSetChanged() lineChartView.setNeedsDisplay()
-
mostworld77 over 7 yearsthank you for the try, but the chart is not updating with chart.data.notifyDataChanged() chart.notifyDataSetChanged().
-
random over 7 years@mostworld77 can you post more of your code implementation
-
random over 7 years
self.lineChartView.data.notifyDataChanged()
along withself.lineChartView.notifyDataChanged()
didn't work correct? -
mostworld77 over 7 yearsthe method:
self.lineChartView.notifyDataChanged()
dont exists, do you meanself.lineChartView.notifyDataSetChanged()
? also dont work -
random over 7 years@mostworld77 correct. Do you have access to that method for
self.lineChartView.data
? -
mostworld77 over 7 yearsLet us continue this discussion in chat.