公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用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. 8.nodejs权威指南--MongoDB

    1. MongoDB var mongo = require('mongodb'); var host = '127.0.0.1'; var port = mongo.Connecton.DEFAUL ...

  2. ffmpeg-20160813-bin.7z

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  3. WPF ListView 排序

    代码如下: list为ListView组件.Score为要排序的列,也是绑定的属性. CollectionViewSource.GetDefaultView(list.ItemsSource).Sor ...

  4. 2.openstack之mitaka搭建控制节点数据库和消息队列

    一:部署mariadb数据库 控制节点(192.168.11.103): yum install mariadb mariadb-server python2-PyMySQL -y 编辑: /etc/ ...

  5. HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

    在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平 ...

  6. Common Knowledge_快速幂

    问题 I: Common Knowledge 时间限制: 1 Sec  内存限制: 64 MB提交: 9  解决: 8[提交][状态][讨论版] 题目描述 Alice and Bob play som ...

  7. mybatis反向生成sql,基本的增删改查

    用到的几个文件 MyBatisGeneratorProxy.java package com.timestech.wsgk.test.tools; import static org.mybatis. ...

  8. 【leetcode】Sort List (middle)

    Sort a linked list in O(n log n) time using constant space complexity. 思路: 用归并排序.设输入链表为S,则先将其拆分为前半部分 ...

  9. 【XLL API 函数】xlGetName

    以字符串格式返回 DLL 文件的长文件名. 原型 Excel12(xlGetName, LPXLOPER12 pxRes, 0); 参数 这个函数没有参数 属性值和返回值 返回文件名和路径 实例 \S ...

  10. SEH-关于捕获memcpy的异常

    网上有说memcpy是C语言写的,没有异常处理机制. 但是貌似SEH可以处理. SEH("Structured Exception Handling"),即结构化异常处理·是(wi ...