ChartEngine android,每秒从左到右移动一个图表

时间:2014-09-25 18:22:54

标签: android charts

我想用Android每秒从左到右移动图表:

如果第一个值例如是坐标(0,5),则稍后一秒 从队列中获取一个新值,并在x轴上添加到0。 坐标(0,5)现在必须向右移动,因此它变为(1,5)。

每秒都会从队列中获取一个y值,因此线图应该每秒移动一次。

使用ChartEngine框架执行此操作的最佳方法是什么? 我试图每秒重新绘制图表及其所有值,但我想有一种更好,更友好的方式来做到这一点

公共类MainActivity扩展了Activity {

private GraphicalView mChart;

private XYSeries visitsSeries ;
private XYMultipleSeriesDataset dataset;

private XYSeriesRenderer visitsRenderer;
private XYMultipleSeriesRenderer multiRenderer;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    // Setting up chart
    setupChart();

    // Start plotting chart
    new ChartTask().execute();

}

private void setupChart(){

    // Creating an  XYSeries for Visits
    visitsSeries = new XYSeries("Unique Visitors");


    // Creating a dataset to hold each series
    dataset = new XYMultipleSeriesDataset();
    // Adding Visits Series to the dataset
    dataset.addSeries(visitsSeries);        


    // Creating XYSeriesRenderer to customize visitsSeries
    visitsRenderer = new XYSeriesRenderer();
    visitsRenderer.setColor(Color.WHITE);
    visitsRenderer.setPointStyle(PointStyle.CIRCLE);
    visitsRenderer.setFillPoints(true);
    visitsRenderer.setLineWidth(2);
    visitsRenderer.setDisplayChartValues(true);


    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    multiRenderer = new XYMultipleSeriesRenderer();

    multiRenderer.setChartTitle("Visits Chart");
    multiRenderer.setXTitle("Seconds");
    multiRenderer.setYTitle("Count");
    multiRenderer.setZoomButtonsVisible(true);

    multiRenderer.setXAxisMin(0);
    multiRenderer.setXAxisMax(10);

    multiRenderer.setYAxisMin(0);
    multiRenderer.setYAxisMax(10);

    multiRenderer.setBarSpacing(2);

    // Adding visitsRenderer to multipleRenderer
    // Note: The order of adding dataseries to dataset and renderers to multipleRenderer
    // should be same
    multiRenderer.addSeriesRenderer(visitsRenderer);

    // Getting a reference to LinearLayout of the MainActivity Layout
    LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);


    mChart = (GraphicalView) ChartFactory.getLineChartView(getBaseContext(), dataset, multiRenderer);


    // Adding the Line Chart to the LinearLayout
    chartContainer.addView(mChart);

}


private class ChartTask extends AsyncTask<Void, String, Void>{

    // Generates dummy data in a non-ui thread


    @Override
    protected Void doInBackground(Void... params)
    {
        int i = 0;
        try{
            do{
                String [] values = new String[2];
                Random r = new Random();
                int visits = r.nextInt(10);
                System.out.println(String.valueOf(visits));

                values[0] = Integer.toString(i); // x-as denkek
                values[1] = Integer.toString(visits); //bijhorende waarde y-as


                publishProgress(values);                    
                Thread.sleep(1000);
                i++;    
            }while(i<=10);
        }catch(Exception e){ }
        return null;
    }

    // Plotting generated data in the graph
    @Override
    protected void onProgressUpdate(String... values) 
    {
        visitsSeries.add(Integer.parseInt(values[0]), Integer.parseInt(values[1]));
        mChart.repaint();
    }

}    

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

0 个答案:

没有答案