原文链接:http://blog.csdn.net/closewbq/article/details/6589547
最近在安卓2.2上写个实时的监控程序,要用到统计图。从网上搜了下
Java4Less (http://java4less.com/charts/chart.php?info=android)
- Chart4J (http://code.google.com/p/charts4j/)
- BlueChart (http://code.google.com/p/bluechart/)- aChartEngine (http://code.google.com/p/achartengine/)
- aiCharts (http://www.artfulbits.com/Android/aiCharts.aspx)主要有以上几种。
java4Less统计的图的形状还算好看,但是如果屏幕分辨率很小的时候,不能进行缩放。
aChartEngine这个东西api太过于简单,支持的东西也太少,但是可以进行缩放。
aiCharts 这个东东做的比较好,基本达到了.net中Chart控件的水平,可是要收费。用试用版的话,还要给你带个试用的标记。(这个垃圾- -!)
最终决定用aChartEngine进行统计。
我想说的是不是如何去使用aChartEngine画图,而是想说说我碰到的问题。
以上是aChartEngine-6.0所有的类
如果你想X轴显示时间并且要画的长方形图的话,那么麻烦来了。X轴如果想要显示时间的话,要使用TimeChart,而TimeChart继承与LineChart,也就是说,TimeChart只能显示线性的统计图。my god!这什么东西呀,做的也太不行了吧,我顿时就恼火了。那我X轴既要显示时间,又要显示长方形图怎么办?以下是我的解决方案。
如果想要显示长方形图,我们只能采用BarChart了,所以只能在它上面动手脚。
以下是我做的例子,供碰到同样的问题的鞋子们参考:
期望功能:以长方形图显示31天内的数据信息。
<textarea style="DISPLAY: none" class="java" readonly="readonly" name="code">import java.util.Calendar; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.achartengine.ChartFactory; import org.achartengine.chart.BarChart.Type; import org.achartengine.model.CategorySeries; import org.achartengine.model.XYMultipleSeriesDataset; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; import android.content.Context; import android.content.Intent; import android.graphics.Color; public class Recent31DayActivity { private static Map<String, Double> map = new TreeMap<String, Double>(); private static XYMultipleSeriesRenderer getBarDemoRenderer() { XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); renderer.setAxisTitleTextSize(12); renderer.setChartTitleTextSize(12); renderer.setLabelsTextSize(12); renderer.setLegendTextSize(12); renderer.setMargins(new int[] { 20, 30, 15, 0 }); XYSeriesRenderer r = new XYSeriesRenderer(); r.setColor(Color.RED); renderer.addSeriesRenderer(r); return renderer; } private static void setChartSettings(XYMultipleSeriesRenderer renderer) { renderer.setChartTitle("最近31天"); renderer.setXTitle("时间-天"); renderer.setYTitle("发电量(Kw/h)"); renderer.setYAxisMin(0); renderer.setXAxisMin(0.5); renderer.setXAxisMax(12.5); renderer.setShowLegend(false); renderer.setShowLabels(true); renderer.setShowGrid(true); renderer.setXLabels(1); renderer.setDisplayChartValues(true); renderer.setBarSpacing(0.5); renderer.setBackgroundColor(Color.WHITE); } private static XYMultipleSeriesDataset getBarDataset(Context cxt) { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); CategorySeries series = new CategorySeries("最近31天"); Calendar c = Calendar.getInstance(); int month = c.get(Calendar.DAY_OF_MONTH)+1; int day =c.get(Calendar.DAY_OF_MONTH); map.put((month<10?("0"+month):month) + "-" + (day<10?("0"+day):day), 0.0); for (int i = 0; i <= 30; i++) { c.add(Calendar.DAY_OF_YEAR, -1); day=c.get(Calendar.DAY_OF_MONTH); month=c.get(Calendar.MONTH) + 1; map.put((month<10?("0"+month):month) + "-" + (day<10?("0"+day):day), 0.0); } </textarea>
- import java.util.Calendar;
- import java.util.List;
- import java.util.Map;
- import java.util.TreeMap;
- import org.achartengine.ChartFactory;
- import org.achartengine.chart.BarChart.Type;
- import org.achartengine.model.CategorySeries;
- import org.achartengine.model.XYMultipleSeriesDataset;
- import org.achartengine.renderer.XYMultipleSeriesRenderer;
- import org.achartengine.renderer.XYSeriesRenderer;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- public class Recent31DayActivity {
- private static Map<String, Double> map = new TreeMap<String, Double>();
- private static XYMultipleSeriesRenderer getBarDemoRenderer() {
- XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
- renderer.setAxisTitleTextSize(12);
- renderer.setChartTitleTextSize(12);
- renderer.setLabelsTextSize(12);
- renderer.setLegendTextSize(12);
- renderer.setMargins(new int[] { 20, 30, 15, 0 });
- XYSeriesRenderer r = new XYSeriesRenderer();
- r.setColor(Color.RED);
- renderer.addSeriesRenderer(r);
- return renderer;
- }
- private static void setChartSettings(XYMultipleSeriesRenderer renderer) {
- renderer.setChartTitle("最近31天");
- renderer.setXTitle("时间-天");
- renderer.setYTitle("发电量(Kw/h)");
- renderer.setYAxisMin(0);
- renderer.setXAxisMin(0.5);
- renderer.setXAxisMax(12.5);
- renderer.setShowLegend(false);
- renderer.setShowLabels(true);
- renderer.setShowGrid(true);
- renderer.setXLabels(1);
- renderer.setDisplayChartValues(true);
- renderer.setBarSpacing(0.5);
- renderer.setBackgroundColor(Color.WHITE);
- }
- private static XYMultipleSeriesDataset getBarDataset(Context cxt) {
- XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
- CategorySeries series = new CategorySeries("最近31天"); Calendar c = Calendar.getInstance();
- int month = c.get(Calendar.DAY_OF_MONTH)+1;
- int day =c.get(Calendar.DAY_OF_MONTH);
- map.put((month<10?("0"+month):month) + "-"
- + (day<10?("0"+day):day), 0.0);
- for (int i = 0; i <= 30; i++) {
- c.add(Calendar.DAY_OF_YEAR, -1);
- day=c.get(Calendar.DAY_OF_MONTH);
- month=c.get(Calendar.MONTH) + 1;
- map.put((month<10?("0"+month):month) + "-"
- + (day<10?("0"+day):day), 0.0);
- }
<textarea style="DISPLAY: none" class="java" readonly="readonly" name="code"> //这里的list是我取出一个对象列表,自己可以找别的数据代替</textarea>
- //这里的list是我取出一个对象列表,自己可以找别的数据代替
<textarea style="DISPLAY: none" class="java" readonly="readonly" name="code"> if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { if (map.containsKey(key)) { map.put(key, min.getElectricalEnergy()); } } } for (String key : map.keySet()) { series.add(key, Utils.getDoublePointTwo(map.get(key))); } dataset.addSeries(series.toXYSeries()); return dataset; } public Intent Recent31DayActivity(Context context) { XYMultipleSeriesRenderer renderer = getBarDemoRenderer(); setChartSettings(renderer); Intent intent = ChartFactory.getBarChartIntent(context, getBarDataset(context), renderer, Type.DEFAULT); int count = 1;</textarea>
- if (list != null && list.size() > 0) {
- for (int i = 0; i < list.size(); i++) {
- if (map.containsKey(key)) {
- map.put(key, min.getElectricalEnergy());
- }
- }
- }
- for (String key : map.keySet()) {
- series.add(key, Utils.getDoublePointTwo(map.get(key)));
- }
- dataset.addSeries(series.toXYSeries());
- return dataset;
- }
- public Intent Recent31DayActivity(Context context) {
- XYMultipleSeriesRenderer renderer = getBarDemoRenderer();
- setChartSettings(renderer);
- Intent intent = ChartFactory.getBarChartIntent(context,
- getBarDataset(context), renderer, Type.DEFAULT);
- int count = 1;
<textarea style="DISPLAY: none" class="java" readonly="readonly" name="code">//这里比较重要,这里手动给X轴填刻度。有多少条内容,你就要天多少个刻度,这样X轴就显示的是时间,也能显示出长方形图</textarea>
- //这里比较重要,这里手动给X轴填刻度。有多少条内容,你就要天多少个刻度,这样X轴就显示的是时间,也能显示出长方形图
<textarea style="DISPLAY: none" class="java" readonly="readonly" name="code"> for (String key : map.keySet()) { renderer.addTextLabel(count, key); count++; } return intent; } </textarea>
- for (String key : map.keySet()) {
- renderer.addTextLabel(count, key);
- count++;
- }
- return intent;
<textarea style="DISPLAY: none" class="java" readonly="readonly" name="code"><blockquote>}</blockquote></textarea>
- <blockquote>}</blockquote>
如果大家有什么不明白的话,或者对属性设置不明白的话,可以问我。不过这东西api就那么几个类,看api文档就够了。