android混合统计图

feifei000 2014-02-12

AChartEngine高级应用之CombinedXYChart(组合统计图)在单个统计图上显示多样化的数据

现在是信息飞速发展时代,单个的统计图已经无法满足实际生产的要求,人们只想通过快速的观察就能知道很多信息,所以组合统计图就运用而生了,诸如,人们想了解一个地方的大致情况,首先可能要了解当地的天气情况,交通运输情况等,以下我模拟的是一个地方的天气、光照时间分布情况的统计图。这个是AChartEngine图表的高级应用。

构建CombinedXYChart图的步骤主要分为以下三步,还需要在项目中引入AChartEngine依赖jar包,在Manifest中添加:<activityandroid:name="org.achartengine.GraphicalActivity"/>

1.设置XYMultipleSeriesRenderer

XYMultipleSeriesRendererrenderer=newXYMultipleSeriesRenderer();//设置描绘器

renderer.setAxisTitleTextSize(16);

renderer.setChartTitleTextSize(20);

renderer.setLabelsTextSize(15);

renderer.setLegendTextSize(15);

renderer.setPointSize(5f);

renderer.setMargins(newint[]{20,30,15,20});//上,左,下,右

intlength=colors.length;

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

XYSeriesRendererr=newXYSeriesRenderer();

r.setColor(colors[i]);

r.setPointStyle(styles[i]);

renderer.addSeriesRenderer(r);

}

renderer.setXLabels(12);//X轴均分12项

renderer.setYLabels(10);//Y轴均分10项

renderer.setShowGrid(true);//显示表格

renderer.setXLabelsAlign(Align.RIGHT);//右对齐

renderer.setYLabelsAlign(Align.RIGHT);

renderer.setZoomButtonsVisible(false);//不显示放大缩小

renderer.setPanEnabled(false,false);//上下左右都不可以移动

//dataset.addSeries(0,sunSeries);

dataset.addSeries(0,waterSeries);

//renderer.addSeriesRenderer(0,lightRenderer);

renderer.addSeriesRenderer(0,waterRenderer);

2.构建数据源XYMultipleSeriesDataset

//--------------------------曲线start---------------------------------------

String[]titles=newString[]{"最低温度","最高温度","平均光照时间"};//曲线标题

List<double[]>x=newArrayList<double[]>();

for(inti=0;i<titles.length;i++){

x.add(newdouble[]{1,2,3,4,5,6,7,8,9,10,11,12});

}

List<double[]>values=newArrayList<double[]>();

values.add(newdouble[]{-12.3,-8,-2,3,16,18,26.4,26.9,18.6,

12.3,-2,-10});

values.add(newdouble[]{9,10,11,15,19,23,38,40,37,32,23,10});

values.add(newdouble[]{6.3,7.5,8.5,9.8,10.8,11.3,13.5,14.1,10.3,9.5,9.2,8});

int[]colors=newint[]{Color.GREEN,Color.RED,Color.YELLOW};//绿色,红色

PointStyle[]styles=newPointStyle[]{PointStyle.CIRCLE,//圆圈状

PointStyle.DIAMOND,PointStyle.SQUARE};//菱形状,矩形状

XYSerieswaterSeries=newXYSeries("平均温度");

waterSeries.add(1,-3);//添加数据

waterSeries.add(2,6);

waterSeries.add(3,10);

waterSeries.add(4,17);

waterSeries.add(5,20);

waterSeries.add(6,26);

waterSeries.add(7,29);

waterSeries.add(8,30);

waterSeries.add(9,23);

waterSeries.add(10,19);

waterSeries.add(11,10);

waterSeries.add(12,-1);

renderer.setBarSpacing(0.5);

XYSeriesRendererwaterRenderer=newXYSeriesRenderer();

waterRenderer.setColor(Color.CYAN);//青色

waterRenderer.setDisplayChartValues(true);//显示数值

waterRenderer.setChartValuesTextSize(10);//设置数值字体

XYMultipleSeriesDatasetdataset=buildDataset(titles,x,values);

//dataset.addSeries(0,sunSeries);

dataset.addSeries(0,waterSeries);

//renderer.addSeriesRenderer(0,lightRenderer);

renderer.addSeriesRenderer(0,waterRenderer);

3.通过ChartFactory生成CombinedXYChartView组合统计图

mViewChart=ChartFactory.getCombinedXYChartView(

getApplicationContext(),dataset,renderer,types);//通过ChartFactory生成图表

