Handy Handy - 14 days ago 4
Android Question

Add value from Array to an XY Series

I want to add a value from SQLite to an XY series to create a diagram. Here is what I've done so far:

Double a, b;
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();

Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
ArrayList x = new ArrayList();
ArrayList y = new ArrayList();
notesCursor.moveToFirst();

for(int i = 0; i<notesCursor.getCount();i++){
a = notesCursor.getDouble(notesCursor.getColumnIndex(mDbHelper.KEY_ROWID));
b = notesCursor.getDouble(notesCursor.getColumnIndex(mDbHelper.KEY_RESULT));
}
x.add(a);
y.add(b);

notesCursor.moveToNext();

mCurrentSeries.add(x, y);
if (mChartView != null) {
mChartView.repaint();
}


But I've got a problem at the line:


mCurrentSeries.add(x, y);



How I can add an Array to an XY series? Any suggestions?

Answer

not sure what library you are using here to create your diagram (chart?). I use aChartEngine which is pretty good. The code I use to get a dataset containing a time series in aChartEngine is:

public static XYMultipleSeriesDataset getDemoDataset(Cursor c,
            String title, String... columnName) {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        TimeSeries series = new TimeSeries(title);


        try {
            if (c.moveToFirst()) {
                do {
                    int mins = c.getInt(c.getColumnIndex(columnName[0]));


                    java.util.Date date =null;
                    try{
                        date = DateFactory.stringToDate(c.getString(c.getColumnIndex(columnName[1])));
                    }catch(Exception e){

                    }
                    if(date==null){
                        continue;
                    }
                    series.add(date, mins);
                } while (c.moveToNext());
            } else {
                Log.d(TAG, "There were no values in the cursor.");
            }
        } finally {
            Log.d(TAG, "finally from getDemoDataset being called");
            c.close();
        }

        dataset.addSeries(series);

        return dataset;
    }