AChartEngine应用之PieChart(饼图)

AChartEngine应用之PieChart(饼图)

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

1.     设置DefaultRenderer

DefaultRenderer mRenderer = new DefaultRenderer();// PieChart的主要描绘器

mRenderer.setZoomButtonsVisible(true);// 显示放大缩小功能按钮

mRenderer.setStartAngle(180);// 设置为水平开始

mRenderer.setDisplayValues(true);// 显示数据

mRenderer.setFitLegend(true);// 设置是否显示图例

mRenderer.setLegendTextSize(10);// 设置图例字体大小

mRenderer.setLegendHeight(10);// 设置图例高度

mRenderer.setChartTitle(“饼图示例”);// 设置饼图标题

mRenderer.setChartTitleTextSize(14);// 设置饼图标题大小

 

2.     构建数据源CategorySeries

for (int i = 0; i < data.length; i++)

VALUE += data[i];

for (int i = 0; i < data.length; i++) {

mSeries.add(“示例 ” + (i + 1), data[i] / VALUE);// 设置种类名称和对应的数值,前面是(key,value)键值对

SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();

if (i < COLORS.length) {

renderer.setColor(COLORS[i]);// 设置描绘器的颜色

} else {

renderer.setColor(getRandomColor());// 设置描绘器的颜色

}

renderer.setChartValuesFormat(NumberFormat.getPercentInstance());// 设置百分比

mRenderer.setChartTitleTextSize(14);// 设置饼图标题大小

mRenderer.addSeriesRenderer(renderer);// 将最新的描绘器添加到DefaultRenderer中

}

 

3.     通过ChartFactory获取饼图

mChartView = ChartFactory.getPieChartView(getApplicationContext(),

mSeries, mRenderer);// 构建mChartView

mRenderer.setClickEnabled(true);// 允许点击事件

