如何在achartengine情节中慢慢制作图表

时间:2011-12-04 10:46:46

标签: java android graph achartengine

是否可以使用achartengine库来制作它,以便在绘制图形时它会慢慢地进行。喜欢每一毫秒绘制一个点?不确定是否应该做出改变。

1 个答案:

答案 0 :(得分:3)

是的,这是可能的。

您可以按时间间隔执行以下操作:

  1. 将(x,y)对添加到当前系列
  2. 重绘图表
  3. 通过这种方式,您可以“慢慢地”绘制图形。

    这是我写的一个示例,它将间隔绘制y = x ^ 2图。这不完全是你想做的,但原则是一样的,玩它并根据你的需要修改它,告诉我你是否遇到任何问题。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plot);
    
        // Buttons
        mButtonStart = (Button) findViewById(R.id.button_plot_start);
        mButtonStop = (Button) findViewById(R.id.button_plot_stop);
    
        mButtonStart.setOnClickListener(this);
        mButtonStop.setOnClickListener(this);
    
        // Chart
        mRenderer.setZoomButtonsVisible(true);
        String seriesTitle = "SAMPLE"
    
        XYSeries series = new XYSeries(seriesTitle);
        mDataSet.addSeries(series);
        mCurrentSeries = series;
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setPointStyle(PointStyle.SQUARE);
        renderer.setFillPoints(true);
        renderer.setDisplayChartValues(true);
        renderer.setColor(Color.RED);
        mRenderer.addSeriesRenderer(renderer);
        mRenderer.setXLabels(0);
    }
    
    @Override
    protected void onRestart() {
        super.onRestart();
    }
    
    @Override
    protected void onResume() {
    
        super.onResume();
        if (mChartView == null) {
            // Enable click and pan
            mRenderer.setClickEnabled(true);
            mRenderer.setPanEnabled(true, true);
    
            LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout_chart);
            mChartView = ChartFactory.getLineChartView(this, mDataSet, mRenderer);
            mRenderer.setClickEnabled(true);
            mChartView.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                }
            });
            mChartView.setOnLongClickListener(new OnLongClickListener() {
                public boolean onLongClick(View v) {
                    return false;
                }
            });
            mChartView.addZoomListener(new ZoomListener() {
    
                public void zoomApplied(ZoomEvent e) {
    
                }
                public void zoomReset() {
                }
            }, true, true);
    
            mChartView.addPanListener(new PanListener() {
                public void panApplied() {
                }
            });
    
            layout.addView(mChartView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        } else {
            mChartView.repaint();       
        }
    }
    
    
    @Override
    protected void onDestroy() {
        if (mTimerTask != null) {
            mTimerTask.cancel();
        }
        if (mTimer != null) {
            mTimer.cancel();
        }
        super.onDestroy();
    }
    
    
    
    protected void updateChart() {      
    
        mCurrentSeries.add(mX, mX*mX);
        mX++;
    
        // Update the chart by repaint()
        if (mChartView != null) {
            mChartView.repaint();
        }
    }
    
    
    /**
     * A TimerTask class
     *
     */
    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            updateChart();
        }
    }
    
    
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button_plot_start:
            // "Start" button clicked
    
            // Cancel current timer and timer task if existed
            if (mTimerTask != null ) {
                mTimerTask.cancel();
            }
            if (mTimer != null) {
                mTimer.cancel();
            }
    
            mTimer = new Timer();
            mX = 0;
            // Clear current X-axis
            mCurrentSeries.clear();
            mRenderer.clearXTextLabels();
    
                        mTimerTask = new MyTimerTask();
    
            // Delay 100(0.1s), interval is 3000 (3s)
            mTimer.schedule(mTimerTask, 100, 3000);
    
            break;
    
        case R.id.button_plot_stop:
            // "Stop" button clicked
    
            // Cancel current timer and timer task
            if (mTimerTask != null) {
                mTimerTask.cancel();
            }
            if (mTimer != null) {
                mTimer.cancel();
            }
            break;
        }
    
    }