MPAndroidChart饼图属性及相关设置


公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图:


MPAndroidChart饼图属性及相关设置_第1张图片



MPAndroidChart饼图属性及相关设置_第2张图片



布局文件:

 

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 names,ArrayList counts) {
        PieDataSet dataSet = new PieDataSet(counts, "");
        dataSet.setSliceSpace(2f);
        dataSet.setSelectionShift(5f);

        ArrayList colors = new ArrayList();
        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);
同时需要把比例块放到下边或者放到上边   mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);  //左下边显示
2、饼图上怎么只显示百分比,查了好多资料没有看有人提到这个,最后在stackoverflow上找到答案。

      http://stackoverflow.com/questions/31154706/mpandroidchart-piechart-remove-percents

pieChart.setDrawSliceText(false)

3、如果比例块放在左边或者右边,如果字体太长,会和饼图叠加在一起,做以下处理可以防止叠加。

    mLegend.setXEntrySpace(2f);//设置距离饼图的距离,防止与饼图重合
    mLegend.setYEntrySpace(2f);

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


你可能感兴趣的:(android资料)