Late answer, but may be useful to other people.
It was also difficult for me with this question, especially since I am starting Java. I implemented my own formatter. It seems a bit ugly solution, but it works. The idea is this:
- take only the Y axis for values ββand leave the X axis as indices in the array (as the Androidplot tutorial suggests, use ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means using the element index as x values)
- every time your data changes, you need to transfer to Plot a new formatter with your new data for the X axis
Here are some code snippets.
First, this is a class that converts the index of an array into a custom String label:
public class MyIndexFormat extends Format { public String[] Labels = null; @Override public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { // try turning value to index because it comes from indexes // but if is too far from index, ignore it - it is a tick between indexes float fl = ((Number)obj).floatValue(); int index = Math.round(fl); if(Labels == null || Labels.length <= index || Math.abs(fl - index) > 0.1) return new StringBuffer(""); return new StringBuffer(Labels[index]); }
And this is how I attach it to the plot:
MyIndexFormat mif = new MyIndexFormat (); mif.Labels = // TODO: fill the array with your custom labels // attach index->string formatter to the plot instance pricesPlot.getGraphWidget().setDomainValueFormat(mif);
This trick also works for dynamic updates.
NOTICE: Androidplot seems to have problems displaying horizontal lines, so if your data has the same Y values, you may get strange results, I already asked for help on this issue.
source share