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

 以上就是本人遇到的一些问题,在这里和大家分享,如有问题及时交流..


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值