您的位置:首页 > 移动开发 > Android开发

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);

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: