1

Hi friends i have used graphs in my android application. In that i update graph in every second .Now i want to move this graph from right to left as it update the value.Any one have any idea how to do so? Any help appreciated.

 dataset = new XYMultipleSeriesDataset();

         renderer = new XYMultipleSeriesRenderer();

         renderer.setChartTitleTextSize(12);
         renderer.setLabelsTextSize(15);
         renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
         renderer.setApplyBackgroundColor(true);
         renderer.setBackgroundColor(Color.TRANSPARENT);
         renderer.setZoomEnabled(false, false);
         renderer.setPointSize(5f);
         renderer.setShowGrid(true);
         renderer.setXTitle("Time");
         renderer.setYTitle("Number");

         rendererSeries = new XYSeriesRenderer();

         rendererSeries.setColor(Color.BLUE);
         rendererSeries.setFillBelowLine(false);
         rendererSeries.setFillPoints(true);
         rendererSeries.setPointStyle(PointStyle.CIRCLE);
         renderer.addSeriesRenderer(rendererSeries);

         timeSeries = new TimeSeries("Random");
         mThread = new Thread(){
             public void run(){
                 while(true){
                     try {
                         Thread.sleep(2000L);
                     } 
                     catch (InterruptedException e) {
                         e.printStackTrace();
                     }

                     timeSeries.add(new Date(), random.nextInt(10));
                     mchartView.repaint();
                 }
             }
         };
         mThread.start();

         dataset.addSeries(timeSeries);
         mchartView = ChartFactory.getLineChartView(Pedometer.this, dataset, renderer);
         mchartView.refreshDrawableState();
         mchartView.repaint();

         llChart.addView(mchartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Kushal Shah
  • 1,303
  • 2
  • 17
  • 33

4 Answers4

2

You can use setRange to set the required range each time you add a new point. For example:

double maxX = timeSeries.getMaxX();
double minX = maxX - deltaX; // deltaX is your required x-range
double maxY = timeSeries.getMinY();
double minY = timeSeries.getMaxY();

renderer.setRange(new double[] { minX, maxX, minY, maxY });

This way your right edge always show the maximum time and the rest of the graph will move from right to left.

Hope it helps.

Dimath
  • 381
  • 1
  • 4
  • 13
0

If you want your graph to move, you need to add new data as you are doing. Once the graph is full (has as many points as you want) you need to start removing the oldest points. This will give you a window that will display the newest X number of points.

If you are using the TimeSeries I think your using you should be able to call timeSeries.setMaximumItemCount(maxNumPoints) and the time series will take care of the rest.

Hope this helps.

cstrutton
  • 5,667
  • 3
  • 25
  • 32
  • hi thnx for the response but timeSeries haven't show me this kind of property. – Kushal Shah May 30 '12 at 07:08
  • Does it have a delete method? What ia the import statement for the timeseries (what package are you using) I found several implimentations online and they all had a maxcount property and a delete method. – cstrutton May 30 '12 at 12:42
  • I can't be sure but it looks like you are using `aChartEngine`. The timeseries in this package includes a `remove(index)` method. If you know the index of the oldest item you could remove it that way. Or, try the `aFreeChart` package. It has the `setMaximumItemCount(maxNumPoints)` method which will make it all happen automatically. (I have no affiliation with any of these projects) – cstrutton May 30 '12 at 17:28
0

An easy way to let your graph auto-calculate its axes min and max is to set them to a min and max value, and then repaint. For example:

mRenderer.setXAxisMax(-Double.MAX_VALUE);
mRenderer.setXAxisMin(Double.MAX_VALUE);
mRenderer.setYAxisMax(-Double.MAX_VALUE);
mRenderer.setYAxisMin(Double.MAX_VALUE);

mChartView.repaint();

I don't know if it's the best option, but it works for me !

reference : My Android AChartEngine is already working, but how to make it look good?

Community
  • 1
  • 1
Julien
  • 797
  • 8
  • 9
0

In my code I came up with the following idea:

    private void animatePlot(float t, float[] v) {
            // moving plot
            datasets[i].add(t, v[i]);
            if (mPointsCount > GRAPH_RESOLUTION)
                datasets[i].remove(0);
            view.repaint();
            mPointsCount++;
    }
  • t and v correspond the x,y plot values.
  • t is growing.
  • when I get new data (t and v) I call this function.
  • GRAPH_RESOLUTION is the amount of points shown in the graph from which the plot starts moving to the left.
Michał Dobi Dobrzański
  • 1,449
  • 1
  • 20
  • 19