XY Scatter AndroidPlot

时间:2014-07-22 17:08:25

标签: scatter-plot androidplot

尝试使用AndroidPlot创建XY散点图,遇到问题...其中绘图仅从左到右绘制点,基本上是滚动图。

示例...说我有以下坐标,(0,1),(1,0),(0,-1),( - 1,0)我希望看到钻石形状(如果所有的点都用一条线连接起来)

我之前已经成功使用过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 个答案:

答案 0 :(得分:2)

这看起来可能是问题 - 在XYSeriesShimmer:

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

这总是会返回i,这意味着每个元素的x值比前一个大1 ...正是您所经历的。尝试将其更改为:

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