Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidPlot : setting the labels on the X-axis

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!

like image 690
gleerman Avatar asked May 26 '12 22:05

gleerman


People also ask

How would you label the X-axis on a plot?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.

Which function is used to set a label for X-axis in a graph?

xlabel( txt ) labels the x-axis of the current axes or standalone visualization. Reissuing the xlabel command replaces the old label with the new label. xlabel( target , txt ) adds the label to the specified target object.

How to put labels on a graph in r?

Use the title( ) function to add labels to a plot. Many other graphical parameters (such as text size, font, rotation, and color) can also be specified in the title( ) function. # labels 25% smaller than the default and green.


2 Answers

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);
    }
}
like image 156
gleerman Avatar answered Oct 18 '22 17:10

gleerman


I used the code written by @Aegonis and make complete code for setting String labels.

I used these 3 array in my case.

Number[] yValues = {1, 3, 2 ,7 ,6};
Number[] xValues = {0, 1, 2, 3, 4};

final String[] xLabels = {"Jan", "Feb", "Mar", "Apr", "May"};

Now the code for the class which extends Format provided by @Aegonis.

class GraphXLabelFormat extends Format {

    @Override
    public StringBuffer format(Object arg0, StringBuffer arg1, FieldPosition arg2) {
        // TODO Auto-generated method stub

        int parsedInt = Math.round(Float.parseFloat(arg0.toString()));
        Log.d("test", parsedInt + " " + arg1 + " " + arg2);
        String labelString = xLabels[parsedInt];
        arg1.append(labelString);
        return arg1;
    }

    @Override
    public Object parseObject(String arg0, ParsePosition arg1) {
        // TODO Auto-generated method stub
        return java.util.Arrays.asList(xLabels).indexOf(arg0);
    }
}

Now the code from onCreate method.

XYPlot plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);

plot.setDomainLabel("TestDomain");
plot.setRangeLabel("TestRange");
plot.setTitle("Height/Weight");

//set domain labels as string [x-axis]
plot.getGraphWidget().setDomainValueFormat(new GraphXLabelFormat());

XYSeries series = new SimpleXYSeries(Arrays.asList(xValues), Arrays.asList(yValues), "Line");

plot.addSeries(series, new LineAndPointFormatter());
like image 3
dakshbhatt21 Avatar answered Oct 18 '22 16:10

dakshbhatt21