I am having trouble displaying my data from my sqlite database in a line graph using Graph view.
My database has two columns, ID, Points
So my database should look as follows:
ID|Points
1 | 35
2 | 55
3 | 62
However i'm not sure as to how I could implement this in GraphView.
My database:
public static final String DATABASE_NAME = "mydatabase.db";
public static final String TABLE_NAME = "my_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "POINTS";
x axis = ID y axis = POINTS
Any help on how to implement this would be appreciated.
First get data from database
SQLiteDatabase db = this.getWritableDatabase();
Cursor myCursor=db.rawQuery("select * from "+TABLE_NAME,null);
create an arraylist and add data in it.
if(myCursor.getCount()==0){
// contains nothing so show some message to user
return;
}
else {
ArrayList<Integer> mylist = new ArrayList<Integer>;
//
while(myCursor.moveToNext()){
// dont need first column if we use loop for arraylist
//mylist.add(myCursor.getInt(cursor.getColumnIndex("COL_1")));
mylist.add(myCursor.getInt(cursor.getColumnIndex("COL_2")));
}
now create a graph object and add your data (it will show just 3 datasets but you can create an loop and show all data
raphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
//just for 3 datasets
new DataPoint(0,mylist.get(0)),
new DataPoint(1,mylist.get(1)),
new DataPoint(2,mylist.get(2))
});
graph.addSeries(series);
hope answerd the question ( if its useful than vote up )