[Android]使用achartengine开发曲线图相关的Android应用程序

人走丿茶凉 2012-02-14

[Android]使用achartengine开发曲线图相关的Android应用程序[Android]使用achartengine开发曲线图相关的Android应用程序

应用程序的开发过程中,经常会遇到和曲线图打交道的情况,相比自己写代码绘制,无疑的,使用一些美观高效的开源库是一个更好的选择。目前开源的曲线图绘制工作有很多,本文以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) 

publicvoidremoveSeriesRenderer(SimpleSeriesRendererrenderer)

public SimpleSeriesRenderer getSeriesRendererAt(int index)

public void setBackgroundColor(int color) 

publicvoidsetAxesColor(intcolor)

publicvoidsetLabelsColor(intcolor)

public void setShowAxes(boolean showAxes)

public void setChartTitle(String title) 

publicvoidsetChartTitleTextSize(floattextSize)

publicvoidsetXTitle(Stringtitle)

publicvoidsetAxisTitleTextSize(floattextSize)

publicvoidsetXAxisMin(doublemin)

publicvoidsetXAxisMax(doublemax)

publicvoidsetYTitle(Stringtitle)

publicvoidsetYAxisMin(doublemin)

public void setYAxisMax(double max)

XYMultipleSeriesDataset中的一些重要方法,由名称可看出具体功能:

public void addSeries(XYSeries series) 

publicvoidremoveSeries(intindex)

publicvoidremoveSeries(XYSeriesseries)

AChartEngine的事例代码中提供了一些管理XYMultipleSeriesDataset和XYMultipleSeriesRenderer的方式,通过buildRenderer,setChartSettings,buildDataset来实现,减少了分条目设置属性的麻烦,具体见代码中的函数。

下文代码在一张图中绘制了两条曲线,并在主窗体中显示出来:

package net.ispiders;

import java.util.ArrayList; import java.util.List;

import org.achartengine.ChartFactory; 

importorg.achartengine.chart.PointStyle;

importorg.achartengine.model.XYMultipleSeriesDataset;

importorg.achartengine.model.XYSeries;

importorg.achartengine.renderer.XYMultipleSeriesRenderer;

import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity; 

importandroid.graphics.Color;

importandroid.os.Bundle;

import android.view.View;

public class chartDemo extends Activity { 

@Override

publicvoidonCreate(BundlesavedInstanceState){

        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=newPointStyle[]{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, 

ListxValues,

ListyValues)

{

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        int length = titles.length;                  //有几条线 

for(inti=0;i<length;i++)

{

XYSeriesseries=newXYSeries(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) 

{

XYMultipleSeriesRendererrenderer=newXYMultipleSeriesRenderer();

intlength=colors.length;

for(inti=0;i<length;i++)

{

XYSeriesRendererr=newXYSeriesRenderer();

r.setColor(colors[i]);

r.setPointStyle(styles[i]);

r.setFillPoints(fill);

renderer.addSeriesRenderer(r);

}

returnrenderer;

    }

    protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, 

StringxTitle,StringyTitle,doublexMin,

doublexMax,doubleyMin,doubleyMax,

intaxesColor,intlabelsColor)

{

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

}

}

相关推荐