由于这次做的项目需要用到图形报表,找了半天,发现有个第三方的组件AChartEngine,就写了3个常用的饼图,状图,折线,供大家参考


按国际惯例,先上截图,再上代码片段


Android 表单界面_移动开发

package com.lee;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.lee.modle.Information;
/**
* 
* @author 
* Patata丶
* <a href='\"http://www.weibo.com/totome\"' target='\"_blank\"'>http://www.weibo.com/totome</a>
*
*/
public class MainAct extends Activity {
ArrayList<Information>informations;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
informations = new ArrayList<Information>();
//数据假设 初始化
informations.add(new Information("福建", "7434832", "人"));
informations.add(new Information("广东", "9434832", "人"));
informations.add(new Information("浙江", "5434832", "人"));
informations.add(new Information("江苏", "7436832", "人"));
informations.add(new Information("香港", "7464832", "人"));
informations.add(new Information("陕西", "6464832", "人"));
informations.add(new Information("上海", "4534832", "人"));
informations.add(new Information("北京", "9435832", "人"));
 
}
 
/**
* 按键监听
*/
public void bing(View view)
{
Intent intent = new Intent(this, Pie_char.class);
intent.putExtra("informations", informations);
intent.putExtra("title", "人口数据");
startActivity(intent);
}
public void zhu(View view)
{
Intent intent = new Intent(this, Stacked.class);
intent.putExtra("informations", informations);
intent.putExtra("title", "人口数据");
startActivity(intent);
}
public void zhexian(View view)
{
Intent intent = new Intent(this, Zhexian.class);
intent.putExtra("informations", informations);
intent.putExtra("title", "人口数据");
startActivity(intent);
}
 
 
}
package com.lee;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.lee.modle.Information;
/**
* 
* @author 
* Patata丶
* <a href='\"http://www.weibo.com/totome\"' target='\"_blank\"'>http://www.weibo.com/totome</a>
*
*/
public class MainAct extends Activity {
ArrayList<Information>informations;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
informations = new ArrayList<Information>();
//数据假设 初始化
informations.add(new Information("福建", "7434832", "人"));
informations.add(new Information("广东", "9434832", "人"));
informations.add(new Information("浙江", "5434832", "人"));
informations.add(new Information("江苏", "7436832", "人"));
informations.add(new Information("香港", "7464832", "人"));
informations.add(new Information("陕西", "6464832", "人"));
informations.add(new Information("上海", "4534832", "人"));
informations.add(new Information("北京", "9435832", "人"));
 
}
 
/**
* 按键监听
*/
public void bing(View view)
{
Intent intent = new Intent(this, Pie_char.class);
intent.putExtra("informations", informations);
intent.putExtra("title", "人口数据");
startActivity(intent);
}
public void zhu(View view)
{
Intent intent = new Intent(this, Stacked.class);
intent.putExtra("informations", informations);
intent.putExtra("title", "人口数据");
startActivity(intent);
}
public void zhexian(View view)
{
Intent intent = new Intent(this, Zhexian.class);
intent.putExtra("informations", informations);
intent.putExtra("title", "人口数据");
startActivity(intent);
}
 
 
}
package com.lee;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import com.lee.modle.Information;
 
 
import android.R.integer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
/**
* 饼图
* * Patata丶
* <a href='\"http://www.weibo.com/totome\"' target='\"_blank\"'>http://www.weibo.com/totome</a>
*
*/
public class Pie_char extends Activity {
private ArrayList<Information> list;
private int[] colorsArr;
private String title;
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
 
DefaultRenderer renderer = buildCategoryRenderer(colorsArr); //把分布的颜色传给渲染器
renderer.setZoomButtonsVisible(true);
renderer.setZoomEnabled(true);
renderer.setChartTitleTextSize(20);
renderer.setInScroll(true);
// View view = ChartFactory.getPieChartView(this, buildCategoryDataset("Project budget", values), renderer);
//饼状图文字信息和对应的百分比
View view = ChartFactory.getPieChartView(this, buildCategoryDataset("Project budget", list), renderer);
view.setBackgroundColor(Color.BLACK);
 
setContentView(view);
 
}
/**
* 初始化数据
*/
private void initData()
{
//获取main传来的集合
list = (ArrayList<Information>) getIntent().getSerializableExtra("informations");
title = getIntent().getStringExtra("title"); //设置标题
//把数据所需颜色复制到数组
colorsArr = new int[list.size()];
for (int i = 0; i < list.size(); i++)
{
colorsArr<i> = IConstant.colorLib<i>;
}
}
/**
* 把分布的颜色传给渲染器
* @param colors
* @return
*/
protected DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] { 20, 30, 15, 0 });
renderer.setChartTitle(title);
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}
 
