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.
I had to implement a simple cache system with a Map
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
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.