我使用AChartEngine Api创建了动态折线图,它显示了G-Force值的变化。一切正常,但是当图形在 X 轴上向前移动时,我必须在 X 轴上手动滚动才能进一步查看,如果您没有正确理解我,请查看下面的图片。
如何使其在 x 轴上自动滚动以避免手动滚动?
以下是我的代码
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;
}
}
}