1

I want to create a graph based on the data in my SQLite database column. I have installed the achartengine.jar and I also know how to generate a graph on hard coded values in an array. But I am not able to figure out how to do it with the Sqlite data. Can anybody help me with links to tutorials on how to achieve this or maybe help me convert the database column into an array which I could use.

Thanks for any help!!

public class ReportGraph extends GraphicalActivity
{

    double[] fQty;
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        getIntent();

    }

    public Intent getIntent(Context context)
    {

        FuelStoredInfo reportInfo =new FuelStoredInfo(this);
        reportInfo.open();
        fQty=reportInfo.getReportData(this);
        reportInfo.close();



        CategorySeries series = new CategorySeries("Reports");
        for(int i = 0; i< fQty.length; i++)
        {
            series.add("Report"+(i+1), fQty[i]);

        }   



        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(series.toXYSeries());
        //dataset.addSeries(series1.toXYSeries());

        //customize bar 1
        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setDisplayChartValues(true);
        renderer.setChartValuesSpacing((float)0.5);
        renderer.setColor(Color.BLUE);




        XYMultipleSeriesRenderer rRenderer = new XYMultipleSeriesRenderer();
        rRenderer.addSeriesRenderer(renderer);
        rRenderer.setChartTitle("Reports");
        rRenderer.setXTitle("Date");
        rRenderer.setYTitle("Prices");


        Intent i =ChartFactory.getBarChartIntent(context, dataset, rRenderer, Type.DEFAULT);

    return i;

    }


    public double[] getfQty() {
        return fQty;
    }

    public void setfQty(double[] fQty) {
        this.fQty = fQty;



}

FuelStoredInfo.java (Database)

public FuelStoredInfo open() throws SQLiteException
    {
        OpenDBHelper ourHelper = new openDBHelper(ourContext);
               SQLiteDatabase   ourDatabase = ourHelper.getWritableDatabase();
        return this;

    }

    public double[] getReportData(ReportGraph reportGraph)
        {
            // TODO Auto-generated method stub
            String [] columns = new String[]{KEY_ROW_ID, KEY_KM, KEY_FUEL_QTY, KEY_FUEL_PRICE, KEY_TOTAL_COST, KEY_MILEAGE, KEY_DATE,KEY_TANK_FULL};

             double[] reportList ;
                int i = 0;

                String selectQuery = "SELECT  * FROM fuel_table";

               reportCursor = ourDatabase.rawQuery(selectQuery, null);
                int count = reportCursor.getCount();
                reportList = new double[count];

                if (reportCursor.moveToFirst()) 
                {
                    do {
                        reportList[i] = reportCursor.getDouble(2);
                        i++;

                        } while (reportCursor.moveToNext());
                }


                return reportList;
        }

Error log-->

07-23 13:16:11.393: E/AndroidRuntime(991): java.lang.NullPointerException
07-23 13:16:11.393: E/AndroidRuntime(991):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
07-23 13:16:11.393: E/AndroidRuntime(991):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
07-23 13:16:11.393: E/AndroidRuntime(991):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-23 13:16:11.393: E/AndroidRuntime(991):  at com.example.fuelcheck.FuelStoredInfo.open(FuelStoredInfo.java:104)
07-23 13:16:11.393: E/AndroidRuntime(991):  at com.example.fuelcheck.ReportGraph.getIntent(ReportGraph.java:36)
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Pooja Gaonkar
  • 1,546
  • 17
  • 27
  • [Inspire from here](http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/) – CRUSADER Jul 23 '13 at 07:03
  • @CRUSADER Thanks for commenting. But I already have my database setup and working. I just cant figure out how to convert a certain column into an array and use it further in plotting a graph. – Pooja Gaonkar Jul 23 '13 at 07:06
  • What you mean is you want to return String array from your db column.. Right?? – CRUSADER Jul 23 '13 at 07:15
  • @CRUSADER yes. I want to use the values from the DB column as points to plot a graph – Pooja Gaonkar Jul 23 '13 at 07:28
  • @CRUSADER It was some stupid errors that I had been doing. Just corrected them..Your code worked out just fine. Thanks again. – Pooja Gaonkar Jul 23 '13 at 11:50

1 Answers1

1

Here is sample you can use..

public String[] getAllContacts() {
    String[] contactList ;
    int i = 0;
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;//You fetch query

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    int count = cursor.getCount();
    contactList = new String[count];
    // looping through all rows and adding to array
    if (cursor.moveToFirst()) {
        do {
            contactList[i] = cursor.getString(0);//getting data from column 0
            i++;
            // Adding contact to list
            } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

I have fetched data from column 0, but you can fetch any column...

Hope this helps..

CRUSADER
  • 5,486
  • 3
  • 28
  • 64