Achartengine显示从sqlite到Line Graph的双重和整数数据

时间:2014-01-16 12:51:16

标签: android sqlite achartengine linegraph

您好我有以下示例如何从数据库中提取数据以放入折线图。我有两个字段,total是double,asup是整数。我想显示两行,一行为总数,另一行为asup。 当我运行它,它变成了空白屏幕,但应用程序不会崩溃。而我的表现看起来很好。 如何在线图中显示双线和整数数据?

我的图表类

public class DbGraph2 extends Activity{

    public DbGraph2(){
        super();
    }
    private View mChart;

    private String[] mililiter = new String[] {
        "1000","1250","1500","1750","2000","2250","2500","2750","3000","3250","3500","3750","4000"
    };

    private static final String TAG = DbGraph2.class.getSimpleName();

    // database configuration
    // if you want the onUpgrade to run then change the database_version
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "HasilAsupan.db";

    // table configuration
    private static final String TABLE_NAME = "AsupanAir";         // Table name
    private static final String HASIL_COLUMN_ID = "_id";     // a column named "_id" is required for cursor
    private static final String HASIL_COLUMN_BBI = "hasil_BBI";
    private static final String HASIL_COLUMN_TOTAL = "hasil_Total";
    private static final String HASIL_COLUMN_LITER = "hasil_Liter";
    private static final String HASIL_COLUMN_ASUP = "Jumlah_Asupan";

    private DatabaseOpenHelper openHelper;
    private SQLiteDatabase database;

    private Context c;

    private XYSeries series2;
    private static Context aContext;

    public DbGraph2(Context aContext) {
         aContext = c;
        openHelper = new DatabaseOpenHelper(aContext);
        database = openHelper.getWritableDatabase();
    } 

    public void insertData (String bbi, String total, String liter, String asup) {

        // we are using ContentValues to avoid sql format errors

        ContentValues contentValues = new ContentValues();

        contentValues.put(HASIL_COLUMN_BBI, bbi);
        contentValues.put(HASIL_COLUMN_TOTAL, total);
        contentValues.put(HASIL_COLUMN_LITER, liter);
        contentValues.put(HASIL_COLUMN_ASUP, asup);

        database.insert(TABLE_NAME, null, contentValues);
    }

    public Cursor getAllData () {

        String buildSQL = "SELECT * FROM " + TABLE_NAME;

        Log.d(TAG, "getAllData SQL: " + buildSQL);

        return database.rawQuery(buildSQL, null);
    }

    // this DatabaseOpenHelper class will actually be used to perform database related operation 

    private class DatabaseOpenHelper extends SQLiteOpenHelper {

        public DatabaseOpenHelper(Context aContext) {
            super(aContext, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            // Create your tables here

            String buildSQL = "CREATE TABLE " + TABLE_NAME + "( " + HASIL_COLUMN_ID + " INTEGER PRIMARY KEY, " +
                    HASIL_COLUMN_BBI + " INTEGER, " + HASIL_COLUMN_TOTAL + " REAL, " + HASIL_COLUMN_LITER+"NUMERIC,"+HASIL_COLUMN_ASUP+" INTEGER )";

            Log.d(TAG, "onCreate SQL: " + buildSQL);

            sqLiteDatabase.execSQL(buildSQL);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
            // Database schema upgrade code goes here

            String buildSQL = "DROP TABLE IF EXISTS " + TABLE_NAME;

            Log.d(TAG, "onUpgrade SQL: " + buildSQL);

            sqLiteDatabase.execSQL(buildSQL);       // drop previous table

            onCreate(sqLiteDatabase);               // create the table from the beginning
        }
    }

        public String geTotalID() {
            String[] column = new String[] {HASIL_COLUMN_ID};
            Cursor c = database.query(TABLE_NAME, column, null,null,null,null,null);

            String result ="";
            int iID = c.getColumnIndex(HASIL_COLUMN_ID);
            for (c.moveToFirst(); ! c.isAfterLast(); c.moveToNext()) {
                result = result + c.getString(iID);
            }
            return result;
        }

