Mpandroidchart: XAxis getFormattedValue() called multiple times when animating

Created on 31 Jan 2017  路  2Comments  路  Source: PhilJay/MPAndroidChart

I set a formatter for the XAxis via getXAxis().setValueFormatter().
I then animate the chart, whether it's on the X or the Y axis. The getFormattedValue() method is called a lot of times with the same values, which is useless and inefficient, especially if the formatting function is complex, which leads to great lag.

Most helpful comment

I had to implement a simple cache system with a Map that checks if the string has already been computed for the value ( see below).
Still, I think it's a bug that the value is trying to be formatted multiple times when it in fact doesn't change when animating on the animating on the Y axis.

private Map<Float, String> formattedDateCache;

[...]

chart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String formattedValue;

                if (!formattedDateCache.containsKey(value)) {
                    formattedValue = DateUtils.getChartDateString((long) value);
                    formattedDateCache.put(value, formattedValue);
                } else {
                    formattedValue = formattedDateCache.get(value);
                }

                return formattedValue;
            }
        });

All 2 comments

I had to implement a simple cache system with a Map that checks if the string has already been computed for the value ( see below).
Still, I think it's a bug that the value is trying to be formatted multiple times when it in fact doesn't change when animating on the animating on the Y axis.

private Map<Float, String> formattedDateCache;

[...]

chart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String formattedValue;

                if (!formattedDateCache.containsKey(value)) {
                    formattedValue = DateUtils.getChartDateString((long) value);
                    formattedDateCache.put(value, formattedValue);
                } else {
                    formattedValue = formattedDateCache.get(value);
                }

                return formattedValue;
            }
        });

You are right; In BarChart for example, even if there is only one bar, the formatter is calles a lot of times (8 to 10 times) before the chart is show.

I had to implement a simple cache system with a Map that checks if the string has already been computed for the value ( see below)>

...any way, your code just add more CPU work and time to you app, becouse it repeats as many times the formatter is called

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Giammaofwar picture Giammaofwar  路  3Comments

OnlyInAmerica picture OnlyInAmerica  路  3Comments

SutharRohit picture SutharRohit  路  3Comments

galex picture galex  路  3Comments

andreyfel picture andreyfel  路  3Comments