2016-11-29 102 views
0

所以我使用android-graphview(https://github.com/jjoe64/GraphView)。android-graphview - 動態設置值

我試圖弄清楚如何在程序中逐一將DataPoints添加到系列中,因爲我從源中獲取數據。

目前,醫生說要做:

LineGraphSeries<DataPoint> series2 = new LineGraphSeries<>(new DataPoint[] { 
    new DataPoint(0, 4), 
    new DataPoint(1, 5), 
    new DataPoint(2, 2), 
    new DataPoint(3, 1), 
    new DataPoint(4, 10) 
}); 

但我希望能夠添加/動態創建我的for循環獲取這些數據點。

有什麼建議嗎?

回答

0
public class AddSeriesAtRuntime extends BaseExample { 
    private Activity mActivity; 

    @Override 
    public void onCreate(FullscreenActivity activity) { 
     mActivity = activity; 
     GraphView graph = (GraphView) activity.findViewById(R.id.graph); 
     initGraph(graph); 
    } 

    @Override 
    public void initGraph(final GraphView graph) { 
     Button btn = (Button) mActivity.findViewById(R.id.btnAddSeries); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       graph.addSeries(new LineGraphSeries(generateData())); 
      } 

     }); 

     btn = (Button) mActivity.findViewById(R.id.btnClear); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       graph.removeAllSeries(); 
      } 
     }); 
    } 

    private DataPoint[] generateData() { 
     Random rand = new Random(); 
     int count = 30; 
     DataPoint[] values = new DataPoint[count]; 
     for (int i=0; i<count; i++) { 
      double x = i; 
      double f = rand.nextDouble()*0.15+0.3; 
      double y = Math.sin(i*f+2) + rand.nextDouble()*0.3; 
      DataPoint v = new DataPoint(x, y); 
      values[i] = v; 
     } 
     return values; 
    } 
} 

來源:https://github.com/jjoe64/GraphView-Demos/blob/master/app/src/main/java/com/jjoe64/graphview_demos/examples/AddSeriesAtRuntime.java

1

最簡單的方法是將數據附加到現有的系列,即加入到該圖表。

GraphView graph = (GraphView) findViewById(R.id.gv); 
LineGraphSeries<DataPoint> lineGraphSeries = new LineGraphSeries<>(); 

graph.addSeries(lineGraphSeries); 

DataPoint dataPoint = sth... 
lineGraphSeries.appendData(dataPoint);