I tried using the Achart Engine and then GraphView to plot data coming from a sensor (real time, live updating) in opposite x-axis direction. I am doing it this way since I want to plot the Magnitude (y) in function of Frequency (x) and I am receiving these magnitudes in decreasing frequency order (from 6000Hz down to 1000Hz). My problem is that Achartengine sorts the incoming data in increasing x-axis direction, therefore when I try to live update with x values decreasing the app crashes. Whereas GraphView simply reorders the points in the order received to plot them in increasing order again and removing the graph axis... Therefore both libraries don't achieve what I want to do. I tried initializing the plots with some values and then update the plots point by point, but it is not working well (the points are not plotted at the correct place on the plot and the values displayed are not the correct ones).
Thanks for any help! :)
Here is a small section of my code showing my implementation for Achartengine in my main activity:
public void lineGraphHandler() {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
gView = line.getView(this);
if (meas_exec == 1 || stim_end == 1 )
{
line.clearPoints();
stim_end = 0;
}
for (int i = 0; i < 22; i++) {
Point p = MockData.initialize(i); // We got new
// data!
Point n = MockData.initialize(i); // We got new
// data!
line.addNewPoints(p, n); // Add it to our graph
}
thread = new Thread() {
public void run()
{
for (int i = 21; i >= 0; i--) {
if(stim_end == 0)
{
try {
Thread.sleep(1800);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = MockData.getDataFromReceiver1(i); // We got new
// data!
Point n = MockData.getDataFromReceiver2(i); // We got new
// data!
line.addNewPoints(p, n); // Add it to our graph
gView.repaint();
}
}
meas_exec = 1;
}
};
thread.start();
layout.addView(gView);
}
From my MockData.java:
public class MockData {
public static Point initialize(int x)
{
// Our first data
double[] xi = { 1,1.091,1.189, 1.297, 1.414, 1.542, 1.682, 1.834, 2, 2.181, 2.378, 2.594, 2.828, 3.084, 3.364, 3.668, 4, 4.362, 4.757, 5.187, 5.657, 6.169}; // x values!
double[] yi = { -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10 }; // y values!
return new Point(xi[x], yi[x]);
}
public static Point getDataFromReceiver1(int x)
{
// Our simulated dataset sensor 1
double[] x1 ={ 1,1.091,1.189, 1.297, 1.414, 1.542, 1.682, 1.834, 2, 2.181, 2.378, 2.594, 2.828, 3.084, 3.364, 3.668, 4, 4.362, 4.757, 5.187, 5.657, 6.169}; // x values!
double[] y1 = { 1.4, 1.1, 1.5, 8.3, 11.4,-1, 2, 8.3, 11.4, 2, 8.3, 13, -10, 8.3, 11.4, 2, 8.3, 13, -10, 2, 0, 3 }; // y values!
return new Point(x1[x], y1[x]);
}
public static Point getDataFromReceiver2(int x)
{
// Our simulated dataset sensor 2
double[] x2 = { 1,1.091,1.189, 1.297, 1.414, 1.542, 1.682, 1.834, 2, 2.181, 2.378, 2.594, 2.828, 3.084, 3.364, 3.668, 4, 4.362, 4.757, 5.187, 5.657, 6.169};// x values!
double[] y2 = { 3, 3.4,-2, -10.6, -3, -8, -5, 0, 2 ,-3, -8, 2 ,-3, -15.0, -3, -8, 3, 3.4, 0, 2 , 2 ,-3}; // y values!
return new Point(x2[x], y2[x]);
}
}
EDIT
Here is an illustration of what I want to accomplish. The red arrow shows in which direction I want to plot the values (with the 1st ,2nd ,3rd... point order indicated)
http://i61.tinypic.com/164ccz.png
So the first point I plot is around 6kHz and last around 1kHz, but when I look at the plot it should be correctly displayed from 1kHz to 6kHz. So in the beginning the points on the left of the plot will be missing and will gradually be plotted.
I finally found a solution by simply initializing a table and then updating this table with the new samples and refreshing the plot for every new sample I gather.
Code example:
measurement_table[index] = measurement;
noise_table[index] = noise;
//Clear points in lineseries to add a new lineseries after
line.clearPoints();
//Update with a new lineseries
for(int i=0;i<table_size;i++)
{
Point p = new Point(x[i], measurement_table[table_size-1-i]);
Point n = new Point(x[i], noise_table[table_size-1-i]);
line.addNewPoints(p, n); // Add it to our graph
}
gView.repaint(); //Replot