I am new to programming. I am developing an android application where I have to plot graph using SQLite database. One of the table that I am using for Graph is Table_order which has item_id, item_name, table_number and order_date as columns.But it doesn't get plot to tha GraphView I am using. The code is below is the java class for GraphView.
public class MomoGraph extends AppCompatActivity {
GraphView momoGraphView;
LineGraphSeries < DataPoint > series;
DatabaseHelper myDBHelper;
SQLiteDatabase mDatabase;
TableOrderDBAdapter mOrderDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_momo_graph);
momoGraphView = (GraphView) findViewById(R.id.graphViewMomo);
myDBHelper = new DatabaseHelper(this);
mOrderDBHelper = new TableOrderDBAdapter(this);
series = new LineGraphSeries < > (getDataPoint());
momoGraphView.addSeries(series);
series.resetData(getDataPoint());
}
private DataPoint[] getDataPoint() {
myDBHelper.getWritableDatabase();
int n = mOrderDBHelper.getX_axis().size();
DataPoint[] values = new DataPoint[n];
for (int i = 0; i < n; i++) {
DataPoint v = new DataPoint(Double.parseDouble(mOrderDBHelper.getX_axis().get(i)), Double.parseDouble(mOrderDBHelper.getY_axis().get(i)));
values[i] = v;
}
return values;
}
The code below is the Xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MomoGraph">
<com.jjoe64.graphview.GraphView
android:id="@+id/graphViewMomo"
android:layout_width="379dp"
android:layout_height="520dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
The code of Table Adapter class where I stored the values from database into arraylist.
public ArrayList<String> getX_axis(){
ArrayList<String> Xval = new ArrayList<String>();
open();
mDatabase = mDBHelper.getReadableDatabase();
Cursor x_cursor = mDatabase.rawQuery("SELECT ORDER_DATE FROM TABLE_ORDER WHERE ITEM_ID =? GROUP BY ORDER_DATE",new String[]{"'198'"});
try{
if(x_cursor!= null){
if(x_cursor.moveToFirst()){
do {
String levelData = x_cursor.getString(x_cursor.getColumnIndexOrThrow("ORDER_DATE"));
Xval.add("" + levelData);
}
while (x_cursor.moveToNext());
}
}
}
catch (SQLException e){
Log.e("Retrieve data","Unable to get Data"+ e);
}
x_cursor.close();
close();
return Xval;
}
public ArrayList<String> getY_axis(){
open();
mDBHelper.getReadableDatabase();
ArrayList<String> Yval = new ArrayList<String>();
Cursor y_cursor = mDatabase.rawQuery("SELECT COUNT(ITEM_ID) FROM TABLE_ORDER WHERE ITEM_ID =? GROUP BY ORDER_DATE",new String[]{"'198'"});
try{
if(y_cursor!= null){
if(y_cursor.moveToFirst()){
do {
String levelData = y_cursor.getString(y_cursor.getColumnIndexOrThrow("ITEM_ID"));
Yval.add(""+levelData);
}
while (y_cursor.moveToNext());
}
}
}
catch (SQLException e){
Log.e("Retrieve data","Unable to get Data"+ e);
}
y_cursor.close();
close();
return Yval;
}
}
series = new LineGraphSeries < > (getDataPoint());
momoGraphView.addSeries(series);
series.resetData(getDataPoint()); //once remove this
private DataPoint[] getDataPoint() {
myDBHelper.getWritableDatabase();
ArrayList<String> Xval = mOrderDBHelper.getX_axis();
ArrayList<String> Yval = mOrderDBHelper.getY_axis();
if (Xval==null || Xval.size()==0)
return new DataPoint[0];
DataPoint[] values = new DataPoint[Xval.size()];
for (int i = 0; i < Xval.size(); i++) {
values[i] = new DataPoint(Double.parseDouble(Xval.get(i)), Double.parseDouble(Yval.get(i)));
}
return values;
}
Try this code once.