Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do auto Pan (scrolling) with AChartEngine dynamic line graph along with X-axis?

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

}
like image 607
Nirav Dangi Avatar asked Mar 23 '23 22:03

Nirav Dangi


1 Answers

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

renderer.setXAxisMin(min);
renderer.setXAxisMax(max);
like image 149
Dan D. Avatar answered Apr 06 '23 02:04

Dan D.