MPAndroidChart饼图属性及相关设置
公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图:
布局文件:
- <com.github.mikephil.charting.charts.PieChart
- android:id="@+id/chart"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
java代码:
1、初始化饼图
- private void initChart(){
- mChart = (PieChart) findViewById(R.id.chart);
- mChart.setUsePercentValues(true);
- mChart.setDescription("");
- mChart.setExtraOffsets(5, 10, 5, 5);
- // mChart.setDrawSliceText(false);//设置隐藏饼图上文字,只显示百分比
- mChart.setDrawHoleEnabled(true);
- mChart.setHoleColorTransparent(true);
- mChart.setTransparentCircleColor(getResources().getColor(R.color.buttombar_bg));
- mChart.setTransparentCircleAlpha(110);
- mChart.setOnChartValueSelectedListener(this);
- mChart.setHoleRadius(45f); //半径
- //mChart.setHoleRadius(0) //实心圆
- mChart.setTransparentCircleRadius(48f);// 半透明圈
- mChart.setDrawCenterText(true);//饼状图中间可以添加文字
- // 如果没有数据的时候,会显示这个,类似ListView的EmptyView
- mChart.setNoDataText(getResources().getString(R.string.no_data));
- mChart.setUsePercentValues(true);//设置显示成比例
- SimpleDateFormat format = new SimpleDateFormat("yyyy");
- String year = format.format(since_at*1000);
- mChart.setCenterText(generateCenterSpannableText(year));
- mChart.setRotationAngle(0); // 初始旋转角度
- // enable rotation of the chart by touch
- mChart.setRotationEnabled(true); // 可以手动旋转
- mChart.setHighlightPerTapEnabled(true);
- mChart.animateY(1000, Easing.EasingOption.EaseInOutQuad); //设置动画
- Legend mLegend = mChart.getLegend(); //设置比例图
- mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT); //左下边显示
- mLegend.setFormSize(12f);//比例块字体大小
- mLegend.setXEntrySpace(2f);//设置距离饼图的距离,防止与饼图重合
- mLegend.setYEntrySpace(2f);
- //设置比例块换行...
- mLegend.setWordWrapEnabled(true);
- mLegend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
- mLegend.setTextColor(getResources().getColor(R.color.alpha_80));
- mLegend.setForm(Legend.LegendForm.SQUARE);//设置比例块形状,默认为方块
- // mLegend.setEnabled(false);//设置禁用比例块
- }
2、设置饼图数据
- /**
- * 设置饼图的数据
- * @param names 饼图上显示的比例名称
- * @param counts 百分比
- */
- private void setData(ArrayList<String> names,ArrayList<Entry> counts) {
- PieDataSet dataSet = new PieDataSet(counts, "");
- dataSet.setSliceSpace(2f);
- dataSet.setSelectionShift(5f);
- ArrayList<Integer> colors = new ArrayList<Integer>();
- for (int c : ColorTemplate.JOYFUL_COLORS)
- colors.add(c);
- //
- for (int c : ColorTemplate.COLORFUL_COLORS)
- colors.add(c);
- for (int c : ColorTemplate.LIBERTY_COLORS)
- colors.add(c);
- // for (int c : ColorTemplate.PASTEL_COLORS)
- // colors.add(c);
- colors.add(ColorTemplate.getHoloBlue());
- // colors.add(getResources().getColor(R.color.stastic_team));
- dataSet.setColors(colors);
- //dataSet.setSelectionShift(0f);
- PieData data = new PieData(names, dataSet);
- data.setValueFormatter(new PercentFormatter());
- data.setValueTextSize(12f);
- data.setValueTextColor(getResources().getColor(R.color.whrite));
- mChart.setData(data);
- // undo all highlights
- mChart.highlightValues(null);
- mChart.invalidate();
- }
在这个过程中遇到几个问题:
1、底部的比例块显示只有一行,如果数据较多的话,会超出屏幕外边,所以需要换行显示比例块,设置如下:
- //设置比例块换行...
- mLegend.setWordWrapEnabled(true);
- 同时需要把比例块放到下边或者放到上边 <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、如果比例块放在左边或者右边,如果字体太长,会和饼图叠加在一起,做以下处理可以防止叠加。
- mLegend.setXEntrySpace(2f);//设置距离饼图的距离,防止与饼图重合
- mLegend.setYEntrySpace(2f);
MPAndroidChart饼图属性及相关设置的更多相关文章
- scrapy的简单使用以及相关设置属性的介绍
0. 楔子(一个最简单的案例) 1.scrapy.Spider scrapy.spiders.Spider name allowed_domains start_urls custom_setting ...
- zend studio 9.0.4 破解、汉化和字体颜色及快捷键相关设置
转载:http://www.penglig.com/post-45.html 下载:http://www.geekso.com/component/zendstudio-downloads/ 破解:h ...
- [Other] 自定义MIME类型支持FLV的相关设置
刚测试知道为何服务器无法播放flv的原因,特此记录而已. 网络空间支持FLV的相关设置,就是自定义一个MIME类型,一般虚拟主机管理里面都有这个选项 自定义MIME类型 扩展名: .flv MIME类 ...
- NPOI 教程 - 3.2 打印相关设置
转:http://www.cnblogs.com/wolfplan/archive/2013/01/13/2858991.html NPOI 教程 - 3.2 打印相关设置 打印设置主要包括方向设 ...
- magento搜索属性值的设置方法
前台特性(Frontend Properties)在快速搜索中应用(Use in quick search) - 开启此选项,在客户使用Header中的 搜索功能时Magento将搜索所有产品这个At ...
- scale相关设置—手动设置
在ggplot2 中,可以进行手动设置的函数有: scale_colour_manual(..., values).scale_fill_manual(..., values). scale_size ...
- ggplot2 scale相关设置
ggplot2 scale相关设置 标度设置:主要用于在ggplot画图后的各个图层进行调整设置. 1.相关属性scale设置 包括scale_size().scale_alpha().scal ...
- MPAndroidChart——饼图
MPAndroidChart--饼图 MPAndroidChart是安卓下的一个开源图形库,很多效果,简单看几个效果图 Github地址:https://github.com/PhilJay/MPAn ...
- Pycharm的相关设置和快捷键集合
原文参考地址:python 环境搭建及pycharm的使用 一.相关设置 1.主题和字体 主题选这个 字体大小在这里设置 二.快捷键 1.编辑(Editing)Ctrl + Space 基本的代码完成 ...
随机推荐
- ACM/ICPC 之 Bellman Ford练习题(ZOJ1791(POJ1613))
这道题稍复杂一些,需要掌握字符串输入的处理+限制了可以行走的时间. ZOJ1791(POJ1613)-Cave Raider //限制行走时间的最短路 //POJ1613-ZOJ1791 //Time ...
- Java for LeetCode 208 Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- mysql-5.6.23-winx64.zip版本安装记录
*操作系统:Win7 64位旗舰版 一.解压至任意目录,此处以“E:\mysql-5.6.23-winx64”为例: 二.设置环境变量:新建变量名 MYSQL_HOME,值为解压的路径 E:\mysq ...
- CentOS 7 关闭图形界面
CentOS 7 关闭图形界面 查看/etc/inittab如下: # systemd uses 'targets' instead of runlevels. # by default, there ...
- Effective C++ -----条款51:编写new 和delete 时需固守常规
operator new 应该内含一个无穷循环,并在其中尝试分配内存,如果它无法满足内存需求,就该调用new-handler.它也应该有能力处理0 bytes 申请.Class专属版本则还应该处理“比 ...
- Match:Censored!(AC自动机+DP+高精度)(POJ 1625)
Censored! 题目大意:给定一些字符,将这些字符组成一个固定长度的字符串,但是字符串不能包含一些禁词,问你有多少种组合方式. 这是一道好题,既然出现了“一些”禁词,那么这题肯定和AC自动机有点 ...
- codeforces 507B. Painting Pebbles 解题报告
题目链接:http://codeforces.com/problemset/problem/509/B 题目意思:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色 ...
- 从下拉菜单拖拽一个元素 出来,插入到页面中的app 列表中
1,实现功能:从下拉菜单拖拽一个元素 出来,插入到页面中的app 列表中 并实现app向后移动一个元素的位置: 2.实现思路: 01.遍历下拉菜单,添加拖拽方法,实现位置移动功能: 02.遍历app列 ...
- NEFU 504 new Flip Game (高斯消元)
题目链接 题解:和 poj1753Filp game 差不多,区别在于t组数据并且翻转的时候多了一个左上角. #include <iostream> #include <cstdio ...
- iOS MD5加密
1.MD5加密 Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 1321 ...