应用程序的开发过程中,经常会遇到和曲线图打交道的情况,相比自己写代码绘制,无疑的,使用一些美观高效的开源库是一个更好的选择。目前开源的曲线图绘制工作有很多,本文以achartengine为例,介绍在Android系统下开发曲线图相关程序的有关方法。
AChartEngine是专为Android系统设计的图形库,可以用于绘制多种图表,具体列表如下,且每张图上都支持多条曲线:
line chart ; area chart ; scatter chart ; time chart ; bar chart ;
pie chart ; bubble chart ; doughnut chart ; range (high-low) bar chart ;
下面主要介绍画line chart的方法:
几个主要的类:
XYMultipleSeriesRenderer renderer; //曲线图的格式,包括颜色,值的范围,点和线的形状等等
XYMultipleSeriesDataset dataset; //保存点集数据 ,包括每条曲线的X,Y坐标
对图表而言几个重要的变量:
String[] titles; //每条曲线的名称
List<double []> x; //点集的x坐标
List<double []> y; //点集的y坐标
int[] colors; //每条曲线的颜色
PointStyle[] styles; //每条曲线点的形状
XYMultipleSeriesRenderer中的一些重要的方法,由名称可以看出具体功能:
public void addSeriesRenderer(SimpleSeriesRenderer renderer)
public void removeSeriesRenderer(SimpleSeriesRenderer renderer)
public SimpleSeriesRenderer getSeriesRendererAt(int index)
public void setBackgroundColor(int color)
public void setAxesColor(int color)
public void setLabelsColor(int color)
public void setShowAxes(boolean showAxes)
public void setChartTitle(String title)
public void setChartTitleTextSize(float textSize)
public void setXTitle(String title)
public void setAxisTitleTextSize(float textSize)
public void setXAxisMin(double min)
public void setXAxisMax(double max)
public void setYTitle(String title)
public void setYAxisMin(double min)
public void setYAxisMax(double max)
XYMultipleSeriesDataset中的一些重要方法,由名称可看出具体功能:
public void addSeries(XYSeries series)
public void removeSeries(int index)
public void removeSeries(XYSeries series)
AChartEngine的事例代码中提供了一些管理XYMultipleSeriesDataset和XYMultipleSeriesRenderer的方式,通过buildRenderer,setChartSettings,buildDataset来实现,减少了分条目设置属性的麻烦,具体见代码中的函数。
下文代码在一张图中绘制了两条曲线,并在主窗体中显示出来:
package net.ispiders;
import java.util.ArrayList;
import java.util.List;
import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
public class chartDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] titles = new String[] { "First", "Second"};
List x = new ArrayList();
List y = new ArrayList();
x.add(new double[] { 1, 3, 5, 7, 9, 11} );
x.add(new double[] { 0, 2, 4, 6, 8, 10} );
y.add(new double[] { 3, 14, 5, 30, 20, 25});
y.add(new double[] { 18, 9, 21, 15, 10, 6});
XYMultipleSeriesDataset dataset = buildDataset(titles, x, y);
int[] colors = new int[] { Color.BLUE, Color.GREEN};
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND};
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles, true);
setChartSettings(renderer, "Line Chart Demo", "X", "Y", -1, 12, 0, 35 , Color.WHITE, Color.WHITE);
View chart = ChartFactory.getLineChartView(this, dataset, renderer);
setContentView(chart);
}
protected XYMultipleSeriesDataset buildDataset(String[] titles,
List xValues,
List yValues)
{
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length; //有几条线
for (int i = 0; i < length; i++)
{
XYSeries series = new XYSeries(titles[i]); //根据每条线的名称创建
double[] xV = xValues.get(i); //获取第i条线的数据
double[] yV = yValues.get(i);
int seriesLength = xV.length; //有几个点
for (int k = 0; k < seriesLength; k++) //每条线里有几个点
{
series.add(xV[k], yV[k]);
}
dataset.addSeries(series);
}
return dataset;
}
protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles, boolean fill)
{
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
int length = colors.length;
for (int i = 0; i < length; i++)
{
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors[i]);
r.setPointStyle(styles[i]);
r.setFillPoints(fill);
renderer.addSeriesRenderer(r);
}
return renderer;
}
protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title,
String xTitle,String yTitle, double xMin,
double xMax, double yMin, double yMax,
int axesColor,int labelsColor)
{
renderer.setChartTitle(title);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
}
}
1 楼 cn23snyga 2012-02-16
请教贵博,用ACE 绘制出的图表,可以捕捉到点击事件的坐标值吗?(注:是图表中的坐标,而不是屏幕的坐标,或者有好方法可以二者换算也可以)