How to plot a real-time graph using an oscilloscope-like achartingin

I am working with achartengine. I have to read a txt file and plot it. I get a graph plot. But what I want to do is when the graph is nearing the end of the layout view, it should start from the very beginning, just like for an oscilloscope. I want my graph to be exactly like the graph in this link

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

      

What I have done so far:

    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 the help.

+3


source to share


2 answers


It should be pretty easy to use AChartEngine to draw dynamic charts . Just update the content of your dataset and call chartView.repaint()

.



+1


source


This is the task of animation. Use Bitmap to plot and use timer to periodically display one X, Y pair per call to a bitmap. Then call invalidate () to redraw the bitmap on the device screen.

The last thing you need is a trivial mod operation. Here are the possible code snippets for 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 take care of moving the cursor back and repeating the animation after reaching the end of the data.

0


source







All Articles