AnyChart / AnyChart-Android

AnyChart Android Chart is an amazing data visualization library for easily creating interactive charts in Android apps. It runs on API 19+ (Android 4.4) and features dozens of built-in chart types.
2.32k stars 369 forks source link

Updating datasets for multiple charts in single view #253

Closed x0m3g4 closed 2 years ago

x0m3g4 commented 2 years ago

I'm trying to update 3 charts on an on-click event, however, the charts are not refreshing the data until the application is restarted.

Update function running inside a Coroutine (data comes from Firestore) :

// Calculate dataset and return it as a data class
val chartData: ChartData = ChartLogicClass().getChartData()

// Update data on each chart:
 APIlib.getInstance().setActiveAnyChartView(charts.chart1)
 val chart1 = AnyChart.line()
 chart1.data(chartData.chart1data)
 APIlib.getInstance().setActiveAnyChartView(charts.chart2)
 val chart2 = AnyChart.line()
 chart2.data(chartData.chart2data)
 APIlib.getInstance().setActiveAnyChartView(charts.chart3)
 val chart3 = AnyChart.line()
 chart3.data(chartData.chart3data)

Data class:

data class ChartData (
    var chart1data: ArrayList<DataEntry> = ArrayList(),
    var chart2data: ArrayList<DataEntry> = ArrayList(),
    var chart3data: ArrayList<DataEntry> = ArrayList()
)

Is there anything I'm missing here? the initial data populates just fine, but the updates I can't seem to quite get.

x0m3g4 commented 2 years ago

I was able to sort this out by creating the alternative data set along with "chartData": val chartData1: ChartData = ChartLogicClass().getChart1Data() val chartData2: ChartData = ChartLogicClass().getChart2Data()

And later assigned data to the charts based on "chartCondition" (in my case, during onClick event) val chartData = if (chartCondition=="Chart1") { chartData1 } else { chartData2 } APIlib.getInstance().setActiveAnyChartView(charts.chart1) chart1.data(chartData.chart1data) APIlib.getInstance().setActiveAnyChartView(charts.chart2) chart2.data(chartData.chart2data) APIlib.getInstance().setActiveAnyChartView(charts.chart3) chart3.data(chartData.chart3data)