2

動的棒グラフを作成しました。しかし、棒グラフを更新して、データベースから新しい値があればそれを取得し、新しい値で棒グラフを再度プロットすることができません。私はデータベースを作成するためにsqliteを使用しています。achartengineを使用して棒グラフを作成しました。

4

1 に答える 1

0

カスタムチャートバーを作成します。SQLite DB で動作する dbHelper を作成します。

import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.provider.BaseColumns;
public class DBHelper extends SQLiteOpenHelper {
public static interface DESTINATION extends BaseColumns {
            String TABLE_NAME = "destination";
            String NAME = "name";
        }
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
private static final String CREATE_TABLE_DESTINATION = CREATE_TABLE + DESTINATION.TABLE_NAME + " (" 
                + DESTINATION._ID + " INTEGER PRIMARY KEY, " + DESTINATION.NAME + " TEXT );";
private static final String DATABASE_NAME = "TravelCompany";
        private static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
@Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE_DESTINATION);
        }
@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DESTINATION.TABLE_NAME);
            onCreate(db);
        }
public void insertDestination(String destinationName) {
            ContentValues values = new ContentValues();
            values.put(DESTINATION.NAME, destinationName);
            getWritableDatabase().insert(DESTINATION.TABLE_NAME, null, values);
        }
public Cursor getTop10Destinations() {
            return getReadableDatabase().query(true, DESTINATION.TABLE_NAME,
                    new String[] { DESTINATION.NAME, "COUNT(" + DESTINATION.NAME + ") AS counter" }, null, null,
                    DESTINATION.NAME, null, "counter DESC", "10");
        }
    }

次に、ボタンを使用して最初のアクティビティを作成します。

import android.app.Activity; <p>
import android.content.Intent; <p>
import android.os.Bundle; <p>
import android.view.View;
import android.view.View.OnClickListener;<p> 
import android.widget.Button;<p> 
import android.widget.LinearLayout;<p>

public class MainActivity extends Activity {
@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Button a = new Button(this);
            a.setText("!DRAW CHART!");
            this.addContentView(a, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT));
            a.setOnClickListener(new OnClickListener() {
@Override
                public void onClick(View v) {
                    Intent b = new Intent(MainActivity.this, ChartActivity.class);
                    startActivity(b);
}
            });
}
}

ボタンをクリックすると、BarChart クラスによるカスタム BarChart 描画で ChartActivity が作成されます。

package com.example.teaorcofee;
import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Random;
import android.app.Activity;
    import android.content.Context;
    import android.database.Cursor;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewGroup.LayoutParams;
    public class ChartActivity extends Activity {
    private DBHelper dbHelper = new DBHelper(this);
            private String[] desinationList = new String[] { "Australia", "Germaty", "China", "Russia", "France",
                    "UK", "USA", "Ukraine", "Moldova", "Greece", "Poland", "Brazil" };
@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_chart);
            for (int i = 0; i < 100; i++) {
                dbHelper.insertDestination(desinationList[(int) (Math.random() * desinationList.length)]);
            }
            BarChart barChart = new BarChart(this, dbHelper.getTop10Destinations());
            addContentView(barChart, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        }
private static class BarChart extends View {
            private static int THING_COUNT = 10;
            private LinkedHashMap&lt;String, Integer&gt; countArray = new LinkedHashMap&lt;String, Integer&gt;();
            private Integer maxAmout = 0;
            private static final int DISTANCE_BETWEEN_SIDE_AND_Y_AXIS = 30;
            private static int DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS;
            private static final int CHART_BG_COLOR = Color.BLACK;
            private Random rnd = new Random();
public BarChart(Context context, Cursor destinationList) {
                super(context);
                for (int i = 0; i < THING_COUNT; i++) {
                    if (destinationList.moveToNext()) {
                        countArray.put(destinationList.getString(0), destinationList.getInt(1));
                        if (maxAmout < destinationList.getInt(1)) {
                            maxAmout = destinationList.getInt(1);
                        }
                    } else {
                        break;
                    }
                }
                // countArray = sortByComparator(countArray);
            }
@Override
            protected void onDraw(Canvas canvas) {
                DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS = canvas.getHeight() -70;
//set chart BG color
                canvas.drawColor(CHART_BG_COLOR);
                Paint bgPaint = new Paint();
//draw white lines of X and Y axises of 2px width
                bgPaint.setColor(0xFFFFFFFF);
                bgPaint.setStrokeWidth(2);
                canvas.drawLine(DISTANCE_BETWEEN_SIDE_AND_Y_AXIS, 0, DISTANCE_BETWEEN_SIDE_AND_Y_AXIS, canvas.getHeight(), bgPaint);
                canvas.drawLine(0, DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS, canvas.getWidth(), DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS, bgPaint);
//calulate next to max value number round number
                //for example if max value is 683, than we will get 700
                maxAmout = (int) (((int) (maxAmout / Math.pow(10, maxAmout.toString().length() - 1) + 1)) * Math
                        .pow(10, maxAmout.toString().length() - 1));
//get iterator on set of keys 
                Iterator&lt;String&gt; iterator = countArray.keySet().iterator();
                for (double i = 0; i < THING_COUNT; i++) {
//set white color
                    bgPaint.setColor(0xFFFFFFFF);
//draw text on Y axis with numbers between 0 and rounded max value - maxAmount
                    canvas.drawText("" + (int) (maxAmout * i / THING_COUNT),
                            25 - ("" + (int) (maxAmout * i / THING_COUNT)).length() * 7,
                            (int) ((DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS) * (1 - i / THING_COUNT)) + 15, bgPaint);
//get name of current item
                    String itemName = iterator.next();
//draw name of current item under its column
                    canvas.drawText(itemName.length()>3?itemName.substring(0, 3):itemName, (float) (30 + i * 28) + 5, DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS+10,
                            bgPaint);
//get random color for current item column
                    bgPaint.setARGB(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
//draw column of current item
                    canvas.drawRect((float) (DISTANCE_BETWEEN_SIDE_AND_Y_AXIS + i * 28) + 5, DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS,
                            (float) (DISTANCE_BETWEEN_SIDE_AND_Y_AXIS + (i + 1) * 28),
                            (int) ((DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS) * (1 - (double) countArray.get(itemName) / maxAmout)),
                            bgPaint);
                }
//draw lines parallel to X axis
                for (double i = 0; i < 10; i++) {
                    bgPaint.setStrokeWidth(1);
                    bgPaint.setColor(0x44444444);
                    canvas.drawLine(0, (int) ((DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS) * (1 - i / 10)), canvas.getWidth(),
                            (int) ((DISTANCE_BETWEEN_BOTTOM_AND_X_AXIS) * (1 - i / 10)), bgPaint);
                }
}
}
}
于 2013-03-07T09:30:34.200 に答える