AndroidPlot : setting the labels on the X-axis

偶尔善良 提交于 2019-12-12 10:59:39

问题


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!


回答1:


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);
    }
}



回答2:


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());


来源:https://stackoverflow.com/questions/10770220/androidplot-setting-the-labels-on-the-x-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!