当前位置:   article > 正文

android achartengine 生成柱状图(支持修改背景)

android achartengine 生成柱状图(支持修改背景)
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="#ffffffff"
  6. android:orientation="vertical" >
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content" >
  10. <LinearLayout
  11. android:layout_width="0dip"
  12. android:layout_height="wrap_content"
  13. android:layout_weight="1" >
  14. <Button
  15. android:id="@+id/single_view_btn"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:text="单图" />
  19. </LinearLayout>
  20. <LinearLayout
  21. android:layout_width="0dip"
  22. android:layout_height="wrap_content"
  23. android:layout_weight="1" >
  24. <Button
  25. android:id="@+id/double_view_btn"
  26. android:layout_width="fill_parent"
  27. android:layout_height="wrap_content"
  28. android:text="双图" />
  29. </LinearLayout>
  30. </LinearLayout>
  31. <LinearLayout
  32. android:id="@+id/barview_content"
  33. android:layout_width="fill_parent"
  34. android:layout_height="fill_parent" >
  35. </LinearLayout>
  36. </LinearLayout>

  1. package cn.knight.barchart;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.achartengine.model.CategorySeries;
  5. import org.achartengine.model.XYMultipleSeriesDataset;
  6. import org.achartengine.renderer.SimpleSeriesRenderer;
  7. import org.achartengine.renderer.XYMultipleSeriesRenderer;
  8. import android.app.Activity;
  9. import android.graphics.Color;
  10. import android.os.Bundle;
  11. import android.support.v4.view.ViewPager.LayoutParams;
  12. import android.view.KeyEvent;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.LinearLayout;
  17. import android.widget.TextView;
  18. public class Main extends Activity implements OnClickListener {
  19. public static String LABLE_TEXT[] = { "", "A", "B", "C", "D" };
  20. private LinearLayout layoutViewContent;
  21. private Button btn_single;
  22. private Button btn_total;
  23. private double first[] = new double[] { 30, 40, 60, 80 };
  24. private double second[] = new double[] { 50, 60, 88, 91 };
  25. private List<String> options = new ArrayList<String>();
  26. private boolean isSingleView;
  27. private BarChartView view;
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. isSingleView = false;
  33. for (String tem : LABLE_TEXT) {
  34. options.add(tem);
  35. }
  36. layoutViewContent = (LinearLayout) findViewById(R.id.barview_content);
  37. view = new BarChartView(Main.this, isSingleView);
  38. view.initData(first, second, options, "双柱状图");
  39. layoutViewContent.setBackgroundColor(0xffffffff);
  40. layoutViewContent.addView(view.getBarChartView());
  41. btn_single = (Button) findViewById(R.id.single_view_btn);
  42. btn_total = (Button) findViewById(R.id.double_view_btn);
  43. btn_single.setOnClickListener(this);
  44. btn_total.setOnClickListener(this);
  45. }
  46. @Override
  47. public void onClick(View v) {
  48. int id = v.getId();
  49. switch (id) {
  50. case R.id.single_view_btn:
  51. isSingleView = true;
  52. layoutViewContent.removeAllViews();
  53. view = new BarChartView(Main.this, isSingleView);
  54. view.initData(first, second, options, "单柱状图");
  55. layoutViewContent.addView(view.getBarChartView());
  56. break;
  57. case R.id.double_view_btn:
  58. isSingleView = false;
  59. layoutViewContent.removeAllViews();
  60. view = new BarChartView(Main.this, isSingleView);
  61. view.initData(first, second, options, "双柱状图");
  62. layoutViewContent.addView(view.getBarChartView());
  63. break;
  64. }
  65. }
  66. }

  1. package cn.knight.barchart;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.achartengine.ChartFactory;
  5. import org.achartengine.chart.BarChart.Type;
  6. import org.achartengine.model.CategorySeries;
  7. import org.achartengine.model.XYMultipleSeriesDataset;
  8. import org.achartengine.renderer.SimpleSeriesRenderer;
  9. import org.achartengine.renderer.XYMultipleSeriesRenderer;
  10. import android.content.Context;
  11. import android.graphics.Color;
  12. import android.graphics.Paint.Align;
  13. import android.view.View;
  14. public class BarChartView {
  15. private static int margins[] = new int[] { 70, 70, 70, 70 };
  16. private static String[] titles = new String[] { "", "" };
  17. private List<double[]> values = new ArrayList<double[]>();
  18. private static int[] colors = new int[] { Color.RED, Color.GREEN };
  19. private XYMultipleSeriesRenderer renderer;
  20. private Context mContext;
  21. private String mTitle;
  22. private List<String> option;
  23. private boolean isSingleView = false;
  24. public BarChartView(Context context, boolean isSingleView) {
  25. this.mContext = context;
  26. this.isSingleView = isSingleView;
  27. this.renderer = new XYMultipleSeriesRenderer();
  28. }
  29. public void initData(double[] firstAnswerPercent, double[] lastAnswerPercent, List<String> option, String title) {
  30. this.values.add(firstAnswerPercent);
  31. if (!isSingleView) {
  32. this.values.add(lastAnswerPercent);
  33. }
  34. this.mTitle = title;
  35. this.option = option;
  36. }
  37. public View getBarChartView() {
  38. buildBarRenderer();
  39. setChartSettings(renderer, mTitle, "", "", 0, 6, 0, 100, Color.BLACK, Color.LTGRAY);
  40. renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
  41. if(!isSingleView){
  42. renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
  43. }
  44. int size = option.size();
  45. for (int i = 0; i < size; i++) {
  46. renderer.addXTextLabel(i, option.get(i));
  47. }
  48. renderer.setMargins(margins);
  49. renderer.setMarginsColor(0x00ffffff);
  50. renderer.setPanEnabled(false, false);
  51. renderer.setZoomEnabled(false, false);// 设置x,y方向都不可以放大或缩�?
  52. renderer.setZoomRate(1.0f);
  53. renderer.setInScroll(false);
  54. renderer.setBackgroundColor(0x00ffffff);
  55. renderer.setApplyBackgroundColor(false);
  56. View view = ChartFactory.getBarChartView(mContext, buildBarDataset(titles, values), renderer, Type.DEFAULT); // Type.STACKED
  57. view.setBackgroundColor(0x00ffffff);
  58. return view;
  59. }
  60. private XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
  61. XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
  62. int length = isSingleView ? (titles.length - 1) : titles.length;
  63. for (int i = 0; i < length; i++) {
  64. CategorySeries series = new CategorySeries(titles[i]);
  65. double[] v = values.get(i);
  66. int seriesLength = v.length;
  67. for (int k = 0; k < seriesLength; k++) {
  68. series.add(v[k]);
  69. }
  70. dataset.addSeries(series.toXYSeries());
  71. }
  72. return dataset;
  73. }
  74. protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, String yTitle,
  75. double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) {
  76. renderer.setChartTitle(title);
  77. renderer.setXTitle(xTitle);
  78. renderer.setYTitle(yTitle);
  79. renderer.setXAxisMin(xMin);
  80. renderer.setXAxisMax(xMax);
  81. renderer.setYAxisMin(yMin);
  82. renderer.setYAxisMax(yMax);
  83. renderer.setAxesColor(axesColor);
  84. renderer.setLabelsColor(labelsColor);
  85. renderer.setXLabels(0);
  86. renderer.setYLabels(10);
  87. renderer.setLabelsTextSize(25);
  88. renderer.setYLabelsAlign(Align.RIGHT);
  89. renderer.setXLabelsAlign(Align.CENTER);
  90. // renderer.setXLabelsColor(0xff000000);//设置X轴上的字体颜�?
  91. // renderer.setYLabelsColor(0,0xff000000);//设置Y轴上的字体颜�?
  92. }
  93. /*
  94. * 初始化柱子风�?
  95. */
  96. protected void buildBarRenderer() {
  97. if (null == renderer) {
  98. return;
  99. }
  100. renderer.setBarWidth(23);
  101. renderer.setBarSpacing(20);
  102. renderer.setAxisTitleTextSize(16);
  103. renderer.setChartTitleTextSize(20);
  104. renderer.setLabelsTextSize(15);
  105. renderer.setLegendTextSize(15);
  106. int length = isSingleView ? (colors.length - 1) : colors.length;
  107. for (int i = 0; i < length; i++) {
  108. SimpleSeriesRenderer ssr = new SimpleSeriesRenderer();
  109. ssr.setChartValuesTextAlign(Align.RIGHT);
  110. ssr.setChartValuesTextSize(15);
  111. ssr.setDisplayChartValues(true);
  112. ssr.setColor(colors[i]);
  113. renderer.addSeriesRenderer(ssr);
  114. }
  115. }
  116. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/290562
推荐阅读
相关标签
  

闽ICP备14008679号