4

I had created dynamic line graph using AChartEngine Api, which shows the changes in G-Force value. All works fine but when graph moves ahead in X-axis i have to manually scroll in X-axis to see it further, please check my image below if you are not getting me properly.

enter image description here

how can i make it auto scrollable in x-axis to avoid manually scrolling?

Following is my code

public class GraphActivity extends Activity implements SensorEventListener{

    private LinearLayout lyGforce;


    //=== G-force =======
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private float gOffset;
    private boolean calibrate;
    private static TimeSeries timeSeriesGforce;
    private static XYMultipleSeriesDataset datasetGforce;
    private static XYMultipleSeriesRenderer rendererGforce;
    private static XYSeriesRenderer rendererSeriesGforce;
    private static GraphicalView viewGforce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);

        lyGforce = (LinearLayout) findViewById(R.id.lygforce);

        //=========== G-force =======
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        datasetGforce = new XYMultipleSeriesDataset();
        rendererGforce = new XYMultipleSeriesRenderer();
        rendererGforce.setApplyBackgroundColor(true);
        rendererGforce.setBackgroundColor(Color.argb(100, 50, 50, 50));
        rendererGforce.setAxisTitleTextSize(16);
        rendererGforce.setChartTitleTextSize(20);
        rendererGforce.setLabelsTextSize(15);
        rendererGforce.setLegendTextSize(15);
        rendererGforce.setMargins(new int[] { 20, 30, 15, 0 });
        rendererGforce.setZoomButtonsVisible(true);
        rendererGforce.setPointSize(10);
        rendererGforce.setShowGrid(true);
        rendererGforce.setGridColor(Color.WHITE);

        rendererSeriesGforce = new XYSeriesRenderer();
        rendererSeriesGforce.setColor(Color.GREEN);
        rendererSeriesGforce.setLineWidth(2);

        rendererGforce.addSeriesRenderer(rendererSeriesGforce);

        timeSeriesGforce = new TimeSeries("G-Force");

        datasetGforce.addSeries(timeSeriesGforce);

        viewGforce = ChartFactory.getTimeChartView(this, datasetGforce, rendererGforce, "");
        viewGforce.refreshDrawableState();
        viewGforce.repaint();

        lyGforce.addView(viewGforce);


    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        // ============= G-force ==================//
        calibrate = true;
        mSensorManager.registerListener(this, mAccelerometer,
                SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();

        //========== G-force=============//
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    //========== G-force=============//
    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub

        switch (event.sensor.getType()) {

            case Sensor.TYPE_ACCELEROMETER:

                float x = event.values[0];
                float y = event.values[1]; // Use to calculate G-Force value
                float z = event.values[2];

                if(calibrate)
                    gOffset = y - SensorManager.GRAVITY_EARTH;

                double gForce = ((y - gOffset) / SensorManager.GRAVITY_EARTH);

                timeSeriesGforce.add(new Date(), gForce);

                  rendererGforce.setXAxisMax(rendererGforce.getXAxisMax() + 30);
           rendererGforce.setXAxisMin(rendererGforce.getXAxisMin() + 30);

                viewGforce.repaint();

                Log.e("NIRAV","G-Force: "+String.valueOf(gForce));

                calibrate = false;

            break;

            default:
                break;
        }
    }

}
Nirav Dangi
  • 3,607
  • 4
  • 49
  • 60

1 Answers1

7

You can do this by changing the visible area on the X axis:

renderer.setXAxisMin(min);
renderer.setXAxisMax(max);
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • @Dan.. thanx for reply.. plz check my question i add my code plz let me know where and how to use setXAxisMin() & setXAxisMax() method ? – Nirav Dangi May 15 '13 at 15:18
  • Put the code before the repaint() call. The min and max should be the X axis values that you want the visible range to be between. – Dan D. May 15 '13 at 15:20
  • @Dan.. it worked thnx.. +1... but it scrolls slower then my graphs goes ahead in x-axis.. i edited my code and put both method before repaint() as you told.. plz help little more.. what value should be of "max" and "min" ? – Nirav Dangi May 15 '13 at 15:43
  • Max should probably be the most recent added X value and min is your choice. For example the value at index lastIndex - 50 or something. – Dan D. May 15 '13 at 15:56
  • i am doing exactly this, plz check my code i had taken my choice value as 30 but still it does not moves as it should be.. Is der any way get exact "my choice" value.?.. @Dan – Nirav Dangi May 15 '13 at 16:05
  • What is this: rendererGforce.setXAxisMax(rendererGforce.getXAxisMax() + 30); ? Is it a joke? – Dan D. May 15 '13 at 16:09
  • rendererGforce.setXAxisMax(new Date()); – Dan D. May 15 '13 at 16:09
  • 1
    Heyy @Dan, how can avoid situation to do manual scroll for first time, after doing it graph start auto scrolling...any idea/suggestion? – CoDe Feb 20 '14 at 05:55
  • @Nirav Thanks for Code. i am looking for such kind of source code and getting here. thanks again. – himen patel Mar 03 '15 at 12:48