2014-07-22 64 views
1

試圖使用AndroidPlot創建一個XY散點圖,遇到問題......因此該繪圖僅從左到右繪製點,基本上是滾動圖表。例如...說我有以下座標,(0,1),(1,0),(0,-1),(-1,0)我期望看到一個菱形(如果所有點都加入了一條線)XY Scatter AndroidPlot

我已經成功地使用了AndroidPlot庫,所以對於可用的方法有所瞭解。

是否有使用AndroidPlot庫的散點圖的任何示例?

希望我在這裏做的意義..

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collection; 

import pl.flex_it.androidplot.XYSeries; 

import com.androidplot.series.XYSeries; 
import com.androidplot.xy.BoundaryMode; 
import com.androidplot.xy.LineAndPointFormatter; 
import com.androidplot.xy.SimpleXYSeries; 
import com.androidplot.xy.XYPlot; 

import android.app.Fragment; 
import android.graphics.Color; 
import android.os.Bundle; 

import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

    public class Temp extends Fragment { 

     private static XYPlot xyPlot; 
     private XYSeriesShimmer series; 
     private LineAndPointFormatter series1Format; 
     private ArrayList<Number> ALdata1, ALdata2; 
     private int Adata1[], Adata2[]; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

      View rootView = inflater.inflate(R.layout.fragment_test, container, false); 

      // Import plot from the layout 
      xyPlot = (XYPlot) rootView.findViewById(R.id.xyPlot); 
      xyPlot.setDomainBoundaries(-2, 2, BoundaryMode.FIXED); // freeze the domain boundary: 
      xyPlot.setRangeBoundaries(-2, 2, BoundaryMode.FIXED); 

      ALdata1 = new ArrayList<Number>(); 
      ALdata2 = new ArrayList<Number>(); 
      ALdata1.clear(); 
      ALdata2.clear(); 

      Adata1 = new int[]{0,1,0,-1}; 
      Adata2 = new int[]{1,0,-1,0}; 

      series = new XYSeriesShimmer(ALdata1, ALdata2, 0, "Sightings in USA"); 
      series1Format = new LineAndPointFormatter(Color.TRANSPARENT, Color.BLACK, null); // line color, point color, fill color 
      xyPlot.addSeries(series, series1Format); 

      plotDataMethod(); 

      return rootView; 
     } 

     private void plotDataMethod() { 

      for(int i=0; i<Adata1.length; i++){ 
       ALdata1.add(Adata1[i]); 
       ALdata2.add(Adata2[i]); 

       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       series.updateData(ALdata1, ALdata2); 
       xyPlot.redraw(); 
      } 

     } 

    } 

編輯:

package pl.flex_it.androidplot; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

import com.androidplot.series.XYSeries; 

public class XYSeriesShimmer implements XYSeries { 
    private List<Number> dataX; 
    private List<Number> dataY; 
    private int seriesIndex; 
    private String title; 

    public XYSeriesShimmer(List<Number> datasource, int seriesIndex, String title) { 
     this.dataY = datasource; 
     this.seriesIndex = seriesIndex; 
     this.title = title; 
    } 

    public XYSeriesShimmer(List<Number> datasourceX, List<Number> datasourceY, int seriesIndex, String title) { 
     this.dataX = datasourceX; 
     this.dataY = datasourceY; 
     this.seriesIndex = seriesIndex; 
     this.title = title; 
    } 

    @Override 
    public String getTitle() { 
     return title; 
    } 

    @Override 
    public int size() { 
     return dataY.size(); 
    } 

    @Override 
    public Number getY(int index) { 
     return dataY.get(index); 
    } 

    @Override 
    public Number getX(int index) { 
     return index; 
    } 

    public void updateData(List<Number> datasourceX){ //dont need to use this cause, the reference is only stored, modifying the datasource externally will cause this to be updated as well 
     this.dataY=datasourceX; 
    } 

    public void updateData(List<Number> datasourceX, List<Number> datasourceY){ //dont need to use this cause, the reference is only stored, modifying the datasource externally will cause this to be updated as well 
     this.dataX=datasourceX; 
     this.dataY=datasourceY; 
    } 

} 
+1

這行看起來可疑的和不應該甚至編譯:系列=新XYSeries(ALdata1,ALdata2,0, 「在美國目擊」); < - 偶然是一個新的SimpleXYSeries嗎? – Nick

+0

感謝您的幫助尼克。我擴展了XYSeries類以獲取Xaxis列表和Yaxis列表,請參閱上面的EDIT。 androidPlot庫肯定可以繪製分散的點,而不是從左到右的繪圖我假設所以我確信我錯過了一些簡單的東西。你知道有散點圖嗎? – Strokes

回答

2

這看起來可能是問題 - 在XYSeriesShimmer:

@Override 
public Number getX(int index) { 
    return index; 
} 

這是總是會返回i,這意味着每個元素的x值比前一個更大......正是您所遇到的。嘗試將其更改爲這樣:

@Override 
public Number getX(int index) { 
    return dataX.get(i); 
} 
+0

成功!乾杯尼克 – Strokes