① 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);