我使用由 @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());