mLayout.addView(mViewChart,newLayoutParams(LayoutParams.FILL_PARENT,

LayoutParams.FILL_PARENT));//将图表添加到布局中去

效果图:

Code:

[java]viewplaincopy

packagecom.qiuzhping.achart;

importjava.util.ArrayList;

importjava.util.List;

importorg.achartengine.ChartFactory;

importorg.achartengine.GraphicalView;

importorg.achartengine.chart.BarChart;

importorg.achartengine.chart.BubbleChart;

importorg.achartengine.chart.CubicLineChart;

importorg.achartengine.chart.LineChart;

importorg.achartengine.chart.PointStyle;

importorg.achartengine.model.XYMultipleSeriesDataset;

importorg.achartengine.model.XYSeries;

importorg.achartengine.model.XYValueSeries;

importorg.achartengine.renderer.XYMultipleSeriesRenderer;

importorg.achartengine.renderer.XYSeriesRenderer;

importandroid.app.Activity;

importandroid.graphics.Color;

importandroid.graphics.Paint.Align;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.view.ViewGroup.LayoutParams;

importandroid.view.Window;

importandroid.widget.LinearLayout;

publicclassCombinedChartextendsActivity{

protectedXYMultipleSeriesRendererbuildRenderer(int[]colors,

PointStyle[]styles){

XYMultipleSeriesRendererrenderer=newXYMultipleSeriesRenderer();//设置两个描绘器

setRenderer(renderer,colors,styles);

returnrenderer;

}

protectedvoidsetChartSettings(XYMultipleSeriesRendererrenderer,

Stringtitle,StringxTitle,StringyTitle,doublexMin,

doublexMax,doubleyMin,doubleyMax,intaxesColor,

intlabelsColor){//图表样式设置

renderer.setChartTitle(title);

renderer.setXTitle(xTitle);//X轴标题

renderer.setYTitle(yTitle);//Y轴标题

renderer.setXAxisMin(xMin);//X最小值

renderer.setXAxisMax(xMax);//X最大值

renderer.setYAxisMin(yMin);//Y最小值

renderer.setYAxisMax(yMax);//Y最小值

renderer.setAxesColor(axesColor);//X轴颜色

renderer.setLabelsColor(labelsColor);//Y轴颜色

}

protectedvoidsetRenderer(XYMultipleSeriesRendererrenderer,int[]colors,

PointStyle[]styles){//设置描绘器属性

renderer.setAxisTitleTextSize(16);

renderer.setChartTitleTextSize(20);

renderer.setLabelsTextSize(15);

renderer.setLegendTextSize(15);

renderer.setPointSize(5f);

renderer.setMargins(newint[]{20,30,15,20});//上,左,下,右

intlength=colors.length;

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

XYSeriesRendererr=newXYSeriesRenderer();

r.setColor(colors[i]);

r.setPointStyle(styles[i]);

renderer.addSeriesRenderer(r);

}

}

publicvoidaddXYSeries(XYMultipleSeriesDatasetdataset,String[]titles,

List<double[]>xValues,List<double[]>yValues,intscale){//设置点集

intlength=titles.length;

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

XYSeriesseries=newXYSeries(titles[i],scale);

double[]xV=xValues.get(i);

double[]yV=yValues.get(i);

intseriesLength=xV.length;

for(intk=0;k<seriesLength;k++){

series.add(xV[k],yV[k]);

}

dataset.addSeries(series);

}

}

protectedXYMultipleSeriesDatasetbuildDataset(String[]titles,

List<double[]>xValues,List<double[]>yValues){//设置数据集

XYMultipleSeriesDatasetdataset=newXYMultipleSeriesDataset();

addXYSeries(dataset,titles,xValues,yValues,0);

returndataset;

}

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);//消除标题栏

setContentView(R.layout.xy_chart);

LinearLayoutmLinear=(LinearLayout)findViewById(R.id.chart);//获取布局

mLinear.setBackgroundColor(Color.BLACK);//设置背景色

//--------------------------曲线start---------------------------------------

String[]titles=newString[]{"最低温度","最高温度","平均光照时间"};//曲线标题

List<double[]>x=newArrayList<double[]>();

for(inti=0;i<titles.length;i++){

x.add(newdouble[]{1,2,3,4,5,6,7,8,9,10,11,12});

}

List<double[]>values=newArrayList<double[]>();

values.add(newdouble[]{-12.3,-8,-2,3,16,18,26.4,26.9,18.6,

12.3,-2,-10});

