Search code examples
androidgraphachartengine

Stuck in moving graph using achartEngine


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));

Solution

  • 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.