AndroidPlot : setting the labels on the X-axis

gleerman picture gleerman · May 27, 2012 · Viewed 7.4k times · Source

With the AndroidPlot website being down, I'm kind of stuck on this issue. Several similar questions have been asked, but none of them were properly answered, so here I go.

I would like to know how I can relabel my X-axis. For example, if I want to plot values about monthly data, I would plot it like (1, 82) for Januari, (2,67) for Februari , and so on. Afterwards, I want to change the X-labels from [1, 2, 3, ...] to x_labels = ["Januari", "Februari", ...]. How can I do this?

Oh and please provide an answer for which x_labels can be anything (in case there is some specific method for monthly labels, you never know).

Anyone who could help? Thanks!

Answer

gleerman picture gleerman · May 27, 2012

Figured it out myself:

this.getGraphWidget().setDomainValueFormat(new GraphXLabelFormat());

// ...

private class GraphXLabelFormat extends Format {

    private static LABELS = ["Label 1", "Label 2", "Label 3"];

    @Override
    public StringBuffer format(Object object, StringBuffer buffer, FieldPosition field) {
        int parsedInt = Math.round(Float.parseFloat(object.toString()));
        String labelString = = LABELS[parsedInt];

        buffer.append(labelString);
        return buffer;
    }

    @Override
    public Object parseObject(String string, ParsePosition position) {
        return java.util.Arrays.asList(LABELS).indexOf(string);
    }
}