MPAndroidChart饼图属性及相关设置
2016-07-12 18:31
676 查看
公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图:
布局文件:
[java] view plain copy
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
java代码:
1、初始化饼图
[java] view plain copy
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、设置饼图数据
[java] view plain copy
/**
* 设置饼图的数据
* @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、底部的比例块显示只有一行,如果数据较多的话,会超出屏幕外边,所以需要换行显示比例块,设置如下:
[java] view plain copy
//设置比例块换行...
mLegend.setWordWrapEnabled(true);
[java] view plain copy
同时需要把比例块放到下边或者放到上边 <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、如果比例块放在左边或者右边,如果字体太长,会和饼图叠加在一起,做以下处理可以防止叠加。
[java] view plain copy
mLegend.setXEntrySpace(2f);//设置距离饼图的距离,防止与饼图重合
mLegend.setYEntrySpace(2f);
相关文章推荐
- Android SDK下载
- xamarin+VS2015安装步骤和破解方法(android)
- Android打包的那些事
- bug-android之INSTALL_FAILED_NO_MATCHING_ABIS无法安装在虚拟机
- Android TextView文字中底部或者中间加横线
- bug-android之INSTALL_FAILED_NO_MATCHING_ABIS
- 【转载】Android RecyclerView Bug:IndexOutOfBoundsException: Inconsistency detected. Invalid item position
- Android:minSdkVersion,targetSdkVersion,compileSdkVersion
- Android追加写入文件的几种方法
- Android 浏览器开发调研
- Android配置ip地址
- android MPAndroidChart饼图实现图例后加数字或文本(定制图例)
- Android - Glide V3 图片加载框架
- android 笔记11 得到布局,状态栏显示隐藏,代码优化
- android异步处理,分析Handle消息机制
- Mac版Android Studio Git 的配置以及提交到仓库
- ListView的水平滑动
- 仿淘宝商品详情页面标题下拉渐变效果
- 一种提高Android应用进程存活率新方法
- Android studio中如何生成引用 .aar 和 .jar