/**
* 饼状图文字信息
* @param title
* @param values
* @return
*/
protected CategorySeries buildCategoryDataset(String title, ArrayList<Information> list) {
CategorySeries series = new CategorySeries(title);
//根据list值分配视图 颜色
for (Information information : list)
{
double num = Double.parseDouble(information.dulian1);
series.add(information.weidu+ " (" + information.dulian1 + information.dw +")" , num);
} 
return series;
}
}</i></i>
package com.lee;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import com.lee.modle.Information;
 
 
import android.R.integer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
/**
* 饼图
* * Patata丶
* <a href='\"http://www.weibo.com/totome\"' target='\"_blank\"'>http://www.weibo.com/totome</a>
*
*/
public class Pie_char extends Activity {
private ArrayList<Information> list;
private int[] colorsArr;
private String title;
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
 
DefaultRenderer renderer = buildCategoryRenderer(colorsArr); //把分布的颜色传给渲染器
renderer.setZoomButtonsVisible(true);
renderer.setZoomEnabled(true);
renderer.setChartTitleTextSize(20);
renderer.setInScroll(true);
// View view = ChartFactory.getPieChartView(this, buildCategoryDataset("Project budget", values), renderer);
//饼状图文字信息和对应的百分比
View view = ChartFactory.getPieChartView(this, buildCategoryDataset("Project budget", list), renderer);
view.setBackgroundColor(Color.BLACK);
 
setContentView(view);
 
}
/**
* 初始化数据
*/
private void initData()
{
//获取main传来的集合
list = (ArrayList<Information>) getIntent().getSerializableExtra("informations");
title = getIntent().getStringExtra("title"); //设置标题
//把数据所需颜色复制到数组
colorsArr = new int[list.size()];
for (int i = 0; i < list.size(); i++)
{
colorsArr<i> = IConstant.colorLib<i>;
}
}
/**
* 把分布的颜色传给渲染器
* @param colors
* @return
*/
protected DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] { 20, 30, 15, 0 });
renderer.setChartTitle(title);
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}
 
/**
* 饼状图文字信息
* @param title
* @param values
* @return
*/
protected CategorySeries buildCategoryDataset(String title, ArrayList<Information> list) {
CategorySeries series = new CategorySeries(title);
//根据list值分配视图 颜色
for (Information information : list)
{
double num = Double.parseDouble(information.dulian1);
series.add(information.weidu+ " (" + information.dulian1 + information.dw +")" , num);
} 
return series;
}
}</i></i>
package com.lee;
import java.util.ArrayList;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import com.lee.modle.Information;
/**
* 柱状图
* Patata丶
* <a href='\"http://www.weibo.com/totome\"' target='\"_blank\"'>http://www.weibo.com/totome</a>
*
*/
public class Stacked extends Activity
{
private ArrayList<Information> list;
private String title;
private double maxValue = 0; //数据的最大值
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initCompont();
View view = ChartFactory.getBarChartView(this, getBarDataset(list), getBarRenderer(list), Type.STACKED); //Type.STACKED
view.setBackgroundColor(Color.BLACK);
setContentView(view);
 
}
/**
* 初始化数据
* @param titles
* @return
*/
private void initCompont()
{
list = (ArrayList<Information>) getIntent().getSerializableExtra("informations");
title = getIntent().getStringExtra("title");
//算出所有数据的最大值
for (Information information : list)
{
double value = Double.parseDouble(information.dulian1);
if (value > maxValue)
{
maxValue = value;
} 
}
maxValue = maxValue + (maxValue/8); //让左边刻度线高出数字的最大值 
}
// 描绘器设置
public XYMultipleSeriesRenderer getBarRenderer(ArrayList<Information> informations)
{
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
// 通过SimpleSeriesDenderer设置描绘器的颜色
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(Color.rgb(1, 128, 205)); //定义柱状图的颜色
renderer.addSeriesRenderer(r);
 
setChartSettings(renderer, informations);// 设置描绘器的其他属性
return renderer;
}
private void setChartSettings(XYMultipleSeriesRenderer renderer, ArrayList<Information> informations )
{
//  renderer.setChartTitle("个人收支表");// 设置柱图名称
//  renderer.setXTitle("名单");// 设置X轴名称
//  renderer.setYTitle("数量");// 设置Y轴名称
renderer.setXAxisMin(0.5);// 设置X轴的最小值为0.5
renderer.setXAxisMax(5.5);// 设置X轴的最大值为5
renderer.setYAxisMin(0);// 设置Y轴的最小值为0
renderer.setYAxisMax(maxValue);// 设置Y轴最大值为500
renderer.setDisplayChartValues(true); // 设置是否在柱体上方显示值
renderer.setShowGrid(true);// 设置是否在图表中显示网格
renderer.setXLabels(0);// 设置X轴显示的刻度标签的个数
renderer.setBarSpacing(0.2); //柱状间的间隔
renderer.setZoomButtonsVisible(true);
//为X轴的每个柱状图设置底下的标题 比如 福建 ,广东.....
int count=1; 
for (Information information : informations)
{
renderer.addXTextLabel(count,information.weidu); 
count++; 
} 
 
 
}
// 数据设置
private XYMultipleSeriesDataset getBarDataset(ArrayList<Information> informations)
{
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
CategorySeries series = new CategorySeries(title + " (单位:" + informations.get(0).dw + ")");
// 声明一个柱形图
// 为柱形图添加值
for (Information information : informations)
{
series.add(Double.parseDouble(information.dulian1));
}
dataset.addSeries(series.toXYSeries());// 添加该柱形图到数据设置列表
 
 
return dataset;
}
}
package com.lee;
import java.util.ArrayList;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import com.lee.modle.Information;
/**
* 柱状图
* Patata丶
* <a href='\"http://www.weibo.com/totome\"' target='\"_blank\"'>http://www.weibo.com/totome</a>
*
*/
public class Stacked extends Activity
{
private ArrayList<Information> list;
private String title;
private double maxValue = 0; //数据的最大值
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initCompont();
View view = ChartFactory.getBarChartView(this, getBarDataset(list), getBarRenderer(list), Type.STACKED); //Type.STACKED
view.setBackgroundColor(Color.BLACK);
setContentView(view);
 
}
/**
* 初始化数据
* @param titles
* @return
*/
private void initCompont()
{
list = (ArrayList<Information>) getIntent().getSerializableExtra("informations");
title = getIntent().getStringExtra("title");
//算出所有数据的最大值
for (Information information : list)
{
double value = Double.parseDouble(information.dulian1);
if (value > maxValue)
{
maxValue = value;
} 
}
maxValue = maxValue + (maxValue/8); //让左边刻度线高出数字的最大值 
}
// 描绘器设置
public XYMultipleSeriesRenderer getBarRenderer(ArrayList<Information> informations)
{
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
// 通过SimpleSeriesDenderer设置描绘器的颜色
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(Color.rgb(1, 128, 205)); //定义柱状图的颜色
renderer.addSeriesRenderer(r);
 
setChartSettings(renderer, informations);// 设置描绘器的其他属性
return renderer;
}
private void setChartSettings(XYMultipleSeriesRenderer renderer, ArrayList<Information> informations )
{
//  renderer.setChartTitle("个人收支表");// 设置柱图名称
//  renderer.setXTitle("名单");// 设置X轴名称
//  renderer.setYTitle("数量");// 设置Y轴名称
renderer.setXAxisMin(0.5);// 设置X轴的最小值为0.5
renderer.setXAxisMax(5.5);// 设置X轴的最大值为5
renderer.setYAxisMin(0);// 设置Y轴的最小值为0
renderer.setYAxisMax(maxValue);// 设置Y轴最大值为500
renderer.setDisplayChartValues(true); // 设置是否在柱体上方显示值
renderer.setShowGrid(true);// 设置是否在图表中显示网格
renderer.setXLabels(0);// 设置X轴显示的刻度标签的个数
renderer.setBarSpacing(0.2); //柱状间的间隔
renderer.setZoomButtonsVisible(true);
//为X轴的每个柱状图设置底下的标题 比如 福建 ,广东.....
int count=1; 
for (Information information : informations)
{
renderer.addXTextLabel(count,information.weidu); 
count++; 
} 
 
 
}
// 数据设置
private XYMultipleSeriesDataset getBarDataset(ArrayList<Information> informations)
{
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
CategorySeries series = new CategorySeries(title + " (单位:" + informations.get(0).dw + ")");
// 声明一个柱形图
// 为柱形图添加值
for (Information information : informations)
{
series.add(Double.parseDouble(information.dulian1));
}
dataset.addSeries(series.toXYSeries());// 添加该柱形图到数据设置列表
 
 
return dataset;
}
}
package com.lee;
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 com.lee.modle.Information;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
/**
* 折线图
* Patata丶
* <a href='\"http://www.weibo.com/totome\"' target='\"_blank\"'>http://www.weibo.com/totome</a>
*
*/
public class Zhexian extends Activity
{
private ArrayList<Information> list;
private String title;
private double maxValue = 0; //数据的最大值
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initCompont(); //初始化组件
String[] titles = new String[] {title}; //表示目前就一条折线信息
//List有几条数据
List<double[]> x = new ArrayList<double[]>();
//X轴所显示的数目 1,2,3,4,5.... 1条数据有几个点信息
double[] xInfo = new double[list.size()];
int count = 1;
for (int i = 0; i < list.size(); i++)
{
xInfo<i> = count;
count++;
}
 
//折线数量 1
for (int i = 0; i < titles.length; i++) {
x.add(xInfo);
}
//折线各个点的值
double[] xValue = new double[list.size()];
for (int i = 0; i < xValue.length; i++)
{
xValue<i> = Double.parseDouble(list.get(i).dulian1);
//把数据最大值赋给maxValue
if (xValue<i> > maxValue)
{
maxValue = xValue<i>;
}
}
maxValue = maxValue + (maxValue/8);
 
List<double[]> values = new ArrayList<double[]>();
values.add(xValue);
int[] colors = new int[] { Color.CYAN};//折线的颜色
PointStyle[] styles = new PointStyle[] { PointStyle.DIAMOND}; //折线的样式
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
}
setChartSettings(renderer, title, "", "", 0.5, 12.5, -10, 40, Color.LTGRAY, Color.LTGRAY);
renderer.setZoomButtonsVisible(true);
View view = ChartFactory.getLineChartView(this, buildDataset(titles, x, values), renderer);
view.setBackgroundColor(Color.BLACK);
setContentView(view);
}
 