values.add(newdouble[]{9,10,11,15,19,23,38,40,37,32,23,10});

values.add(newdouble[]{6.3,7.5,8.5,9.8,10.8,11.3,13.5,14.1,10.3,9.5,9.2,8});

int[]colors=newint[]{Color.GREEN,Color.RED,Color.YELLOW};//绿色,红色

PointStyle[]styles=newPointStyle[]{PointStyle.CIRCLE,//圆圈状

PointStyle.DIAMOND,PointStyle.SQUARE};//菱形状,矩形状

//还有其他类型:X("x"),CIRCLE("circle"),TRIANGLE("triangle"),

//SQUARE("square"),DIAMOND("diamond"),POINT("point")

//----------------------------曲线end---------------------------------

XYMultipleSeriesRendererrenderer=newXYMultipleSeriesRenderer();//设置描绘器

setRenderer(renderer,colors,styles);

renderer.setPointSize(5.5f);

intlength=renderer.getSeriesRendererCount();

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

XYSeriesRendererr=(XYSeriesRenderer)renderer

.getSeriesRendererAt(i);

Log.i("length","length="+length);

r.setLineWidth(2f);//宽度

r.setFillPoints(true);//完全填充

}

setChartSettings(renderer,"气温光照分布图","月份","数量(度或小时)",0.5,12.5,-20,40,

Color.LTGRAY,Color.LTGRAY);//设置图表的X轴,Y轴,标题

renderer.setXLabels(12);//X轴均分12项

renderer.setYLabels(10);//Y轴均分10项

renderer.setShowGrid(true);//显示表格

renderer.setXLabelsAlign(Align.RIGHT);//右对齐

renderer.setYLabelsAlign(Align.RIGHT);

renderer.setZoomButtonsVisible(false);//不显示放大缩小

renderer.setPanEnabled(false,false);//上下左右都不可以移动

/*-----------------------------光照分布图start------------------------------------

XYValueSeriessunSeries=newXYValueSeries("光照时间");

sunSeries.add(1f,10,4.3);

sunSeries.add(2f,10,4.9);

sunSeries.add(3f,10,5.9);

sunSeries.add(4f,10,8.8);

sunSeries.add(5f,10,10.8);

sunSeries.add(6f,10,11.9);

sunSeries.add(7f,10,13.6);

sunSeries.add(8f,10,12.8);

sunSeries.add(9f,10,11.4);

sunSeries.add(10f,10,9.5);

sunSeries.add(11f,10,7.5);

sunSeries.add(12f,10,5.5);

XYSeriesRendererlightRenderer=newXYSeriesRenderer();

lightRenderer.setColor(Color.YELLOW);

-----------------------------光照分布图end------------------------------------*/

XYSerieswaterSeries=newXYSeries("平均温度");

waterSeries.add(1,-3);//添加数据

waterSeries.add(2,6);

waterSeries.add(3,10);

waterSeries.add(4,17);

waterSeries.add(5,20);

waterSeries.add(6,26);

waterSeries.add(7,29);

waterSeries.add(8,30);

waterSeries.add(9,23);

waterSeries.add(10,19);

waterSeries.add(11,10);

waterSeries.add(12,-1);

renderer.setBarSpacing(0.5);

XYSeriesRendererwaterRenderer=newXYSeriesRenderer();

waterRenderer.setColor(Color.CYAN);//青色

waterRenderer.setDisplayChartValues(true);//显示数值

waterRenderer.setChartValuesTextSize(10);//设置数值字体

XYMultipleSeriesDatasetdataset=buildDataset(titles,x,values);

//dataset.addSeries(0,sunSeries);

dataset.addSeries(0,waterSeries);

//renderer.addSeriesRenderer(0,lightRenderer);

renderer.addSeriesRenderer(0,waterRenderer);

String[]types=newString[]{BarChart.TYPE,LineChart.TYPE,

LineChart.TYPE,CubicLineChart.TYPE};//块状,泡沫,线性、柱形类型

//其他的还有RangeStackedBarChart区域图、BubbleChart泡沫状、ScatterChart

//散点、TimeChart时间

GraphicalViewmChartView=ChartFactory.getCombinedXYChartView(

getApplicationContext(),dataset,renderer,types);

mLinear.addView(mChartView,newLayoutParams(LayoutParams.FILL_PARENT,

LayoutParams.FILL_PARENT));

}

}

相关推荐