English 中文(简体)
AndroidPlot : 在 X 轴上设置标签
原标题:AndroidPlot : setting the labels on the X-axis

AndroidPlot网站倒闭后,我对此问题有点犹豫不决。 已经问了几个类似的问题,但没有一个回答得当,所以我问了一下。

我想知道如何重新标签我的X轴。例如,如果我想为月度数据绘制值,我会为 Januari 绘制 < code> (1, 82) ,为 Februari 绘制 < code> (2, 67) ,等等。之后,我想将X标记从 < code> [1、2, 3,......] 改为 x_lables = ["Januari", "Februari","...] 。我该怎么做?

哦,请给出一个答案, x_ labels 可以是任何东西(如果有特定的每月标签方法,你永远不知道)。

Anyone who could help? Thanks!

最佳回答

我自己想通了:

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);
    }
}
问题回答

我使用由 @Aegonis 撰写的代码, 并为设置 < code> String 标签提供完整的代码 。

我用这三个阵列来对付我

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

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

现在由 @Aegonis 提供的扩展 Format 的类代码 。

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

现在使用 onCreate 方法的代码。

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签