Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift: reload view for displaying new data in chart

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

}
like image 658
mostworld77 Avatar asked Aug 30 '16 12:08

mostworld77


1 Answers

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];
    }
like image 50
random Avatar answered Sep 21 '22 01:09

random