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 基本的代码完成 ...
随机推荐
- java读取excel
/* * this function will read from excel * and will return the items of excel */ public static String ...
- ffmpeg-20160515-git-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- 【leetcode】Single Number (Medium) ☆
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 【leetcode】Min Stack(easy)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【python】入门学习(十)
#入门学习系列的内容均是在学习<Python编程入门(第3版)>时的学习笔记 统计一个文本文档的信息,并输出出现频率最高的10个单词 #text.py #保留的字符 keep = {'a' ...
- 【XLL 文档翻译】【第2部分】C API 回调函数 Excel4, Excel12
Excel4 和 Excel12 函数使得 DLL 可以调用 Excel 工作表函数.宏表函数.命令.XLL特定函数或命令.最近的一些 Excel 版本都支持 Excel12 函数.这两个函数支持下面 ...
- IOS-01零碎知识总结
1. 变量的@public @private @package @protected 声明有什么含义? @public 可以被所有的类访问 @private 只有该类的方法可以访问,子类的都不能访 ...
- Does the OpenSceneGraph have a native file format?
From OpenSceneGraph-3.0 onwards we have new native file formats based on generic serializers that ar ...
- Linux Shell常用快捷键
ctrl+a[A]:将光标移到命令行开头 ctrl+e[E]:将光标移到命令行结尾 ctrl+c[C]:强制终止命令执行 ctrl+u[U]:删除/剪切光标之前的所有字符 ctrl+y[Y]:粘贴ct ...
- printf 打印 unit32_t
今天在网上找了老半天终于找到了:printf 打印 uint32_t 类型 uint32_t domainId; printf("\ndomainId=========%lu", ...