Newer
Older
MpAndroidChart / MPChartExample / src / com / xxmassdeveloper / mpchartexample / view / MyPieChart.java
Bello on 7 Mar 2018 6 KB 调整颜色Theme
package com.xxmassdeveloper.mpchartexample.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
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.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.xxmassdeveloper.mpchartexample.R;

import java.util.ArrayList;

/**
 * @Info 圆饼图
 * @Auth Bello
 * @Time 18-3-2 下午6:08
 * @Ver
 */

public class MyPieChart extends RelativeLayout implements OnChartValueSelectedListener {
    private Context context;
    private TextView titleText;
    private CheckBox showBtn;
    //圆饼形控件
    private PieChart mChart;
    //圆饼图数据
    private ArrayList<PieEntry> entries = new ArrayList<>();


    public MyPieChart(Context context) {
        super(context);
        initView(context);
    }

    public MyPieChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    /**
     * 加载UI布局
     *
     * @param context
     */
    private void initView(Context context) {
        this.context = context;
        View.inflate(context, R.layout.lt_my_pie, this);
        titleText = this.findViewById(R.id.chart_title_text);
        showBtn = this.findViewById(R.id.chart_show_text);
        showBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                for (IDataSet<?> set : mChart.getData().getDataSets())
                    set.setDrawValues(!set.isDrawValuesEnabled());

                mChart.invalidate();
            }
        });
        mChart = this.findViewById(R.id.chart_pie);

    }


    /**
     * 加载数据
     *
     * @param chartName
     * @param entries
     */
    public void setData(String chartName, ArrayList<PieEntry> entries) {
        this.entries = entries;

        //显示表的名称
        titleText.setText(chartName);

        //载入chart
        loadChart();

        if (entries.size() > 0) {
            loadData();
        }
    }


    /**
     * 为chart加载数据
     */
    private void loadChart() {
        mChart.setUsePercentValues(true);
        mChart.setBackgroundColor(Color.WHITE);
        mChart.getDescription().setEnabled(false);
        mChart.setExtraOffsets(5, 5, 5, 5);

        mChart.setDragDecelerationFrictionCoef(0.95f);

//        mChart.setCenterText(generateCenterSpannableText());


        mChart.setDrawHoleEnabled(false);
/*        mChart.setHoleColor(Color.WHITE);
        mChart.setTransparentCircleColor(Color.WHITE);
        mChart.setTransparentCircleAlpha(110);
        mChart.setHoleRadius(158f);
        mChart.setTransparentCircleRadius(61f);
*/

        mChart.setDrawCenterText(false);

        mChart.setRotationAngle(0);
        // enable rotation of the chart by touch
        mChart.setRotationEnabled(true);
        mChart.setHighlightPerTapEnabled(true);

        // entry label styling
        mChart.setEntryLabelColor(Color.WHITE);
        mChart.setEntryLabelTextSize(12f);

        mChart.setUsePercentValues(true);
        //显示自定义的文字格式
        mChart.setMyLabelStyle(true);

        // add a selection listener
//        mChart.setOnChartValueSelectedListener(this);

        mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
        // mChart.spin(2000, 0, 360);

        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);
        l.setXOffset(2f);
        l.setEnabled(false);

    }

    private void loadData() {

        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setDrawIcons(false);
        dataSet.setSliceSpace(0f);
        dataSet.setSelectionShift(5f);

        // add a lot of colors
        ArrayList<Integer> colors = new ArrayList<>();
        for (int i = 0; i < entries.size(); i++) {
            colors.add(Color.rgb(240 - i * 18, 108 - i * 18, 0));
        }
        dataSet.setColors(colors);
        //dataSet.setSelectionShift(0f);
//        dataSet.setValueLinePart1OffsetPercentage(80.f);
        dataSet.setValueLinePart1Length(0.7f);
        dataSet.setValueLinePart2Length(0f);
        dataSet.setValueLineColor(Color.TRANSPARENT);
        dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(ColorChartTheme.DRAY_COLOR);


        mChart.setData(data);

        // undo all highlights
        mChart.highlightValues(null);

        mChart.invalidate();
    }


    /**
     * Called when a value has been selected inside the chart.
     *
     * @param e The selected Entry
     * @param h The corresponding highlight object that contains information
     */
    @Override
    public void onValueSelected(Entry e, Highlight h) {
        if (e == null)
            return;
    }

    /**
     * Called when nothing has been selected or an "un-select" has been made.
     */
    @Override
    public void onNothingSelected() {
    }


}