我使用了@Aegonis 编写的代码,并为设置String
标签制作了完整的代码。
我在我的案例中使用了这 3 个数组。
Number[] yValues = {1, 3, 2 ,7 ,6};
Number[] xValues = {0, 1, 2, 3, 4};
final String[] xLabels = {"Jan", "Feb", "Mar", "Apr", "May"};
现在是Format
@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);
}
}
现在来自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());