Skip to content

Instantly share code, notes, and snippets.

@ueno-yuhei
Last active August 29, 2015 14:01
Show Gist options
  • Save ueno-yuhei/082511700c5ba9c1780f to your computer and use it in GitHub Desktop.
Save ueno-yuhei/082511700c5ba9c1780f to your computer and use it in GitHub Desktop.
Holo Graph libraryを使用したグラフ作成
// https://bitbucket.org/danielnadeau/holographlibrary/wiki/Home をimportして使う
// http://dev.classmethod.jp/smartphone/android/android-tips-holographlibrary/ 参考
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import com.echo.holographlibrary.Line;
import com.echo.holographlibrary.LineGraph;
import com.echo.holographlibrary.LinePoint;
public class MainActivity extends Activity {
private ExampleOpenHelper mExampleOpenHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mExampleOpenHelper = new ExampleOpenHelper(this);
SQLiteDatabase db = mExampleOpenHelper.getReadableDatabase(); //データベース取得
Cursor cursor = db.rawQuery("SELECT * from example", new String[] {});
Log.d("TAG",""+cursor.getCount());
int number = 0;
Line line = new Line();
boolean next = cursor.moveToFirst();
while (next) {
LinePoint linePoint = new LinePoint();
linePoint.setY(cursor.getFloat(1));
linePoint.setX(number);
linePoint.setColor(Color.parseColor("#9acd32"));
line.addPoint(linePoint);
next = cursor.moveToNext();
number++;
}
line.setColor(Color.parseColor("#9acd32"));
LineGraph graph = (LineGraph) findViewById(R.id.graph);
graph.addLine(line);
graph.setRangeY(0, 1);
graph.setRangeX(0, cursor.getCount());
cursor.close();
db.close();
mExampleOpenHelper.close();
}
private class ExampleOpenHelper extends SQLiteOpenHelper {
final static private int DB_VERSION = 1;
public ExampleOpenHelper(Context context) {
super(context, "sample.db", null, DB_VERSION);
}
// 初回起動時のみ処理される
@Override
public void onCreate(SQLiteDatabase db) {
/**
* name: exampleテーブル
* id:integer | memo:real
*/
db.execSQL("CREATE TABLE example(id INTEGER PRIMARY KEY AUTOINCREMENT, memo REAL NOT NULL);");
// 初期データの投入
for(int i=0;i<=99;i++){
db.execSQL("INSERT INTO example(memo) values('"+ Math.random() +"')");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment