公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图:

布局文件:

  1. <com.github.mikephil.charting.charts.PieChart
  2. android:id="@+id/chart"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" />

java代码:

1、初始化饼图

  1. private void initChart(){
  2. mChart = (PieChart) findViewById(R.id.chart);
  3. mChart.setUsePercentValues(true);
  4. mChart.setDescription("");
  5. mChart.setExtraOffsets(5, 10, 5, 5);
  6. //        mChart.setDrawSliceText(false);//设置隐藏饼图上文字,只显示百分比
  7. mChart.setDrawHoleEnabled(true);
  8. mChart.setHoleColorTransparent(true);
  9. mChart.setTransparentCircleColor(getResources().getColor(R.color.buttombar_bg));
  10. mChart.setTransparentCircleAlpha(110);
  11. mChart.setOnChartValueSelectedListener(this);
  12. mChart.setHoleRadius(45f); //半径
  13. //mChart.setHoleRadius(0)  //实心圆
  14. mChart.setTransparentCircleRadius(48f);// 半透明圈
  15. mChart.setDrawCenterText(true);//饼状图中间可以添加文字
  16. // 如果没有数据的时候,会显示这个,类似ListView的EmptyView
  17. mChart.setNoDataText(getResources().getString(R.string.no_data));
  18. mChart.setUsePercentValues(true);//设置显示成比例
  19. SimpleDateFormat format = new SimpleDateFormat("yyyy");
  20. String year = format.format(since_at*1000);
  21. mChart.setCenterText(generateCenterSpannableText(year));
  22. mChart.setRotationAngle(0); // 初始旋转角度
  23. // enable rotation of the chart by touch
  24. mChart.setRotationEnabled(true); // 可以手动旋转
  25. mChart.setHighlightPerTapEnabled(true);
  26. mChart.animateY(1000, Easing.EasingOption.EaseInOutQuad); //设置动画
  27. Legend mLegend = mChart.getLegend();  //设置比例图
  28. mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);  //左下边显示
  29. mLegend.setFormSize(12f);//比例块字体大小
  30. mLegend.setXEntrySpace(2f);//设置距离饼图的距离,防止与饼图重合
  31. mLegend.setYEntrySpace(2f);
  32. //设置比例块换行...
  33. mLegend.setWordWrapEnabled(true);
  34. mLegend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
  35. mLegend.setTextColor(getResources().getColor(R.color.alpha_80));
  36. mLegend.setForm(Legend.LegendForm.SQUARE);//设置比例块形状,默认为方块
  37. //        mLegend.setEnabled(false);//设置禁用比例块
  38. }

2、设置饼图数据

  1. /**
  2. * 设置饼图的数据
  3. * @param names 饼图上显示的比例名称
  4. * @param counts 百分比
  5. */
  6. private void setData(ArrayList<String> names,ArrayList<Entry> counts) {
  7. PieDataSet dataSet = new PieDataSet(counts, "");
  8. dataSet.setSliceSpace(2f);
  9. dataSet.setSelectionShift(5f);
  10. ArrayList<Integer> colors = new ArrayList<Integer>();
  11. for (int c : ColorTemplate.JOYFUL_COLORS)
  12. colors.add(c);
  13. //
  14. for (int c : ColorTemplate.COLORFUL_COLORS)
  15. colors.add(c);
  16. for (int c : ColorTemplate.LIBERTY_COLORS)
  17. colors.add(c);
  18. //        for (int c : ColorTemplate.PASTEL_COLORS)
  19. //            colors.add(c);
  20. colors.add(ColorTemplate.getHoloBlue());
  21. //        colors.add(getResources().getColor(R.color.stastic_team));
  22. dataSet.setColors(colors);
  23. //dataSet.setSelectionShift(0f);
  24. PieData data = new PieData(names, dataSet);
  25. data.setValueFormatter(new PercentFormatter());
  26. data.setValueTextSize(12f);
  27. data.setValueTextColor(getResources().getColor(R.color.whrite));
  28. mChart.setData(data);
  29. // undo all highlights
  30. mChart.highlightValues(null);
  31. mChart.invalidate();
  32. }

在这个过程中遇到几个问题:

1、底部的比例块显示只有一行,如果数据较多的话,会超出屏幕外边,所以需要换行显示比例块,设置如下:

  1. //设置比例块换行...
  2. mLegend.setWordWrapEnabled(true);
  1. 同时需要把比例块放到下边或者放到上边  <span style="font-family: Arial, Helvetica, sans-serif;"> mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);  //左下边显示</span>

2、饼图上怎么只显示百分比,查了好多资料没有看有人提到这个,最后在stackoverflow上找到答案。

http://stackoverflow.com/questions/31154706/mpandroidchart-piechart-remove-percents

pieChart.setDrawSliceText(false)

