Android : how to draw a line without spikes using AChartEngine with fast incoming data

on Thursday, September 25, 2014


Overview


Three signals should be drawn as lines in a graph in a very fast way (10ms). Most of the time the graph shows three ramps without any error. Sometimes there are spikes in these ramps. This is what I have created:



  • activity_temp_linegraph.xml: contains the attributes for the graph

  • DynamicLineGraphActivity: which register the DynmicLineGraphPresenter and setContentView() to activity_temp_linegraph




Problem


As it turns out most of the time the ramps are just looking fine. Sometimes there seems to be a race condition and there are spikes in the ramps. Where are these spikes coming from?




DynmicLineGraph



private void initDataset() {
mData0 = new TimeSeries(graphName1);
... // add more grapNames

mDatasets = new XYMultipleSeriesDataset();
mDatasets.addSeries(mData0);
... // add mData1 and mData2
}

private void initRenderer(Context context) {

mRenderer0 = new XYSeriesRenderer();
... //setAttributes for example: setColor()

mMultiRenderer = new XYMultipleSeriesRenderer();
... // setAttributes for example: setXTitle()

mMultiRenderer.addSeriesRenderer(mRenderer0);
}

// the code in the other addPointAndRepaint1-2 is the same
public void addPointAndRepaint(PointXY p) {
addPoint(p, mData2);
repaint();

}

private synchronized void addPoint(PointXY p, TimeSeries mData) {

mData.add(p.getX(), p.getY());

// move XAxis boundaries if plot would exceed the right border
if (p.getX() >= mXAxisMax) {

mXAxisMin = mXAxisMin + (p.getX() - mLastXvalue);
mMultiRenderer.setXAxisMin(mXAxisMin);

mXAxisMax = mXAxisMax + (p.getX() - mLastXvalue);
mMultiRenderer.setXAxisMax(mXAxisMax);

}
mLastXvalue = p.getX();
}

} // end addPoint




Simulated Data with a Thread


I have tried it with a thread, that draws linear rising lines. But even than, there are spikes.



@Override
public void onStart() {
super.onStart();

Thread thread = new Thread() {

long initTimestamp = System.currentTimeMillis();

public void run() {
for (int i = 0; i < 10000; ++i) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}

if (runB) {
t += 0.1;
t = (System.currentTimeMillis()- initTimestamp)/1000.0;

simVal+=0.1;

if (simVal>9)
simVal=-6;

PointXY p0 = new PointXY(t , simVal-2);
PointXY p1 = new PointXY(t+0.02, simVal-1);
PointXY p2 = new PointXY(t+0.04, simVal);

mLineGraph.addPointAndRepaint(p0);
mLineGraph.addPointAndRepaint1(p1);
mLineGraph.addPointAndRepaint2(p2);
} } } };
thread.start();
}




What can I do? Where is the fault?


0 comments:

Post a Comment