有几种可以利用的工具库:
- aChartEngine (http://code.google.com/p/achartengine/)
- Java4Less Charts for Android [ j4lChartAndroid.jar ]
(http://java4less.com/charts/chart.php?info=android)
- Chart4J (http://code.google.com/p/charts4j/)
- BlueChart (http://code.google.com/p/bluechart/)
- aiCharts (http://www.artfulbits.com/Android/aiCharts.aspx)
———————————————— 详细介绍 ——————————————————————————
- AChartEngine (http://code.google.com/p/achartengine/)
【 二维的。 aChartEngine的api 过于简单,支持的东西较少,但是可以进行缩放。】
aChartEngine 简称ACE ,是google为android应用设计的一个开源绘图库,它支持饼图、柱状图、折线图、
散点图、气泡图、短棒图、仪表图等多种图表。
(以下为转别人帖内容)
我想说的是不是如何去使用aChartEngine画图,而是想说说我碰到的问题。
以上是aChartEngine-6.0所有的类
![[android] <wbr>绘制统计图 利用 Android achartengine 实现统计图](http://news.open-open.com/lib/uploadImg/20120118/20120118123147_716.gif)
如果你想X轴显示时间并且要画的长方形图的话,那么麻烦来了。X轴如果想要显示时间的话,要使用TimeChart,而TimeChart继承与 LineChart,也就是说,TimeChart只能显示线性的统计图。my god!这什么东西呀,做的也太不行了吧,我顿时就恼火了。那我X轴既要显示时间,又要显示长方形图怎么办?以下是我的解决方案。
如果想要显示长方形图,我们只能采用BarChart了,所以只能在它上面动手脚。
以下是我做的例子,供碰到同样的问题的鞋子们参考:
期望功能:以长方形图显示31天内的数据信息。
001 | import java.util.Calendar; |
002 | import java.util.List; |
004 | import java.util.TreeMap; |
005 | import org.achartengine.ChartFactory; |
006 | import org.achartengine.chart.BarChart.Type; |
007 | import org.achartengine.model.CategorySeries; |
008 | import org.achartengine.model.XYMultipleSeriesDataset; |
009 | import org.achartengine.renderer.XYMultipleSeriesRenderer; |
010 | import org.achartengine.renderer.XYSeriesRenderer; |
012 | import android.content.Context; |
013 | import android.content.Intent; |
014 | import android.graphics.Color; |
016 | public class Recent31DayActivity { |
036 |
private static XYMultipleSeriesRenderer getBarDemoRenderer() { |
037 |
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); |
038 |
renderer.setAxisTitleTextSize( 12 ); |
039 |
renderer.setChartTitleTextSize( 12 ); |
040 |
renderer.setLabelsTextSize( 12 ); |
041 |
renderer.setLegendTextSize( 12 ); |
042 |
renderer.setMargins( new int [] { 20 , 30 , 15 , 0 }); |
043 |
XYSeriesRenderer r = new XYSeriesRenderer(); |
044 |
r.setColor(Color.RED); |
045 |
renderer.addSeriesRenderer(r); |
049 |
private static void setChartSettings(XYMultipleSeriesRenderer renderer) { |
050 |
renderer.setChartTitle( "最近31天" ); |
051 |
renderer.setXTitle( "时间-天" ); |
052 |
renderer.setYTitle( "发电量(Kw/h)" ); |
053 |
renderer.setYAxisMin( 0 ); |
054 |
renderer.setXAxisMin( 0.5 ); |
055 |
renderer.setXAxisMax( 12.5 ); |
056 |
renderer.setShowLegend( false ); |
057 |
renderer.setShowLabels( true ); |
058 |
renderer.setShowGrid( true ); |
059 |
renderer.setXLabels( 1 ); |
060 |
renderer.setDisplayChartValues( true ); |
061 |
renderer.setBarSpacing( 0.5 ); |
062 |
renderer.setBackgroundColor(Color.WHITE); |
065 |
private static XYMultipleSeriesDataset getBarDataset(Context cxt) { |
066 |
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); |
067 |
CategorySeries series = new CategorySeries( "最近31天" ); |
068 |
Calendar c = Calendar.getInstance(); |
069 |
int month = c.get(Calendar.DAY_OF_MONTH)+ 1 ; |
070 |
int day =c.get(Calendar.DAY_OF_MONTH); |
071 |
map.put((month< 10 ?( "0" +month):month) + "-" |
072 |
+ (day< 10 ?( "0" +day):day), 0.0 ); |
073 |
for ( int i = 0 ; i <= 30 ; i++) { |
074 |
c.add(Calendar.DAY_OF_YEAR, - 1 ); |
075 |
day=c.get(Calendar.DAY_OF_MONTH); |
076 |
month=c.get(Calendar.MONTH) + 1 ; |
077 |
map.put((month< 10 ?( "0" +month):month) + "-" |
078 |
+ (day< 10 ?( "0" +day):day), 0.0 ); |
081 |
//这里的list是我取出一个对象列表,自己可以找别的数据代替 |
082 |
if (list != null && list.size() > 0 ) { |
083 |
for ( int i = 0 ; i < list.size(); i++) { |
084 |
if (map.containsKey(key)) { |
085 |
map.put(key, min.getElectricalEnergy()); |
089 |
for (String key : map.keySet()) { |
090 |
series.add(key, Utils.getDoublePointTwo(map.get(key))); |
092 |
dataset.addSeries(series.toXYSeries()); |
096 |
public Intent Recent31DayActivity(Context context) { |
097 |
XYMultipleSeriesRenderer renderer = getBarDemoRenderer(); |
098 |
setChartSettings(renderer); |
099 |
Intent intent = ChartFactory.getBarChartIntent(context, |
100 |
getBarDataset(context), renderer, Type.DEFAULT); |
102 | //这里比较重要,这里手动给X轴填刻度。有多少条内容,你就要天多少个刻度,这样X轴就显示的是时间,也能显示出长方形图 |
103 |
for (String key : map.keySet()) { |
104 |
renderer.addTextLabel(count, key); |
![[android] <wbr>绘制统计图 利用 Android achartengine 实现统计图](http://news.open-open.com/lib/uploadImg/20120118/20120118123147_465.gif)
- Java4Less Charts for Android [ j4lChartAndroid.jar ]
(http://java4less.com/charts/chart.php?info=android)
【 二维 & 三维,比较好看,但是如果屏幕分辨率很小的时候,不能进行缩放。】
J4LChart 提供了完整的Chart图表,从饼、线、柱等都一网打尽,使用也比较简单,
Chart的式样可以从TXT文本中读取,当然,也可以在代码里修改,不过,用TXT文件读取,可以做成
模板那样,想换就换.
示例1:
- package com.sun.lwuit.uidemo;
- import com.sun.lwuit.Command;
- import com.sun.lwuit.Form;
- import com.sun.lwuit.events.ActionEvent;
- import com.sun.lwuit.events.ActionListener;
- import com.java4less.rchart.Chart;
- import com.java4less.rchart.ChartLoader;
- import com.java4less.rchart.gc.GraphicsProvider;
- import com.java4less.rchart.lwuit.ChartViewer;
- import com.java4less.rchart.util.Formats;
- import com.sun.lwuit.Display;
- import com.sun.lwuit.animations.Transition3D;
- import com.sun.lwuit.geom.Dimension;
- import com.sun.lwuit.layouts.BorderLayout;
- import java.util.Date;
- public class ChartDemo implements ActionListener {
- public Form form = new Form("J4LChart");
- Display display;
- Command nextExample= new Command("Next");
- Command previousExample= new Command("Previous");
- ChartViewer chartCanvas;
- ChartLoader loader;
- String[] examples={"areaChart.txt","areaCurves.txt","axisOnTop.txt","barChart3D.txt","barChart3D.txt","barChart.txt",
- "barImages.txt","bubbleChart.txt","candleStick.txt","combined2Positions.txt","combinedChart.txt","event.txt","linechart3D.txt",
- "linechart.txt","linechartClipping.txt","linechartNULLS.txt","MultiAxis.txt","MultiCharts.txt","patternFilling.txt",
- "piechart3D.txt","PieDrillDown.txt","radarChart.txt","realtime.txt","scatterChart.txt","stackedBar.txt","StackedMultiAxis.txt",
- "subpie1.txt","subpie2.txt","subpie3.txt","targetZones.txt"};
- int currentExample=0;
- ChartDemo()
- {
- GraphicsProvider.setMode(GraphicsProvider.MODE_LWUIT);
- form.addCommand(previousExample);//上一个
- form.addCommand(nextExample);//下一个
- form.setCommandListener(this);
- chartCanvas=new ChartViewer();//chart视图
- chartCanvas.setPreferredSize(new Dimension(200,200));//设置大小
- loadChartFromFile("/txt/"+examples[currentExample]);//读取文本,设置chart
- form.setLayout(new BorderLayout());
- form.addComponent(BorderLayout.CENTER,chartCanvas);
- form.refreshTheme();
- }
- private void createRealtimedata(ChartLoader loader) {
- java.util.Date now=java.util.Calendar.getInstance().getTime();
- loader.setParameter("XAXIS_INITIAL_DATE",Formats.formatDate(now,"dd/MM/yyyy hh:mm:ss"));
- // create random data based on the time
- java.util.Calendar cal=java.util.Calendar.getInstance();
- String data1="";
- String data2="";
- for (int i=0;i<9;i++) {
- cal.setTime(new Date(cal.getTime().getTime()-2000));
- if (data1.length()>0) data1="|" + data1;
- data1=""+cal.get(java.util.Calendar.SECOND)+ data1;
- if (data2.length()>0) data2="|" + data2;
- data2=""+(cal.get(java.util.Calendar.SECOND)/6)+ data2;
- }
- loader.setParameter("SERIE_DATA_1",data1);
- loader.setParameter("SERIE_DATA_2",data2);
- }
- public void loadChartFromFile(String filename) {
- ChartLoader cha=new ChartLoader();
- cha.loadFromFile(this.getClass().getResourceAsStream(filename),true);//读取文本
- //this.loader=cha;
- //********设置Chart数值
- //createRealtimedata(cha);
- //********设置Chart数值
- Chart c=cha.build(false,false);//建立chart
- chartCanvas.setChart(c);
- chartCanvas.getChart().setSize(chartCanvas.getWidth(),chartCanvas.getHeight());
- chartCanvas.repaintChart();
- form.setTitle(filename);
- }
- public void actionPerformed(ActionEvent arg0) {
- if(arg0.getCommand()==nextExample)//读取上一个chart(TXT文本)
- {
- chartCanvas.getChart().stopUpdater();
- currentExample++;
- if(currentExample>=examples.length-1)
- currentExample=0;
- form.replace(chartCanvas, chartCanvas,Transition3D.createRotation(500, true));
- loadChartFromFile("/txt/"+examples[currentExample]);
- }
- else if(arg0.getCommand()==previousExample)//读取下一个chart(TXT文本)
- {
- chartCanvas.getChart().stopUpdater();
- currentExample--;
- if(currentExample<=0)
- currentExample=examples.length-1;
- form.replace(chartCanvas, chartCanvas,Transition3D.createRotation(500, false));
- loadChartFromFile("/txt/"+examples[currentExample]);
- }
- }
- }
示例2:用j4lChartAndroid实现3D图饼
这几天在网上找了一些关于3D图饼的文章,做了一个小例子,废话不多说,开始讲解代码
- 先生成一个图饼对象
//new出来一个图饼的对象, 参数简介1.所占的比例 2.颜色 3.是否分组(是否种其他的块是分开的) 4.文字介绍
PieDataSerie pds = new PieDataSerie(chart_scale, chart_color, chart_group, chart_lable);
//设置label的样式
pds.valueFont = GraphicsProvider.getFont("Arial", ChartFont.PLAIN, 14);
//设置label到中心的距离
pds.textDistanceToCenter = 1.6;
2.生成一个标题对象
//设置图饼的标题
Title title = new Title("理财账户");
3.开始绘图
//开始绘图
PiePlotter pp = new PiePlotter();
//设置3D效果为true
pp.effect3D = true;
//设置边框
pp.border = new LineStyle(1,GraphicsProvider.getColor(ChartColor.BLACK),LineStyle.LINE_NORMAL);
//设置label的格式(#PERCENTAGE#,#VALUE#,#LABEL#)什么也不想显示的话直接“ ”里面有一个空格就可以了
pp.labelFormat = "#PERCENTAGE#";
//设置半径
pp.radiusModifier = 3.2;
//设置块与块之间的间隔
pp.space = 10;
//设置label到块之间的线的样式
pp.labelLine = new LineStyle(1,GraphicsProvider.getColor(ChartColor.BLACK),LineStyle.LINE_NORMAL);
4.设置图饼旁边的文字解说的样式
//生成一个对象
Legend legend = new Legend();
//因为我这里不需要解说,所以设置它为" ",中间有空格,没有空格的话,会出现多余的文字
legend.legendLabel=" ";
5.
//new一个图表对象,用来存放生成的图饼
com.java4less.rchart.Chart chart = new com.java4less.rchart.Chart(title, pp, null, null);
chart.layout = com.java4less.rchart.Chart.LAYOUT_LEGEND_TOP;
chart.back = new FillStyle(GraphicsProvider.getColor(ChartColor.WHITE));
chart.topMargin = 0.1;
chart.bottomMargin = 0.4;
chart.leftMargin =0.1;
chart.legend = legend;
chart.setHeight(200);
chart.setWidth(width);
chart.addSerie(pds);
6.
LinearLayout l = (LinearLayout) this.findViewById(R.id.chart);
ChartPanel chart = new ChartPanel(this);
chart.setChart(getChart());
l.addView(chart);
好了,就这样的成了。最后再来张图
![[android] <wbr>绘制统计图](https://i-blog.csdnimg.cn/blog_migrate/cc911c2c48ba9af60c1a9a1b587d1c16.jpeg)
原创作品,转载请注明出处:转载自道痕
- aiCharts (http://www.artfulbits.com/Android/aiCharts.aspx)
aiCharts 做的比较好,基本达到了.net中Chart控件的水平,可是要收费。用试用版的话,要带个试用的标记