无法在AndroidPlot中显示x轴上的所有字符串值

时间:2014-05-03 10:56:22

标签: android string androidplot

我无法在AndroidPlot中显示x轴上的所有字符串值

我只能从这12个值中显示10个值:

String [] Labels = {" Ahmed"," Mohammed"," tarek"," alaa"," mai& #34;," sahar","票价",    " adel"," omar"," mahmoud"," tamer"," ali"};

alaa和omar没有出现。

我使用此代码:

         private class GraphXLabelFormat extends Format {
    String[]  Labels = {"Ahmed", "Mohammed", "tarek", "alaa", "mai","sahar", "fares",
        "adel","omar", "mahmoud","tamer","ali"};

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

在onCreate方法中我做了:

       plot.getGraphWidget().setDomainValueFormat(new GraphXLabelFormat());

1 个答案:

答案 0 :(得分:0)

最初在this thread回答,但为方便起见,此处转贴:

可能是由数据集中的点数不等以及标签[]的大小引起的舍入问题。如果你在这样的格式(...)实现中添加一些日志记录,问题应该变得很明显:

@Override
public StringBuffer format(Object object, StringBuffer buffer, FieldPosition field)     
     {
     Log.d(TAG, "raw input: " + object.toString());
     int parsedInt =  Math.round(Float.parseFloat(object.toString()));
     Log.d(TAG, "parsed input: " + parsedInt);

String labelString = Labels[parsedInt];
      buffer.append(labelString);
      return buffer;
}

toString()的这种用法似乎有点奇怪:

int parsedInt =  Math.round(Float.parseFloat(object.toString()));

似乎有点奇怪。我建议将此行更改为:

int parsedInt =  Math.round(((Number) object).floatValue());