/**
* 初始化数据
* @param titles
* @return
*/
private void initCompont()
{
list = (ArrayList<Information>) getIntent().getSerializableExtra("informations");
title = getIntent().getStringExtra("title");
title = title + " (单位:" + list.get(0).dw + ")"; //处理null字符串
}
 
private XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
setRenderer(renderer, colors, styles);
return renderer;
}
private void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(12); //标注字
renderer.setLegendTextSize(15);
renderer.setPointSize(5f);
renderer.setMargins(new int[] { 20, 30, 15, 20 });
int length = colors.length;
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors<i>);
r.setPointStyle(styles<i>);
renderer.addSeriesRenderer(r);
}
}
private void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) {
renderer.setYTitle("数量");// 设置Y轴名称
renderer.setXAxisMin(0.5);// 设置X轴的最小值为0.5
renderer.setXAxisMax(5.5);// 设置X轴的最大值为5
renderer.setYAxisMin(0);// 设置Y轴的最小值为0
renderer.setYAxisMax(maxValue);// 设置Y轴最大值为500
renderer.setDisplayChartValues(true); // 设置是否在柱体上方显示值
renderer.setShowGrid(true);// 设置是否在图表中显示网格
renderer.setXLabels(0);// 设置X轴显示的刻度标签的个数
renderer.setBarSpacing(1);
renderer.setXLabels(0);// 设置X轴显示的刻度标签的个数
int tempNum = 1;
for (Information information : list)
{
renderer.addXTextLabel(tempNum,information.weidu); 
tempNum++;
}
}
private XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues, List<double[]> yValues) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
addXYSeries(dataset, titles, xValues, yValues, 0);
return dataset;
}
private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues, List<double[]> yValues, int scale) {
int length = titles.length;
for (int i = 0; i < length; i++) {
XYSeries series = new XYSeries(titles<i>, scale);
double[] xV = xValues.get(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);
}
}
}
</i></i></i></i></i></i></i>
package com.lee;
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 com.lee.modle.Information;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
/**
* 折线图
* Patata丶
* <a href='\"http://www.weibo.com/totome\"' target='\"_blank\"'>http://www.weibo.com/totome</a>
*
*/
public class Zhexian extends Activity
{
private ArrayList<Information> list;
private String title;
private double maxValue = 0; //数据的最大值
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initCompont(); //初始化组件
String[] titles = new String[] {title}; //表示目前就一条折线信息
//List有几条数据
List<double[]> x = new ArrayList<double[]>();
//X轴所显示的数目 1,2,3,4,5.... 1条数据有几个点信息
double[] xInfo = new double[list.size()];
int count = 1;
for (int i = 0; i < list.size(); i++)
{
xInfo<i> = count;
count++;
}
 
//折线数量 1
for (int i = 0; i < titles.length; i++) {
x.add(xInfo);
}
//折线各个点的值
double[] xValue = new double[list.size()];
for (int i = 0; i < xValue.length; i++)
{
xValue<i> = Double.parseDouble(list.get(i).dulian1);
//把数据最大值赋给maxValue
if (xValue<i> > maxValue)
{
maxValue = xValue<i>;
}
}
maxValue = maxValue + (maxValue/8);
 
List<double[]> values = new ArrayList<double[]>();
values.add(xValue);
int[] colors = new int[] { Color.CYAN};//折线的颜色
PointStyle[] styles = new PointStyle[] { PointStyle.DIAMOND}; //折线的样式
XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
}
setChartSettings(renderer, title, "", "", 0.5, 12.5, -10, 40, Color.LTGRAY, Color.LTGRAY);
renderer.setZoomButtonsVisible(true);
View view = ChartFactory.getLineChartView(this, buildDataset(titles, x, values), renderer);
view.setBackgroundColor(Color.BLACK);
setContentView(view);
}
 