        public String getTotal() {
            String[] column = new String[] {HASIL_COLUMN_TOTAL};
            Cursor c = database.query(TABLE_NAME,column,null,null,null,null,null);

            String result = "";
            int iTotal = c.getColumnIndex(HASIL_COLUMN_TOTAL);
            for (c.moveToFirst(); ! c.isAfterLast(); c.moveToNext()) {
                result = result + c.getString(iTotal);
            }
            return result;
        }

        public String getAsup () {
            String[] column = new String[] {HASIL_COLUMN_ASUP};
            Cursor c = database.query(TABLE_NAME,column, null,null,null,null,null);

            String result = "";
            int iAsup = c.getColumnIndex(HASIL_COLUMN_ASUP);
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                result = result + c.getString(iAsup);
            }
            return result;
        }

        public void updateEntry (long id, String total, String asup) {
            ContentValues cvUpdate = new ContentValues();

            cvUpdate.put(HASIL_COLUMN_TOTAL, total);
            cvUpdate.put(HASIL_COLUMN_ASUP, asup);
            database.update(TABLE_NAME, cvUpdate, HASIL_COLUMN_ID + "= ", null);
        }

        public String getTotal (long id) {
            return null;
        }
        public String getAsup (long id) {
            return null;
        }

        public XYMultipleSeriesDataset getDemoDataSet (String title){
            String[] column = new String[] {HASIL_COLUMN_TOTAL};
            Cursor c = database.query(TABLE_NAME,column,null,null,null,null,null);

            XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

            XYSeries series = new XYSeries("line1");
            XYSeries series2 = new XYSeries(title);

            getTotal();

            while (! c.isAfterLast()) {
                int total = c.getInt((int) c.getColumnIndexOrThrow("total"));
                int asup = c.getInt((int) c.getColumnIndexOrThrow("asup"));
                series2.add(total, asup);
                c.moveToNext();
            }
            c.close();

            dataset.addSeries(series);
            dataset.addSeries(series2);

            return dataset;             
            }

        public Intent getIntent (Context content) {

            // Creating a dataset to hold each series
            XYMultipleSeriesDataset dataset = getDemoDataSet("line1");

            XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();

            // Creating XYSeriesRenderer to customize incomeSeries
            XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
            incomeRenderer.setColor(Color.WHITE);
            incomeRenderer.setPointStyle(PointStyle.CIRCLE);
            incomeRenderer.setFillPoints(true);
            incomeRenderer.setLineWidth(2);
            incomeRenderer.setDisplayChartValues(true);

            // Creating XYSeriesRenderer to customize expenseSeries
            XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
            expenseRenderer.setColor(Color.YELLOW);
            expenseRenderer.setPointStyle(PointStyle.CIRCLE);
            expenseRenderer.setFillPoints(true);
            expenseRenderer.setLineWidth(2);
            expenseRenderer.setDisplayChartValues(true);

            multiRenderer.addSeriesRenderer(incomeRenderer);

            // Creating a XYMultipleSeriesRenderer to customize the whole chart

            multiRenderer.setXLabels(0);
            multiRenderer.setChartTitle("Income vs Expense Chart");
            multiRenderer.setXTitle("Year 2012");
            multiRenderer.setYTitle("Amount in Dollars");
            multiRenderer.setZoomButtonsVisible(true);

            multiRenderer.addSeriesRenderer(expenseRenderer);
            Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer,"asfsf");
            // Getting a reference to LinearLayout of the MainActivity Layout
            LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_containeer);

            // Creating a Line Chart
//          mChart = ChartFactory.getLineChartView(getBaseContext(), dataset, multiRenderer);

            // Adding the Line Chart to the LinearLayout
//          chartContainer.addView(mChart);

        return intent;
        }
     }

清单:

<activity
            android:name="com.example.perhitunganasupancairantubuh.DbGraph2"
            android:label="@string/title_activity_menu_grafik" >
            <intent-filter >
                <action android:name="android.intent.action.View"/>
            </intent-filter>
        </activity>

0 个答案:

没有答案