① android MPAndroidChart 折線圖設置哪些屬性
在org.achartengine.renderer這個包里有一個類BasicStroke,可以改變睜運行線型(實線、虛線、點線),而折線圖中用的是XYMultipleSeriesRenderer這個類悉嘩,它繼承自DefaultRenderer,這個與BasicStroke是一個包里的子類,DefaultRenderer有一個方法是setShowGridY(boolean showGrid),感覺在這個方法里肯定有設置線型的語句,但是我現在沒找到AChartEngine的具體五毛源碼,不知道這個方法具體是怎樣的,只能提供給你一個思路……悄祥我幾個月前也搗鼓過AChartEngine
② 在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;
}
}
③ 如何在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的構建方法.
}
}
④ android中數據的折線圖怎麼實現
推薦你幾個相關開源項目,可以根據需要自己選擇,自己畫還是比較麻煩的:MPAndroidChart強大的圖表繪制工具,支持折線圖、面積圖、散點圖、時間圖、柱狀圖、條圖、餅圖、氣泡圖、圓環圖、范圍(高至低)條形圖、網狀圖及各種圖的結合;支持圖的拖拽縮放;支持Android2.2以上,支持橫縱軸縮放,多指縮放,展現動畫、高亮、保存到sdcard、從文件讀取圖表achartengine強大的圖表繪制工具,支持折線圖、面積圖、散點圖、時間圖、柱狀圖、條圖、餅圖、氣泡圖、圓環圖、范圍(高至低)條形圖、撥號圖/表、立方線圖及各種圖的結合GraphView繪制圖表和曲線圖的View,可用於Android上的曲形圖、柱狀圖、波浪圖展示HoloGraphLibrary繪制線狀圖、柱狀圖、餅狀圖XCL-ChartsXCL-Charts基於原生的Canvas來繪制各種圖表,在設計時,盡量在保證開發效率的同時,給使用者提供足夠多的定製化能力。因此使用簡便,同時具有相當靈活的定製能力。目前支持3D/非3D柱形圖(BarChart)、3D/非3D餅圖(PieChart)、堆積圖(StackedBarChart)、面積圖(AreaChart)、折線圖(LineChart)、曲線圖(SplineChart)、環形圖(DountChart)、南丁格爾玫瑰圖(RoseChart)、儀表盤(DialChart)、刻度盤(GaugeChart)、雷達圖(RadarChart)、圓形圖(CircleChart)等圖表。其它特性還包括支持圖表縮放、手勢移動、動畫顯示效果、高密度柱形顯示、圖表分界定製線、多圖表的混合顯示及同數據源不同類型圖表切換等。EazeGraphAndroid圖表庫,支持柱狀圖、分層柱狀圖、餅狀圖、線性圖WilliamChart繪制圖表的庫,支持LineChartView、BarChartView和StackBarChartView三中圖表類型,並且支持Android2.2及以上的系統。HelloChartsforAndroid支持折線圖、柱狀圖、餅圖、氣泡圖、組合圖;支持預覽、放大縮小,滾動,部分圖表支持動畫;支持Android2.2以上這些項目都可以在GitHub搜索到。
⑤ android折線圖
packagecom.example.testcanvasdraw;importjava.util.ArrayList;importjava.util.List;importjava.util.Random;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.os.Handler;importandroid.os.Message;importandroid.util.AttributeSet;importandroid.view.View;publicclassMyViewextendsView{privateintXPoint=60;privateintYPoint=260;privateintXScale=8;//刻度長度privateintYScale=40;privateintXLength=380;privateintYLength=240;privateintMaxDataSize=XLength/XScale;privateList<Integer>data=newArrayList<Integer>();privateString[]YLabel=newString[YLength/YScale];privateHandlerhandler=newHandler(){publicvoidhandleMessage(Messagemsg){if(msg.what==0x1234){MyView.this.invalidate();}};};publicMyView(Contextcontext,AttributeSetattrs){super(context,attrs);for(inti=0;i<YLabel.length;i++){YLabel[i]=(i+1)+"M/s";}newThread(newRunnable(){@Overridepublicvoidrun(){while(true){try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}if(data.size()>=MaxDataSize){data.remove(0);}data.add(newRandom().nextInt(4)+1);handler.sendEmptyMessage(0x1234);}}}).start();}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);Paintpaint=newPaint();paint.setStyle(Paint.Style.STROKE);paint.setAntiAlias(true);//去鋸齒paint.setColor(Color.BLUE);//畫Y軸canvas.drawLine(XPoint,YPoint-YLength,XPoint,YPoint,paint);//Y軸箭頭canvas.drawLine(XPoint,YPoint-YLength,XPoint-3,YPoint-YLength+6,paint);//箭頭canvas.drawLine(XPoint,YPoint-YLength,XPoint+3,YPoint-YLength+6,paint);//添加刻度和文字for(inti=0;i*YScale<YLength;i++){canvas.drawLine(XPoint,YPoint-i*YScale,XPoint+5,YPoint-i*YScale,paint);//刻度canvas.drawText(YLabel[i],XPoint-50,YPoint-i*YScale,paint);//文字}//畫X軸canvas.drawLine(XPoint,YPoint,XPoint+XLength,YPoint,paint);System.out.println("Data.size="+data.size());if(data.size()>1){for(inti=1;i<data.size();i++){canvas.drawLine(XPoint+(i-1)*XScale,YPoint-data.get(i-1)*YScale,XPoint+i*XScale,YPoint-data.get(i)*YScale,paint);}}}}
上面繪制折線部分我們還有一種方式同樣可以實現:
if(data.size()>1){Pathpath=newPath();path.moveTo(XPoint,YPoint-data.get(0)*YScale);for(inti=1;i<data.size();i++){path.lineTo(XPoint+i*XScale,YPoint-data.get(i)*YScale);}canvas.drawPath(path,paint);}
⑥ android編程中遇到問題,如何用achartengine繪制X軸上數值有反復的折線圖例如下圖
1、onListItemClick方法
當點擊「柱狀圖」菜單時,使用 ChartFactory.getLineChrtIntent獲得Intdent:
Intent intent = ChartFactory.getLineChartIntent(this, getDataset(),getRenderer());
2、getDataset方法
使用隨機數構建了2個系列中的點坐標x和y:
int x=r.nextInt()%10;// x:0-10之間的隨機整數
inty=50+r.nextInt()%50;// y:50-100之間的隨機整數
series.add(x,y);// 往系列中加入一個隨機分布的點
由於使用隨機數作為點的x、y坐標,所以形成的折線圖是很不規律的
3、getRenderer方法
構建了XYMultipleSeriesRenderer,在其中加入了兩個Series。注意,這里的Series要用XYSeriesRenderer ,而不能使用 SimpleSeriesRenderer。
⑦ android怎麼讓折線圖上方顯示數字
直接在旁邊寫數字就可以了。主要畫折線圖。
步驟一:折線圖的坐標集
首先應該創建 XYSeries對象 : 用於提供繪制的點集合的數據
XYSeries series = new XYSeries(titles[i]); // 根據每條線的名稱創建
將所有的坐標加入這個集合
series .add (xV[k], yV[k]);
XYMultipleSeriesDataset對象 用於 保存點集數據 ,包括每條曲線的X,Y坐標
// 用於數據的存放
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset .addSeries (series);
步驟二:折線圖的樣式集
定義每條曲線的顏色
int [] colors = new int [] {Color. BLUE ,Color. GREEN };
定義每條曲線的點的形狀
PointStyle [] styles =
new PointStyle []{ PointStyle . CIRCLE , PointStyle . DIAMOND };
曲線圖的格式,包括顏色,值的范圍,點和線的形狀等等 都封裝在 XYSeriesRender對象中,再將XYSeriesRender對象封裝在 XYMultipleSeriesRenderer 對象中
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors[i]);
r.setPointStyle(styles[i]);
r.setFillPoints(fill);
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.addSeriesRenderer(r);
設置折線圖名稱,坐標軸的名稱,坐標軸的起點重點,坐標軸的顏色,坐標軸上數字的顏色
setChartSettings(renderer, "Line Chart Demo" , "X" , "Y" , - 1 , 12, 0, 35 , Color. WHITE , Color. WHITE );
步驟三:顯示折線圖
View chart = ChartFactory. getLineChartView ( this , dataset, renderer);
setContentView(chart);