/**
* 初始化数据
* @param titles
* @return
*/
private void initCompont()
{
list = (ArrayList<Information>) getIntent().getSerializableExtra("informations");
title = getIntent().getStringExtra("title");
title = title + " (单位:" + list.get(0).dw + ")"; //处理null字符串
}
 
private XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
setRenderer(renderer, colors, styles);
return renderer;
}
private void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(12); //标注字
renderer.setLegendTextSize(15);
renderer.setPointSize(5f);
renderer.setMargins(new int[] { 20, 30, 15, 20 });
int length = colors.length;
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors<i>);
r.setPointStyle(styles<i>);
renderer.addSeriesRenderer(r);
}
}
private void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) {
renderer.setYTitle("数量");// 设置Y轴名称
renderer.setXAxisMin(0.5);// 设置X轴的最小值为0.5
renderer.setXAxisMax(5.5);// 设置X轴的最大值为5
renderer.setYAxisMin(0);// 设置Y轴的最小值为0
renderer.setYAxisMax(maxValue);// 设置Y轴最大值为500
renderer.setDisplayChartValues(true); // 设置是否在柱体上方显示值
renderer.setShowGrid(true);// 设置是否在图表中显示网格
renderer.setXLabels(0);// 设置X轴显示的刻度标签的个数
renderer.setBarSpacing(1);
renderer.setXLabels(0);// 设置X轴显示的刻度标签的个数
int tempNum = 1;
for (Information information : list)
{
renderer.addXTextLabel(tempNum,information.weidu); 
tempNum++;
}
}
private XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues, List<double[]> yValues) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
addXYSeries(dataset, titles, xValues, yValues, 0);
return dataset;
}
private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues, List<double[]> yValues, int scale) {
int length = titles.length;
for (int i = 0; i < length; i++) {
XYSeries series = new XYSeries(titles<i>, scale);
double[] xV = xValues.get(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);
}
}
}
</i></i></i></i></i></i></i>
package com.lee.modle;
import java.io.Serializable;
 
public class Information implements Serializable
{
public String weidu; //名称
public String dulian1; //数据
public String bfb; //百分比
public String dw; //单位
 
 
public Information(String weidu, String dulian1, String dw)
{
super();
this.weidu = weidu;
this.dulian1 = dulian1;
this.dw = dw;
}
 
 
public Information ()
{
}
}
package com.lee.modle;
import java.io.Serializable;
 
public class Information implements Serializable
{
public String weidu; //名称
public String dulian1; //数据
public String bfb; //百分比
public String dw; //单位
 
 
public Information(String weidu, String dulian1, String dw)
{
super();
this.weidu = weidu;
this.dulian1 = dulian1;
this.dw = dw;
}
 
 
public Information ()
{
}
}
源码demo地址:http://www.kuaipan.cn/file/id_32645732884308999.htm
源码demo地址:http://www.kuaipan.cn/file/id_32645732884308999.htm