2011-07-21 55 views
4

我實際上使用AndroidPlot庫在我的android項目中使用簡單的圖表,但我不知道如何更改域區域的值。AndroidPlot:我如何更改域數據?

更具體的,此行:

mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("#")); 

在網站上說,我可以使用另一種格式,其真正的,但如果我使用例如:

SimpleDateFormat("dd-MM-yyyy") 

在圖表中出現「31-12-1969」所有域值。

有人知道我該如何改變這個日期嗎?或使用其他格式(如字符串)?

回答

4

最後回答,但也許會對其他人有用。

我也很難過這個問題,特別是因爲我是Java初學者。 我實現了一個自定義的格式化程序。看起來有點醜陋的解決方案,但它的工作。思想如下:

  • 採取僅Y軸爲值,並留下X軸作爲索引到一個數組(如Androidplot教程表明,使用ArrayFormat.Y_VALS_ONLY使用,// Y_VALS_ONLY意味着使用元素索引爲x值)
  • 每次數據的變化,你需要傳遞給繪圖一個新的格式化您的新數據X軸

這裏有一些代碼段。

首先是轉換數組索引到一個自定義標籤字符串類:

public class MyIndexFormat extends Format { 

    public String[] Labels = null; 

     @Override 
     public StringBuffer format(Object obj, 
       StringBuffer toAppendTo, 
       FieldPosition pos) { 

      // try turning value to index because it comes from indexes 
      // but if is too far from index, ignore it - it is a tick between indexes 
      float fl = ((Number)obj).floatValue(); 
      int index = Math.round(fl); 
      if(Labels == null || Labels.length <= index || 
        Math.abs(fl - index) > 0.1) 
       return new StringBuffer("");  

      return new StringBuffer(Labels[index]); 
     } 

這裏是我如何將其連接到劇情:

MyIndexFormat mif = new MyIndexFormat(); 
mif.Labels = // TODO: fill the array with your custom labels 

// attach index->string formatter to the plot instance 
pricesPlot.getGraphWidget().setDomainValueFormat(mif); 

這招作品也爲動態更新。

注意:Androidplot在繪製水平線時似乎有問題,所以如果您的數據具有相同的Y值,您可能會得到奇怪的結果,我已經在此問題上尋求幫助。

+0

非常感謝馬丁:) – K1988

+0

謝謝偉大的作品 –

+0

@Martin在水平線上的任何問題?對於那些不知道的人來說,如果你畫出一條水平線(比如說一個圖的y值都是0),那麼這個圖不會顯示在圖上,但是如果你有另一個不是水平線的圖,水平線會顯示出來。 – whyoz