因开发需要,App中需要用到图表的绘制,参考了网上的文章和Project Hosting on Google Code(项目地址为:[url]https://code.google.com/p/achartengine/[/url]),写了一个简单的折线图的Demo.
下载下项目下的achartengine-1.0.0-demo-source.zip用于参考.
1.新建一个AbstractDemoChart抽象类和IDemoChart接口,在项目Demo中可以找到.
2.创建自己的图表显示类CustomerChart,主要功能是显示本周或者本月需要显示的信息,代码如下:
[img]http://dl.iteye.com/upload/attachment/0084/1655/c4f7baeb-32f2-37f8-b9bc-037524dc1af3.png[/img]
下载下项目下的achartengine-1.0.0-demo-source.zip用于参考.
1.新建一个AbstractDemoChart抽象类和IDemoChart接口,在项目Demo中可以找到.
2.创建自己的图表显示类CustomerChart,主要功能是显示本周或者本月需要显示的信息,代码如下:
public class CustomerChart extends AbstractDemoChart {
private List<MonitorChartData> monitor_datas;
private int chart_for;
private int mode;
private int year;
private int month;
private int day;
public CustomerChart(){
}
public CustomerChart(List<MonitorChartData> datas,int charfor,int mode,int y,int m,int d) {
this.monitor_datas = datas;
this.chart_for = charfor;
this.mode = mode;
this.year = y;
this.month = m;
this.day = d;
}
@Override
public String getName() {
return null;
}
@Override
public String getDesc() {
return null;
}
@Override
public Intent execute(Context context) {
// 如果未选择日期则默认以当前日期为准
if (this.mode == ChartUtil.MODE_MONTH || this.year == 0 || this.month == 0) {
Calendar cal = Calendar.getInstance();
setYear(cal.get(Calendar.YEAR));
setMonth(cal.get(Calendar.MONTH)+1);
setDay(cal.get(Calendar.DAY_OF_MONTH));
}
String[] titles = new String[] { "平均值" }; // 设置坐标系下方说明两天线的注释
int[] colors = new int[] { Color.YELLOW }; // 设置两条线的颜色
PointStyle[] styles = new PointStyle[] { PointStyle.DIAMOND }; // 设置线上的点的样式
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles); // 根据颜色和样式初始化坐标系渲染
List<double[]> values_y = new ArrayList<double[]>(); // y轴数据集
List<double[]> values_x = new ArrayList<double[]>(); // x轴数据集
String chart_title = ""; // 图表标题和y轴标题
renderer.setXLabels(0);
renderer.setYLabels(10);
renderer.setShowGrid(true); // 设置显示刻度
renderer.setXLabelsAlign(Align.RIGHT); // 设置x轴刻度的数值显示在x轴的下方
renderer.setYLabelsAlign(Align.RIGHT); // 设置y轴刻度的数值显示在y轴的左方
renderer.setZoomButtonsVisible(true); // 设置可以缩放
renderer.setBackgroundColor(Color.GRAY);
setColors(renderer, Color.WHITE, Color.WHITE);
// 根据按月和按周的方式设置坐标系x轴上的label
if (this.mode == ChartUtil.MODE_WEEK) {
Log.e("CustomerChart", "MODE_WEEK");
// 在x轴上显示中文的label
for (int i = 0; i < ChartUtil.WEEK_DAY.length; i++) {
renderer.addXTextLabel(i + 1, ChartUtil.WEEK_DAY[i]);
}
// 设置X轴显示范围
setXLimits(renderer, 0, 7.5);
} else if (this.mode == ChartUtil.MODE_MONTH) {
Log.e("CustomerChart", "MODE_MONTH");
int dat_count = ChartDateUtil.getMonthDaysCount(this.year, this.month); // 根据计算出来的天数规定数据集的长度
// 在x轴上显示表示天数的标识
for (int k = 1; k <= dat_count; k++) {
renderer.addXTextLabel(k, "" + k);
}
// 设置天数的显示范围
setXLimits(renderer, 0, dat_count + 1);
}
double avg_y[] = new double[monitor_datas.size()];
double avg_x[] = new double[monitor_datas.size()];
// 根据数据类型设置图表不同的渲染方式
switch (this.chart_for) {
// 空气温度
case ChartUtil.ENV_TEMPERATURE:
// 设置标题和y轴标题(常量)
chart_title = ChartUtil.ENV_TEMPERATURE_TITLE;
// 设置缩放范围
renderer.setPanLimits(ChartUtil.TEMPERATURE_LIMITES);
renderer.setZoomLimits(ChartUtil.TEMPERATURE_LIMITES);
// 根据周或者月模式取得的天数加载平均温度数据集
for (int i = 0; i < monitor_datas.size(); i++) {
avg_y[i] = this.monitor_datas.get(i).getEnv_temperature();
avg_x[i] = this.monitor_datas.get(i).getDate_info();
}
setXYTitles(renderer, chart_title, chart_title); // 设置表格的标题,y轴标题
setYLimits(renderer, -10, 45); // 设置y轴显示初始值范围
break;
// 空气湿度
case ChartUtil.ENV_HUMIDITY:
chart_title = ChartUtil.ENV_HUMIDITY_TITLE;
renderer.setPanLimits(ChartUtil.HUMIDITY_LIMITES);
renderer.setZoomLimits(ChartUtil.HUMIDITY_LIMITES);
for (int i = 0; i < monitor_datas.size(); i++) {
avg_y[i] = this.monitor_datas.get(i).getEnv_humidity();
avg_x[i] = this.monitor_datas.get(i).getDate_info();
}
setXYTitles(renderer, chart_title, chart_title);
setYLimits(renderer, -10, 45);
break;
// 土壤温度
case ChartUtil.SOIL_TEMPERATURE:
chart_title = ChartUtil.SOIL_TEMPERATURE_TITLE;
renderer.setZoomLimits(ChartUtil.HUMIDITY_LIMITES);
renderer.setPanLimits(ChartUtil.TEMPERATURE_LIMITES);
for (int i = 0; i < monitor_datas.size(); i++) {
avg_y[i] = this.monitor_datas.get(i).getSoil_temperature();
avg_x[i] = this.monitor_datas.get(i).getDate_info();
}
setXYTitles(renderer, chart_title, chart_title);
setYLimits(renderer, -10, 45);
break;
// 土壤湿度
case ChartUtil.SOIL_HUMIDITY:
chart_title = ChartUtil.SOIL_HUMIDITY_TITLE;
renderer.setPanLimits(ChartUtil.HUMIDITY_LIMITES);
for (int i = 0; i < monitor_datas.size(); i++) {
avg_y[i] = this.monitor_datas.get(i).getSoil_humidity();
avg_x[i] = this.monitor_datas.get(i).getDate_info();
}
setXYTitles(renderer, chart_title, chart_title);
break;
// 光照强度
case ChartUtil.ILLUMINATION:
chart_title = ChartUtil.ILLUMINATION_TITLE;
renderer.setPanLimits(ChartUtil.ILLUMINATION_LIMITES);
for (int i = 0; i < monitor_datas.size(); i++) {
avg_y[i] = this.monitor_datas.get(i).getIntensity_of_illumination();
avg_x[i] = this.monitor_datas.get(i).getDate_info();
}
setXYTitles(renderer, chart_title, chart_title);
break;
// 二氧化碳浓度
case ChartUtil.CO2:
chart_title = ChartUtil.CO2_TITLE;
renderer.setPanLimits(ChartUtil.CO2_LIMITES);
for (int i = 0; i < monitor_datas.size(); i++) {
avg_y[i] = this.monitor_datas.get(i).getConcentration_of_CO2();
avg_x[i] = this.monitor_datas.get(i).getDate_info();
}
setXYTitles(renderer, chart_title, chart_title);
break;
default:
break;
}
values_y.add(avg_y);
values_x.add(avg_x);
StringBuffer actTitle = new StringBuffer();
actTitle.append(chart_title);
DateRange dateRange = getBeginDate(year, month, day);
actTitle.append(" ( "+dateRange.beginDate+" 到 "+dateRange.endDate+" ) ");
Intent intent = ChartFactory
.getLineChartIntent(context, buildDataset(titles, values_x, values_y), renderer,
actTitle.toString());
return intent;
}
/**
* 设置X轴显示范围
*
* @param renderer
* @param xMin
* @param xMax
*/
private void setXLimits(XYMultipleSeriesRenderer renderer, double xMin,
double xMax) {
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
}
/**
* 设置Y轴显示范围
*
* @param renderer
* @param yMin
* @param yMax
*/
private void setYLimits(XYMultipleSeriesRenderer renderer, double yMin,
double yMax) {
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
}
/**
* 设置标题
*
* @param renderer
* @param title
* @param yTitle
*/
private void setXYTitles(XYMultipleSeriesRenderer renderer, String title,
String yTitle) {
renderer.setChartTitle(" ");
renderer.setXTitle(" ");
renderer.setYTitle(yTitle);
}
private void setColors(XYMultipleSeriesRenderer renderer, int axesColor,
int labelsColor) {
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
}
@SuppressLint("SimpleDateFormat")
private DateRange getBeginDate(int y,int m,int d){
String begin = "";
String end = "";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
if (ChartUtil.MODE_MONTH == mode) {
List<Date> temp = ChartDateUtil.getMonthByYM(y, m);
begin = df.format(temp.get(0));
end = df.format(temp.get(1));
return new DateRange(begin, end);
} else if (ChartUtil.MODE_WEEK == mode) {
List<Date> temp2 = ChartDateUtil.getWeekByDate(y, m, d);
begin = df.format(temp2.get(0));
end = df.format(temp2.get(1));
return new DateRange(begin, end);
} else {
return null;
}
}
private class DateRange {
public String beginDate;
public String endDate;
public DateRange(String begin, String end) {
this.beginDate = begin;
this.endDate = end;
}
}
public List<MonitorChartData> getMonitor_datas() {
return monitor_datas;
}
public void setMonitor_datas(List<MonitorChartData> monitor_datas) {
this.monitor_datas = monitor_datas;
}
public int getChart_for() {
return chart_for;
}
public void setChart_for(int chart_for) {
this.chart_for = chart_for;
}
public int getMode() {
return mode;
}
public void setMode(int mode) {
this.mode = mode;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
[img]http://dl.iteye.com/upload/attachment/0084/1655/c4f7baeb-32f2-37f8-b9bc-037524dc1af3.png[/img]