user1044585
user1044585

Reputation: 521

how to make a graph in achartengine plot slowly

Would it b possible using the achartengine library to make it so that when a graph is plotted it does it slowly. like plot a point every milisecond? Not really sure wer to put that change.

Upvotes: 1

Views: 1983

Answers (1)

Wenshan
Wenshan

Reputation: 700

Yes, it is possible.

You can make a timertask to do things below at intervals:

  1. add a (x,y) pair to current series
  2. repaint the graph

In this way, you can plot a graph "slowly".

Here is a sample written by me, which will draw a y = x^2 graph at intervals. It is not exactly what you want to do, but the principle is the same, play with it and modify it to suit your needs, tell me if you meet with any problem.

@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;
    }

}

Upvotes: 3

Related Questions