MPAndroidChart3 - pieChart(饼状图)

重点:
https://blog.csdn.net/qq_20553007/article/details/82793487

细节学习-方法解释
https://blog.csdn.net/Honiler/article/details/80073883
https://blog.csdn.net/Morling0811/article/details/77450730

package com.eshore.cloud.activitys.ai.list;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;

import com.eshore.cloud.R;
import com.eshore.cloud.activitys.BaseActivity;
import com.eshore.cloud.utils.L;
import com.eshore.cloud.utils.Tools;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
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.utils.ColorTemplate;

import java.util.ArrayList;

public class AIListActivity extends BaseActivity {
    private PieChart picChartView;

    private Typeface tf;

    protected String[] mParties = new String[] {
            "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
            "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
            "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
            "Party Y", "Party Z"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Tools.setBarColor(this);
        setContentView(R.layout.activity_ailist);

        picChartView = (PieChart) findViewById(R.id.pie_chart_with_line);
        /**
         * 是否使用百分比
         */
        picChartView.setUsePercentValues(true);
        /**
         * 描述信息
         */
        /*Description tempDescription=new Description();
        tempDescription.setText("描述信息");
        pie_chart_with_line.setDescription(tempDescription);*/
        picChartView.getDescription().setEnabled(false);

        //tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

        /**
         * 设置圆环中间的文字
         */
        picChartView.setCenterText(generateCenterSpannableText());
        /**
         * 圆环距离屏幕上下上下左右的距离
         */
        picChartView.setExtraOffsets(5f, 5.f, 5.f, 5.f);
        /**
         * 是否显示圆环中间的洞
         */
        picChartView.setDrawHoleEnabled(true);
        /**
         * 设置中间洞的颜色
         */
        picChartView.setHoleColor(Color.WHITE);
        /**
         * 设置圆环透明度及半径
         */
        picChartView.setTransparentCircleColor(Color.YELLOW);
        picChartView.setTransparentCircleAlpha(110);
        picChartView.setTransparentCircleRadius(61f);

        /**
         * 设置圆环中间洞的半径
         */
        picChartView.setHoleRadius(60f);

        /**
         * 是否显示洞中间文本
         */
        picChartView.setDrawCenterText(true);

        /**
         *触摸是否可以旋转以及松手后旋转的度数
         */
        picChartView.setRotationAngle(20);
        // enable rotation of the chart by touch
        picChartView.setRotationEnabled(false);

        picChartView.setDrawEntryLabels(false);
        //选中是否高亮,放大显示
        picChartView.setHighlightPerTapEnabled(false);
        /**
         *add a selection listener 值改变时候的监听
         */
       /* pie_chart_with_line.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                L.w("tan","Entry="+e);
                //e.getY() 等于 tempPieEntry.getValue()
                showToast(""+e.getData());
                PieEntry tempPieEntry=(PieEntry)e;
                //tempPieEntry.getValue()
                //tempPieEntry.getLabel()
                L.w("tan","tempPieEntry="+tempPieEntry);
            }

            @Override
            public void onNothingSelected() {

            }
        });*/



        //setData(3, 100);
        setData2(3);

        //圆环外面文字列表样式
        Legend l = picChartView.getLegend();
        //l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_INSIDE);
        l.setEnabled(false);
        l.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        l.setForm(Legend.LegendForm.CIRCLE);

        l.setFormSize(8f);
        l.setFormToTextSpace(4f);
        l.setXEntrySpace(6f);

        //pie_chart_with_line.animateY(1400, Easing.EasingOption.EaseInOutQuad);


    }

    private SpannableString generateCenterSpannableText() {
        SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda");
        s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
        s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
        s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
        s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
        s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
        s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
        return s;
    }

    private void setData2(int count){
        float mult = 100;
        ArrayList<PieEntry> yVals = new ArrayList<PieEntry>();
        for (int i = 0; i < count + 1; i++) {
            float value=(float) (Math.random() * mult) + mult / 5;
            String name=mParties[i % mParties.length];
            L.w("tan","v="+value+",n="+name);
            yVals.add(new PieEntry(value, name));
        }

        /*List<Integer> colors = new ArrayList<>();
        colors.add(Color.BLUE);
        colors.add(Color.RED);
        colors.add(Color.GREEN);
        colors.add(Color.BLACK);
        colors.add(Color.YELLOW);*/
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

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

        PieDataSet pieDataSet = new PieDataSet(yVals, "Election Results");
        pieDataSet.setColors(colors);
        PieData pieData = new PieData(pieDataSet);
        //圆环内 是否显示百分比文字
        pieData.setDrawValues(false);
        picChartView.setData(pieData);
        picChartView.highlightValues(null);
        picChartView.invalidate();

    }




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值