3、如果比例块放在左边或者右边,如果字体太长,会和饼图叠加在一起,做以下处理可以防止叠加。

  1. mLegend.setXEntrySpace(2f);//设置距离饼图的距离,防止与饼图重合
  2. mLegend.setYEntrySpace(2f);

MPAndroidChart饼图属性及相关设置的更多相关文章

  1. scrapy的简单使用以及相关设置属性的介绍

    0. 楔子(一个最简单的案例) 1.scrapy.Spider scrapy.spiders.Spider name allowed_domains start_urls custom_setting ...

  2. zend studio 9.0.4 破解、汉化和字体颜色及快捷键相关设置

    转载:http://www.penglig.com/post-45.html 下载:http://www.geekso.com/component/zendstudio-downloads/ 破解:h ...

  3. [Other] 自定义MIME类型支持FLV的相关设置

    刚测试知道为何服务器无法播放flv的原因,特此记录而已. 网络空间支持FLV的相关设置,就是自定义一个MIME类型,一般虚拟主机管理里面都有这个选项 自定义MIME类型 扩展名: .flv MIME类 ...

  4. NPOI 教程 - 3.2 打印相关设置

    转:http://www.cnblogs.com/wolfplan/archive/2013/01/13/2858991.html NPOI 教程 - 3.2 打印相关设置   打印设置主要包括方向设 ...

  5. magento搜索属性值的设置方法

    前台特性(Frontend Properties)在快速搜索中应用(Use in quick search) - 开启此选项,在客户使用Header中的 搜索功能时Magento将搜索所有产品这个At ...

  6. scale相关设置—手动设置

    在ggplot2 中,可以进行手动设置的函数有: scale_colour_manual(..., values).scale_fill_manual(..., values). scale_size ...

  7. ggplot2 scale相关设置

     ggplot2   scale相关设置 标度设置:主要用于在ggplot画图后的各个图层进行调整设置. 1.相关属性scale设置 包括scale_size().scale_alpha().scal ...

  8. MPAndroidChart——饼图

    MPAndroidChart--饼图 MPAndroidChart是安卓下的一个开源图形库,很多效果,简单看几个效果图 Github地址:https://github.com/PhilJay/MPAn ...

  9. Pycharm的相关设置和快捷键集合

    原文参考地址:python 环境搭建及pycharm的使用 一.相关设置 1.主题和字体 主题选这个 字体大小在这里设置 二.快捷键 1.编辑(Editing)Ctrl + Space 基本的代码完成 ...

随机推荐

  1. ACM/ICPC 之 Prim范例(ZOJ1586-POJ1789(ZOJ2158))

    两道Prim解法范例题型,简单的裸Prim,且两题相较以边为重心的Kruskal解法而言更适合以点为重心扩展的Prim解法. ZOJ1586-QS Network 题意:见Code 题解:直接的MST ...

  2. MySQL排序原理与MySQL5.6案例分析【转】

    本文来自:http://www.cnblogs.com/cchust/p/5304594.html,其中对于自己觉得是重点的加了标记,方便自己查阅.更多详细的说明可以看沃趣科技的文章说明. 前言    ...

  3. Ubuntu ( Linux) Eclipse 乱码问题

    刚装完Ubuntu,导入Java和Android项目时,发现字符乱码,究其原因,是由于Windows下使用的是GBK编码,而Ubuntu使用的是UTF-8编码.网上查找了相关资料,主要解决方案有两种. ...

  4. c/c++与Python的语法差异

    1.程序块语法方面: c/c++中用一对“{}”将多段语句括起来,表示一个程序块,并以右大括号表明程序块结束 ;i<n;i++) { cout<<a[i]; j+=; } Pytho ...

  5. HTML 表单 选择器

    表单元素 每个表单都对应一个<form></form>标签    表单内所有元素都写在 <form></form>里面: 1.最重要的属性 <fo ...

  6. IOS-Social.framework

    1.使用前 需要导入Social.framework 框架 2.实例代码(新浪微博为例) - (IBAction)shejiaoBtn {        // 判断服务器是否可用    if ([SL ...

  7. osg::NodeVisitor中计算一个节点对应的世界变换矩阵、法向量、顶点坐标

    class MyNodeVisitor:public osg::NodeVisitor { pulic: MyNodeVisitor():osg::NodeVisitor(osg::NodeVisit ...

  8. xmpp SASL 定义

    SASL 定义 <摘抄自:xmpp_3920> [SASL]的 profiling 需求要求协议定义 供以下信息: 服务名:“xmpp” 初始序列:初始实体 供一个开放 XML 流头后,并 ...

  9. oracle dataguard (一)

    一.什么是data guard及data guard的工作原理 Data Guard 是一个集合,由一个primary数据库(生产数据库)及一个或多个standby数据库(最多9个)组成.组成Data ...

  10. 浅析 - 提高xib(Interface Builder)高效工作的几个小技巧

    本文译自:8 Tips for working effectively with Interface Builder(需FQ)先来看看目录:介绍使view的Size与view中的Content相适应按 ...