mChartView.setOnClickListener(new View.OnClickListener() {// 具体内容

}

运行效果图:

code

 

[java]

  1. package com.qiuzhping.achart;
  2. import java.text.NumberFormat;
  3. import java.util.Random;
  4. import org.achartengine.ChartFactory;
  5. import org.achartengine.GraphicalView;
  6. import org.achartengine.model.CategorySeries;
  7. import org.achartengine.model.SeriesSelection;
  8. import org.achartengine.renderer.DefaultRenderer;
  9. import org.achartengine.renderer.SimpleSeriesRenderer;
  10. import android.app.Activity;
  11. import android.graphics.Color;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. import android.view.ViewGroup.LayoutParams;
  15. import android.widget.LinearLayout;
  16. import android.widget.Toast;
  17. /**
  18.  * @项目名称:AChart
  19.  * @类名称:PieChartBuilder
  20.  * @作者:Qiuzhping
  21.  * @时间:2014-1-15下午11:20:48
  22.  * @作用 :构建饼图,并产生与用户交互,点击对应的区域能正确显示信息
  23.  */
  24. public class PieChartBuilder extends Activity {
  25.     private static int[] COLORS = new int[] { Color.RED, Color.GREEN,
  26.             Color.BLUE, Color.MAGENTA, Color.CYAN, Color.YELLOW, Color.DKGRAY };
  27.     double data[] = new double[] { 20, 30, 40, 50, 60, 70, 80, 90, 100 };
  28.     private CategorySeries mSeries = new CategorySeries(“”);// PieChart的DataSet
  29.                                                             // 其实就是一些键值对,跟Map使用方法差不多
  30.     private DefaultRenderer mRenderer = new DefaultRenderer();// PieChart的主要描绘器
  31.     private GraphicalView mChartView;// 用来显示PieChart 需要在配置文件Manifest中添加
  32.                                         // <activity
  33.                                         // android:name=”org.achartengine.GraphicalActivity”
  34.                                         // />
  35.     private LinearLayout mLinear;
  36.     private static double VALUE = 0;// 总数
  37.     @Override
  38.     protected void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         setContentView(R.layout.xy_chart);
  41.         mLinear = (LinearLayout) findViewById(R.id.chart);
  42.         mLinear.setBackgroundColor(Color.BLACK);
  43.         mRenderer.setZoomButtonsVisible(true);// 显示放大缩小功能按钮
  44.         mRenderer.setStartAngle(180);// 设置为水平开始
  45.         mRenderer.setDisplayValues(true);// 显示数据
  46.         mRenderer.setFitLegend(true);// 设置是否显示图例
  47.         mRenderer.setLegendTextSize(10);// 设置图例字体大小
  48.         mRenderer.setLegendHeight(10);// 设置图例高度
  49.         mRenderer.setChartTitle(“饼图示例”);// 设置饼图标题
  50.         for (int i = 0; i < data.length; i++)
  51.             VALUE += data[i];
  52.         for (int i = 0; i < data.length; i++) {
  53.             mSeries.add(“示例 ” + (i + 1), data[i] / VALUE);// 设置种类名称和对应的数值,前面是(key,value)键值对
  54.             SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
  55.             if (i < COLORS.length) {
  56.                 renderer.setColor(COLORS[i]);// 设置描绘器的颜色
  57.             } else {
  58.                 renderer.setColor(getRandomColor());// 设置描绘器的颜色
  59.             }
  60.             renderer.setChartValuesFormat(NumberFormat.getPercentInstance());// 设置百分比
  61.             mRenderer.setChartTitleTextSize(14);// 设置饼图标题大小
  62.             mRenderer.addSeriesRenderer(renderer);// 将最新的描绘器添加到DefaultRenderer中
  63.         }
  64.         if (mChartView == null) {// 为空需要从ChartFactory获取PieChartView
  65.             mChartView = ChartFactory.getPieChartView(getApplicationContext(),
  66.                     mSeries, mRenderer);// 构建mChartView
  67.             mRenderer.setClickEnabled(true);// 允许点击事件
  68.             mChartView.setOnClickListener(new View.OnClickListener() {// 具体内容
  69.                         @Override
  70.                         public void onClick(View v) {
  71.                             SeriesSelection seriesSelection = mChartView
  72.                                     .getCurrentSeriesAndPoint();// 获取当前的类别和指针
  73.                             if (seriesSelection == null) {
  74.                                 Toast.makeText(getApplicationContext(),
  75.                                         “您未选择数据”, Toast.LENGTH_SHORT).show();
  76.                             } else {
  77.                                 for (int i = 0; i < mSeries.getItemCount(); i++) {
  78.                                     mRenderer.getSeriesRendererAt(i)
  79.                                             .setHighlighted(
  80.                                                     i == seriesSelection
  81.                                                             .getPointIndex());
  82.                                 }
  83.                                 mChartView.repaint();
  84.                                 Toast.makeText(
  85.                                         getApplicationContext(),
  86.                                         “您选择的是第”
  87.                                                 + (seriesSelection
  88.                                                         .getPointIndex() + 1)
  89.                                                 + ” 项 “
  90.                                                 + ” 百分比为  “
  91.                                                 + NumberFormat
  92.                                                         .getPercentInstance()
  93.                                                         .format(seriesSelection
  94.                                                                 .getValue()),
  95.                                         Toast.LENGTH_SHORT).show();
  96.                             }
  97.                         }
  98.                     });
  99.             mLinear.addView(mChartView, new LayoutParams(
  100.                     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  101.         } else {
  102.             mChartView.repaint();
  103.         }
  104.     }
  105.     private int getRandomColor() {// 分别产生RBG数值
  106.         Random random = new Random();
  107.         int R = random.nextInt(255);
  108.         int G = random.nextInt(255);
  109.         int B = random.nextInt(255);
  110.         return Color.rgb(R, G, B);
  111.     }
  112. }

 

对应的项目源码:http://download.csdn.net/detail/qiu_11/6853469

标签