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

Pie chart is flickering whenever new data is inserted #259

Open zaidnaseer opened 2 years ago

zaidnaseer commented 2 years ago

I want to update the pie chart from the data from a local database whenever I press the button, but whenever I press it the chart is flickering with old and new data. Here's my code.

pie = AnyChart.pie();
AnyChartView anyChartView = binding.anyChartView;
anyChartView.setChart(pie);

binding.getPieChartBtn.setOnClickListener(v -> {
//   amountForCategory array is retrieved from database whenever button is clicked
        final int delayMillis = 500;
        final Handler handler = new Handler();
        final Runnable runnable = new Runnable() {
            public void run() {
                List<DataEntry> data = new ArrayList<>();
                if(amountForCategory[0]!=0) data.add(new ValueDataEntry("Food", amountForCategory[0]));
                if(amountForCategory[1]!=0) data.add(new ValueDataEntry("Transportation", amountForCategory[1]));
                if(amountForCategory[2]!=0) data.add(new ValueDataEntry("Rent", amountForCategory[2]));
                if(amountForCategory[3]!=0) data.add(new ValueDataEntry("Medicine", amountForCategory[3]));
                if(amountForCategory[4]!=0) data.add(new ValueDataEntry("Other", amountForCategory[4]));
                pie.data(data);

                handler.postDelayed(this, delayMillis);
            }
        };
        handler.postDelayed(runnable, delayMillis);
        });
TarushGupta23 commented 11 months ago

thats because whenever you click the button you start that runnable, why dont you just run it once instead of using runable?

pie = AnyChart.pie();
AnyChartView anyChartView = binding.anyChartView;
anyChartView.setChart(pie);

binding.getPieChartBtn.setOnClickListener(v -> {
//   amountForCategory array is retrieved from database whenever button is clicked

                List<DataEntry> data = new ArrayList<>();
                if(amountForCategory[0]!=0) data.add(new ValueDataEntry("Food", amountForCategory[0]));
                if(amountForCategory[1]!=0) data.add(new ValueDataEntry("Transportation", amountForCategory[1]));
                if(amountForCategory[2]!=0) data.add(new ValueDataEntry("Rent", amountForCategory[2]));
                if(amountForCategory[3]!=0) data.add(new ValueDataEntry("Medicine", amountForCategory[3]));
                if(amountForCategory[4]!=0) data.add(new ValueDataEntry("Other", amountForCategory[4]));
                pie.data(data);

        });