MPChart饼图工具类

package com.example.epnc.utils;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created by TCL on 2016/9/1.
 * 饼图自定义图例的二次封装
 */
public class PieChartUtils {

    /**
     * @param context      上下文
     * @param legendLayout 外层的Legend布局
     * @param pieChart     要初始化的饼图
     * @param data         饼图上的数据
     * @param labels       饼图上的数据描述
     * @param isCenterText 是否显示中间文字
     */
    public static void initPieChart(Context context, LinearLayout legendLayout, PieChart pieChart, float[] data,
                                    String[] labels, boolean isCenterText) {
        //设置描述
        pieChart.setDescription("");
        // 设置中心圆孔半径占整个饼状图半径的百分比(100f 是最大=整个图表的半径),默认的50%的百分比(即50f)。
        pieChart.setHoleRadius(55f);
        // 设置中间文字中大小
        pieChart.setCenterTextSize(12f);
        // 百分比显示
        pieChart.setUsePercentValues(true);
        // 禁用触摸
        pieChart.setTouchEnabled(false);
        //设置内置图例不显示
        pieChart.getLegend().setEnabled(false);
        // 设置隐藏饼图上文字,只显示百分比
        pieChart.setDrawSliceText(false);

        if (isCenterText) {
            pieChart.setCenterText(labels[0] + "\n" + data[0]);
        }


        //数值
        ArrayList<Entry> values = new ArrayList<>();
        //数值描述
        ArrayList<String> valuesLabels = new ArrayList<>();
        //颜色
        int[] colors = new int[data.length];

        //随机颜色需要随机
        Random random = new Random();
        for (int i = 0; i < data.length; i++) {
            //添加数值
            values.add(new Entry(Math.abs(data[i]), i));
            //添加数值描述
            valuesLabels.add(labels[i]);

            //随机颜色
            int red = 30 + random.nextInt(200);
            int green = 30 + random.nextInt(200);
            int blue = 30 + random.nextInt(200);

            //添加颜色
            colors[i] = Color.rgb(red, green, blue);
        }


        PieDataSet pieDataSet = new PieDataSet(values, "");
        pieDataSet.setSliceSpace(2f);
        pieDataSet.setValueTextColor(Color.WHITE);
        pieDataSet.setValueTextSize(12f);
        //将随机的颜色设置进来
        pieDataSet.setColors(colors);


        PieData pieData = new PieData(valuesLabels, pieDataSet);
        //设置百分比显示
        pieData.setValueFormatter(new PercentFormatter());

        //自定义图例 该方法一定要在setData之前执行
        setCustomizeLegend(legendLayout, context, colors, data, labels);

        //set data...
        pieChart.setData(pieData);

        //设置是否显示区域百分比的值
        for (IDataSet<?> set : pieChart.getData().getDataSets()) {
            set.setDrawValues(false);
        }
    }

    private static void setCustomizeLegend(LinearLayout legendLayout, Context context, int[] colors, float[] data,
                                           String[] labels) {
        for (int i = 0; i < data.length; i++) {
            //单个图例的layoutParams
            LinearLayout.LayoutParams singleLegendLayoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            // 设置比重为1
            singleLegendLayoutParams.weight = 1;
            // 单个图例的布局
            LinearLayout singleLegendLayout = new LinearLayout(context);
            // 水平排列
            singleLegendLayout.setOrientation(LinearLayout.HORIZONTAL);
            // 垂直居中
            singleLegendLayout.setGravity(Gravity.CENTER_VERTICAL);
            singleLegendLayout.setLayoutParams(singleLegendLayoutParams);


            //添加颜色方块
            LinearLayout.LayoutParams colorLayoutParams = new LinearLayout.LayoutParams(
                    20, 20);
            colorLayoutParams.setMargins(0, 0, 20, 0);
            LinearLayout colorLayout = new LinearLayout(context);
            colorLayout.setLayoutParams(colorLayoutParams);
            colorLayout.setBackgroundColor(colors[i]);
            singleLegendLayout.addView(colorLayout);

            //Label部分和百分比数值部分
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.weight = 1;
            params.gravity = Gravity.CENTER;


            // 添加label
            TextView tvLabel = new TextView(context);
            tvLabel.setText(labels[i] + " ");
            tvLabel.setLayoutParams(params);
            tvLabel.setTextColor(Color.BLACK);
            singleLegendLayout.addView(tvLabel);


            // 添加data(百分比显示)
            TextView tvPercentData = new TextView(context);
            float total = 0;
            for (int j = 0; j < data.length; j++) {
                total += Math.abs(data[j]);
            }
            tvPercentData.setTextColor(Color.BLACK);
            if (data[i] < 0) {
                tvPercentData.setText("-" + (-data[i] / total * 100 + "").substring(0, 4) + "%");
            } else {
                tvPercentData.setText((data[i] / total * 100 + "").substring(0, 3) + "%");
            }
            tvPercentData.setLayoutParams(params);
            singleLegendLayout.addView(tvPercentData);

            // legendLayout为外层布局即整个图例布局,是在xml文件中定义
            legendLayout.addView(singleLegendLayout);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值