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 之 Prim范例(ZOJ1586-POJ1789(ZOJ2158))
两道Prim解法范例题型,简单的裸Prim,且两题相较以边为重心的Kruskal解法而言更适合以点为重心扩展的Prim解法. ZOJ1586-QS Network 题意:见Code 题解:直接的MST ...
- MySQL排序原理与MySQL5.6案例分析【转】
本文来自:http://www.cnblogs.com/cchust/p/5304594.html,其中对于自己觉得是重点的加了标记,方便自己查阅.更多详细的说明可以看沃趣科技的文章说明. 前言 ...
- Ubuntu ( Linux) Eclipse 乱码问题
刚装完Ubuntu,导入Java和Android项目时,发现字符乱码,究其原因,是由于Windows下使用的是GBK编码,而Ubuntu使用的是UTF-8编码.网上查找了相关资料,主要解决方案有两种. ...
- c/c++与Python的语法差异
1.程序块语法方面: c/c++中用一对“{}”将多段语句括起来,表示一个程序块,并以右大括号表明程序块结束 ;i<n;i++) { cout<<a[i]; j+=; } Pytho ...
- HTML 表单 选择器
表单元素 每个表单都对应一个<form></form>标签 表单内所有元素都写在 <form></form>里面: 1.最重要的属性 <fo ...
- IOS-Social.framework
1.使用前 需要导入Social.framework 框架 2.实例代码(新浪微博为例) - (IBAction)shejiaoBtn { // 判断服务器是否可用 if ([SL ...
- osg::NodeVisitor中计算一个节点对应的世界变换矩阵、法向量、顶点坐标
class MyNodeVisitor:public osg::NodeVisitor { pulic: MyNodeVisitor():osg::NodeVisitor(osg::NodeVisit ...
- xmpp SASL 定义
SASL 定义 <摘抄自:xmpp_3920> [SASL]的 profiling 需求要求协议定义 供以下信息: 服务名:“xmpp” 初始序列:初始实体 供一个开放 XML 流头后,并 ...
- oracle dataguard (一)
一.什么是data guard及data guard的工作原理 Data Guard 是一个集合,由一个primary数据库(生产数据库)及一个或多个standby数据库(最多9个)组成.组成Data ...
- 浅析 - 提高xib(Interface Builder)高效工作的几个小技巧
本文译自:8 Tips for working effectively with Interface Builder(需FQ)先来看看目录:介绍使view的Size与view中的Content相适应按 ...