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

Cannot get line chart to reload data (checked other posts too) #147

Open
AthanB opened this issue Mar 11, 2020 · 8 comments
Open

Cannot get line chart to reload data (checked other posts too) #147

AthanB opened this issue Mar 11, 2020 · 8 comments

Comments

@AthanB
Copy link

AthanB commented Mar 11, 2020

Hi,

I am having trouble getting the cortasian line chart to update data based on new values inputed into the array.

List<DataEntry> data = new ArrayList<>();

public void GetDataSleep() {
data.add(new ValueDataEntry(snapshot.getString("simpleDate"), snapshot.getLong("finalDifference")));
                    
                    Cartesian line = AnyChart.line();
                    line.removeAllSeries();
                    AnyChartView anyChartView = (AnyChartView) findViewById(R.id.any_chart_view);
                    line.data(data);
                    anyChartView.setChart(line);
}

This code works fine first time but when the "data" array changes the chart does not change even though the data array has changed. I am calling this method each time I change the data. I have tried adding APIlib.getInstance().setActiveAnyChartView(cartesian); but get an error: "setActiveAnyChartView (com.anychart.AnyChartView) in APIlib cannot be applied to (com.anychart.charts.Cartesian)"

I see from previous posts that I shouldn't recreate the chart each time the data is updated but that makes no difference to the problem.

@Shestac92
Copy link

@AthanB
All you need is to apply new data to the chart or series. The code snippet below describes how it works:

        AnyChartView anyChartView = findViewById(R.id.any_chart_view);

        Cartesian cartesian = AnyChart.line();

        List<DataEntry> seriesData = new ArrayList<>();
        seriesData.add(new ValueDataEntry("A", 5));
        seriesData.add(new ValueDataEntry("B", 4));
        seriesData.add(new ValueDataEntry("C", 3));
        seriesData.add(new ValueDataEntry("D", 4));

        final Line series1 = cartesian.line(seriesData);

        anyChartView.setChart(cartesian);


        // simulate real-time update
        final int delayMillis = 500;
        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            public void run() {
                List<DataEntry> data = new ArrayList<>();
                data.add(new ValueDataEntry("A", new Random().nextDouble() * 10));
                data.add(new ValueDataEntry("B", new Random().nextDouble() * 10));
                data.add(new ValueDataEntry("C", new Random().nextDouble() * 10));
                data.add(new ValueDataEntry("D", new Random().nextDouble() * 10));
                // apply new data to the series
                series1.data(data);

                handler.postDelayed(this, delayMillis);
            }
        };
        handler.postDelayed(runnable, delayMillis);

Here is the result:
result

@AthanB
Copy link
Author

AthanB commented Mar 13, 2020

@Shestac92

Thanks for the reply. This implementation does work but is it not possible to have the same data array but with changing data within it being attached to the chart? So instead of having a "seriesData" array and a "data" array, have a single "data" array that has changing data based on user input? This is what I'm having trouble implementing. After that array changes I do series1.data(data); yet the chart remains unchanged. Do I have to assign it a brand new array anytime I want the chart to change?

Thanks

@AthanB
Copy link
Author

AthanB commented Mar 13, 2020

@Shestac92

Can you check this out and understand why the chart is remaining unchanged. Whatever the first radiobutton was checked, the chart remains with that data. Clicking another radiobutton using the other data array remains it unchanged.

public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
                    List<DataEntry> data = new ArrayList<>();
                    List<DataEntry> data2 = new ArrayList<>();
                    List<DataEntry> data3 = new ArrayList<>();

                    data.clear();
                    data2.clear();
                    data3.clear();
                    for (DocumentSnapshot snapshot : documentSnapshots) {

                        data.add(new ValueDataEntry(snapshot.getString("simpleDate"), snapshot.getLong("finalDifference")));
                        data2 = data;
                        data3 = data;
                    }

                    if(radioButton1.isChecked()) {
                        AnyChartView anyChartView = findViewById(R.id.any_chart_view);
                        Cartesian line = AnyChart.line();
                        line.data(data);
                        anyChartView.setChart(line);

                    }
                    if(radioButton2.isChecked()) {
                        AnyChartView anyChartView = findViewById(R.id.any_chart_view);
                        Cartesian line = AnyChart.line();
                        line.data(data2);
                        anyChartView.setChart(line);

                    }
                    if(radioButton3.isChecked()) {
                        AnyChartView anyChartView = findViewById(R.id.any_chart_view);
                        Cartesian line = AnyChart.line();
                        line.data(data3);
                        anyChartView.setChart(line);
                    }

data, data2 and data3 all will change their data depending on user input beforehand. Yet even when using seperate arrays and assigning them seperately to the chart. The chart still remains with the first set of data.

@Shestac92
Copy link

@AthanB
There are several approaches to apply the data to the chart (directly to the chart, to the series, using data set). The approach of updating the chart data depends on how you applied the data the first time.
If you are using the dataSet you can append, remove and update the specific point.
Please, describe your requirements and I will provide recommendations for your use-case.
Probably, I didn't get your idea from the snippet... Do you want to switch data by clicking radio buttons? If so, there's no need to create anyChartView, chart and apply it to the view.

Here is the code to apply new data:

                   if(radioButton1.isChecked()) {
                        line.data(data);
                    }

@gabriel-TheCode
Copy link

I tried and it doesn't work

@Shestac92
Copy link

@gabriel-TheCode
Check the wiki guide on updating chart data.

@gabriel-TheCode
Copy link

The only difference between the wiki guide and my code is the Runnable

@kvaruna
Copy link

kvaruna commented Oct 14, 2023

To whoever is looking to refresh.

The comment @Shestac92 is suggesting is working. Just make sure to use a delay for set.data(barDataList) and declare your set as global.

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