android圖表
1. 安卓怎麼編寫圖形化界面(比如圖表)展示資料庫數據
這個圖形化界面是在layout布局文件中(.xml)編寫代碼,也可以將圖表以圖片形式放入UI文件里。資料庫的數據可以採取調用方法顯示在圖表相應id位置的。因為不清楚你具體要求是什麼,所以只能大概給你這么說一下
2. 繪制android圖表有幾種框架
AChartEngine(簡稱ACE)是Google的一個開源圖表庫(for Android)。它功能強大,支持散點圖、折線圖、餅圖、氣泡圖、柱狀圖、短棒圖、儀表圖等多種圖表。
3. Android中打造圖表可以使用JFreeChart嗎
應該是不可以的,就算可以,用Jfreechart對android來說也太重量級了。
android有開源的專門針對移動端的圖表組件。
例如:MPAndroidChart ,achartengine,GraphView等等很多開源的組件。
4. 如何在Android上將現有的數據,以圖表的形式展現在手機上。
主要思路是:
1. 把資料庫分解成幾個asset文件。
2. 當需要打開資料庫時,如果資料庫不存在,就把那幾個asset文件重新合並成一個資料庫文件。
3. 如果資料庫的版本改變了,就在onUpgrade()方法中把資料庫文件刪除掉。
下面是代碼:
//資料庫的預設路徑
private static finalString DB_PATH = "/data/data/com.mypackage.myapp/databases/";
private static finalString DB_NAME = "mydb.db";
private static finalint DB_VERSION = 2;
private static finalString DB_SPLIT_NAME = "mydb.db.00";
private static finalint DB_SPLIT_COUNT = 3;
private SQLiteDatabasem_database;
private final Contextm_context;
/**
* Constructor
*保存傳進來的context參數以用來訪問應用的asset和資源文件。
* @param context
*/
public MyDB(Contextcontext) {
super(context, DB_NAME, null, DB_VERSION);
this.m_context = context;
}
public static MyDBopenDatabaseReadOnly(Context context) {
MyDB db = new MyDB(context);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.openDataBase(SQLiteDatabase.OPEN_READONLY);
return db;
}
public static MyDBopenDatabaseReadWrite(Context context) {
MyDB db = new MyDB(context);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.openDataBase(SQLiteDatabase.OPEN_READWRITE);
return db;
}
/**
*創建一個空資料庫,用來存儲你已有的資料庫。
*/
public voidcreateDataBase() throws IOException{
boolean dbExist =checkDataBase();
if (dbExist) {
/*
**如果你的資料庫的版本改變了,調用這個方法確保在onUpgrade()被調用時
**傳進去的是可寫的資料庫。
5. 如何在android畫分析圖(例如 柱狀圖、趨勢圖、餅圖)
目前android上圖標引擎並不少見,像aChartEngine就能很好的完成繪圖:
aChartEngine支持:1、linechart(折線圖)2、areachart(面積圖;分區圖,對比圖)3、scatterchart(散點圖)4、timechart(時間圖;進度表)5、barchart(條形圖;柱狀圖)6、piechart(餅圖)7、bubblechart(氣泡圖)8、doughnutchart(圓環圖)9、range(high-low)barchart(范圍條形圖)10、dialchart/gauge(撥號盤/壓力表)11、combined(anycombinationofline,cubicline,scatter,bar,rangebar,bubble)chart(組合圖)12、cubiclinechart(立方折線圖)
上述所有支持的圖表類型,都可以包含多個系列,都支持水平(默認)或垂直方式展示圖表,並且支持許多其他的自定義功能。所有圖表都可以建立為一個view,也可以建立為一個用於啟動activity的intent.
下面是一個餅狀圖的源碼事例:
package org.achartengine.chartdemo.demo.chart;
import org.achartengine.ChartFactory;
import org.achartengine.renderer.DefaultRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
public class BudgetPieChart extends AbstractDemoChart {
public String getName() {
return "Budget chart";
}
public String getDesc() {
return "The budget per project for this year (pie chart)";
}
public Intent execute(Context context) {
double[] values = new double[] { 12, 14, 11, 10, 19 };//餅圖分層5塊,每塊代表的數值
int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.CYAN };//每塊餅圖的顏色
DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setZoomButtonsVisible(true);//設置顯示放大縮小按鈕
renderer.setZoomEnabled(true);//設置允許放大縮小.
renderer.setChartTitleTextSize(20);//設置圖表標題的文字大小
return ChartFactory.getPieChartIntent(context, buildCategoryDataset("Project budget", values),
renderer, "Budget");//構建Intent, buildCategoryDataset是調用AbstraDemoChart的構建方法.
}
}
6. 在Android中要實現圖表統計該怎麼做
package com.yzxy.draw;
import java.util.ArrayList;
import java.util.HashMap;
import com.yzxy.draw.tools.Tools;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
@SuppressLint("ViewConstructor")
class MyTuView extends View{
public static final int RECT_SIZE = 10;
private Point mSelectedPoint = null;
public static enum Mstyle
{
Line,scroll
}
private Mstyle mstyle=Mstyle.Line;
private Point[] mPoints = new Point[8];
Context context;
Activity act;
int bheight=0;
Tools tool=new Tools();
HashMap map;
ArrayList dlk;
int totalvalue=30;
int pjvalue=5;
String xstr,ystr;
int margint=15;
int marginb=40;
int c=0;
int resid=0;
Boolean isylineshow;
public MyTuView(Context context,HashMap map,int totalvalue,int pjvalue,String xstr,String ystr,Boolean isylineshow)
{
super(context);
this.context=context;
this.act = (Activity)context;
this.map=map;
this.totalvalue=totalvalue;
this.pjvalue=pjvalue;
this.xstr=xstr;
this.ystr=ystr;
this.isylineshow=isylineshow;
act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawColor(Color.GRAY);
if(c!=0)
this.setbg(c);
if(resid!=0)
this.setBackgroundResource(resid);
dlk=tool.getintfrommap(map);
int height=getHeight();
if(bheight==0)
bheight=height-marginb;
int width=getWidth();
Log.i("w", getWidth()+":"+getHeight());
int blwidh=tool.dip2px(context,50);
int pjsize=totalvalue/pjvalue;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.GRAY);
paint.setStrokeWidth(1);
paint.setStyle(Style.STROKE);
// 畫直線(橫向)
for(int i=0;i xlist=new ArrayList();//記錄每個x的值
//畫直線(縱向)
for(int i=0;i dlk,HashMap map,ArrayList xlist,int max,int h)
{
Point[] points=new Point[dlk.size()];
for(int i=0;i getMap() {
return map;
}
public void setMap(HashMap map) {
this.map = map;
}
public int getTotalvalue() {
return totalvalue;
}
public void setTotalvalue(int totalvalue) {
this.totalvalue = totalvalue;
}
public int getPjvalue() {
return pjvalue;
}
public void setPjvalue(int pjvalue) {
this.pjvalue = pjvalue;
}
public String getXstr() {
return xstr;
}
public void setXstr(String xstr) {
this.xstr = xstr;
}
public String getYstr() {
return ystr;
}
public void setYstr(String ystr) {
this.ystr = ystr;
}
public int getMargint() {
return margint;
}
public void setMargint(int margint) {
this.margint = margint;
}
public Boolean getIsylineshow() {
return isylineshow;
}
public void setIsylineshow(Boolean isylineshow) {
this.isylineshow = isylineshow;
}
public int getMarginb() {
return marginb;
}
public void setMarginb(int marginb) {
this.marginb = marginb;
}
public Mstyle getMstyle() {
return mstyle;
}
public void setMstyle(Mstyle mstyle) {
this.mstyle = mstyle;
}
public int getBheight() {
return bheight;
}
public void setBheight(int bheight) {
this.bheight = bheight;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public int getResid() {
return resid;
}
public void setResid(int resid) {
this.resid = resid;
}
}
代碼片段
package com.yzxy.draw.tools;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
public class Tools
{
private final static String ALBUM_PATH = Environment.getExternalStorageDirectory() + "/yueqiu/";
public int dip2px(Context context, float dpValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public int px2dip(Context context, float pxValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
public ArrayList getintfrommap(HashMap map)
{
ArrayList dlk=new ArrayList();
int position=0;
@SuppressWarnings("rawtypes")
Set set= map.entrySet();
@SuppressWarnings("rawtypes")
Iterator iterator = set.iterator();
while(iterator.hasNext())
{
@SuppressWarnings("rawtypes")
Map.Entry mapentry = (Map.Entry)iterator.next();
dlk.add((Double)mapentry.getKey());
}
for(int i=0;i
代碼片段
package com.yzxy.draw;
import java.io.IOException;
import java.util.HashMap;
import com.yzxy.draw.MyTuView.Mstyle;
import com.yzxy.draw.tools.Tools;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.Color;
public class MainActivity extends Activity {
MyTuView tu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HashMap map=new HashMap();
map.put(1.0, 8.0);
map.put(7.0, 130.0);
map.put(2.0, 4.0);
map.put(3.0, 1.0);
map.put(4.0, 18.0);
map.put(5.0, 160.0);
map.put(6.0, 180.0);
map.put(8.1, 133.5);
tu=new MyTuView(this,map,200,50,"x","y",false);
// tu.setC(Color.CYAN);
tu.setResid(R.drawable.bg);
// tu.setBheight(200);
tu.setTotalvalue(200);
tu.setPjvalue(50);
tu.setXstr("x");
tu.setYstr("y");
tu.setMargint(20);
tu.setBackgroundColor(Color.WHITE);
tu.setMarginb(50);
tu.setMstyle(Mstyle.scroll);
RelativeLayout rela=getlayout(R.layout.activity_main);
rela.addView(tu);
LayoutParams parm=new LayoutParams(1200,400);
parm.setMargins(50, 50, 50, 100);
tu.setLayoutParams(parm);
setContentView(rela);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
if(item.getItemId() == R.id.menu_settings)
{
if (false == tu.isDrawingCacheEnabled())
{
tu.setDrawingCacheEnabled(true);
}
Bitmap bitmap = tu.getDrawingCache();
Tools tool=new Tools();
try {
Boolean b=tool.saveFile(bitmap, "aaaa.png");
if(b)
Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(item.getItemId() == R.id.menu_ch)
{
HashMap map=new HashMap();
map.put(1.0, 21.0);
map.put(3.0, 25.0);
map.put(4.0, 32.0);
map.put(5.0, 31.0);
map.put(6.0, 26.0);
tu.setTotalvalue(40);
tu.setPjvalue(10);
tu.setMap(map);
tu.setIsylineshow(true);
tu.postInvalidate();
}
if(item.getItemId() == R.id.menu_ch2)
{
HashMap map=new HashMap();
map.put(1.0, 41.0);
map.put(3.0, 25.0);
map.put(4.0, 32.0);
map.put(5.0, 41.0);
map.put(6.0, 16.0);
map.put(7.0, 36.0);
map.put(8.0, 26.0);
tu.setTotalvalue(50);
tu.setPjvalue(10);
tu.setMap(map);
tu.setMstyle(Mstyle.Line);
tu.setIsylineshow(false);
tu.postInvalidate();
}
return true;
}
public RelativeLayout getlayout(int r)
{
LayoutInflater inflater = (LayoutInflater)getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE );
try {
XmlResourceParser parser = getResources().getLayout(r);
RelativeLayout layout = (RelativeLayout) inflater.inflate(parser, null);
return layout;
}catch (Exception e) {
// TODO: handle exception
}
return null;
}
}