series.add doesn't add new series
I've modified XYseries demo for charting with Android using AChartEngine. It doesn't add the second array as a new series. Here is my code.
private XYMultipleSeriesDataset getDemoDataset() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
final int nr = 10;
double[] test1=new double[] {121.0,122.0,123.0,124.0,125.0,126.0,127.0,128.0,129.0,130.0};
double[] test2=new double[] {131.0,132.0,133.0,134.0,135.0,136.0,137.0,138.0,139.0,140.0};
double[] testArray= new double[test1.length + test2.length];
System.arraycopy(test1, 0, testArray, 0, test1.length);
System.arraycopy(test2, 0, testArray, test1.length, test2.length);
for(double d:testArray){
Log.i(MAIN_DEBUG_TAG, "testArray "+d);
}
Random r = new Random();
for (int i = 0; i < SERIES_NR; i++) {
XYSeries series = new XYSeries("Demo series " + (i + 1));
for (int k = 0; k < nr; k++) {
// series.add(k, 120 + r.nextInt() % 100);
series.add(k, testArray[k]);
Log.i(MAIN_DEBUG_TAG, "series.add "+k +" "+test1[k]);
}
dataset.addSeries(series);
}
return dataset;
}
Test1 array is repeated. If I remove the comments from series.add(k,120+ random...... i开发者_JAVA百科t will genereated 2 series of data to plot. Any help appreciated. TIA
You're adding the same data points twice - testArray[0]
to testArray[9]
in both iterations of the outer loop. k
always goes from 0
to 9
, and the current code doesn't rely on anything other than k
:
// No sign of anything that changes between iterations of the outer loop
series.add(k, testArray[k]);
How about:
series.add(k, testArray[k + i * nr]);
That way the first series will have testArray[0]
to testArray[9]
, and the second series will have testArray[10]
to testArray[19]
.
精彩评论