4

I am working with achartengine. I have to read a txt file and plot the graph. I get the graph plot. But, what I want to do is when the graph reaches the end of the layout view, it should get plotted from the beginning view as similar to oscilloscope view. I want my graph exactly similar to the graph in this link

  http://www.youtube.com/watch?v=N6BuRqeUhqc.

What I have done so far is:

    private class ChartTask extends AsyncTask<Void, String, Void>{
         String[] values = new String[2];int i1=0;

        // Generates dummy data in a non-ui thread
        @Override
        protected Void doInBackground(Void... params) {
            int i = 0;

            try{

                do{

                    values[0] = Integer.toString(i);
                    values[1] = Integer.toString(amplitude[i]);

                    if(i<=600){
                        multiRenderer.setXAxisMax(600);

                    }
                    else if(i>600){

                        double minX = amplitudeSeries.getMaxX();
                        multiRenderer.setXAxisMin(minX);                     

                    } 
                    publishProgress(values);
                    Thread.sleep(1);

                    i++; 

                }while(i<=amplitude.length);}

            catch (Exception e1){
            }
            return null;

            }

Can someone help me with this. Thanks for anyone's help.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
Pooja
  • 77
  • 1
  • 6

2 Answers2

1

It should be quite easy to draw dynamic charts using AChartEngine. Just update the contents of your dataset and call chartView.repaint().

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

This is an animation task. Use Bitmap to hold the chart and use timer to plot periodically one X,Y pair per call into bitmap. After that, call invalidate() to repaint the bitmap on the screen of device.

The last thing you need is a trivial mod operation. Here are the possible code fragments of this periodically called code:

// Get position in bitmap from the iteration index 
int ig = mIteration % bitmap.getWidth(); 

// Erase the vertical line in bitmap
for (int y = 0, y < bitmap.getHeight(); i++) 
  bitmap.setPixel(ig, y, Color.WHITE); 

// Plot the point
bitmap.setPixel(ig, data[mIteration], Color.BLACK); 

// Advance mIteration field value.
mIteration = (mIteration + 1) % data.length; 

// Force to repaint component containing the bitmap.
invalidate(); 

this will care for you about returning the cursor back and repeating the animation after reaching the end of data.

Community
  • 1
  • 1
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • Thanks for your idea. I somehow figured to force plot to repaint from the first once it reaches end. Now my problem is the graph gets overwritten. The previous plot remains as such. So, how should i repaint after clearing the previous plot? And by clearing i should not loose the previous data. It should remain as such when i scroll.Can you help me with this pls? – Pooja Feb 01 '13 at 09:46
  • 1
    @Audrius Meškauskas You don't have to play with pixels for drawing dynamic charts in AChartEngine :) – Dan D. Feb 02 '13 at 16:39
  • @Dan from the [website](http://www.achartengine.org/content/demo.html) looks like a great engine for general charting but would it support the kind of animation he needs? Look into YouTube video linked from the question, to see how the animation should look like. – Audrius Meškauskas Feb 02 '13 at 17:03
  • @Audrius Meškauskas Yes, it definitely supports dynamic charting. I wrote a few. In order to get that ECG like behavior, you just have to know how and when to remove data from a previous series. See this as well: http://stackoverflow.com/questions/14643953/how-to-prevent-the-graph-from-overwriting-the-previous-plot-using-achartengine – Dan D. Feb 02 '13 at 17:27