MPAndroidChart饼图用法系列一

一、展示效果:
这里写图片描述
这里写图片描述

这里写图片描述

二、用法:
1、引入仓库:工程路径下面的build.gradle文件中引入 maven { url “https://jitpack.io” }
这里写图片描述

2、引入jar包 compile ‘com.github.PhilJay:MPAndroidChart:v3.0.2’

3、XML中使用饼图控件
这里写图片描述

4、`

package com.cd.mobile.demochart;

import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;

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.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener {

    private PieChart mPieChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();


    }

    private void initView() {
        //饼状图
        mPieChart = (PieChart) findViewById(R.id.mPieChart);
        mPieChart.setUsePercentValues(true);//设置value是否用显示百分数,默认为false
        mPieChart.getDescription().setEnabled(false);//设置描述
        mPieChart.setExtraOffsets(5, 10, 5, 5);//设置饼状图距离上下左右的偏移量

        mPieChart.setDragDecelerationFrictionCoef(0.95f);//设置阻尼系数,范围在[0,1]之间,越小饼状图转动越困难
        //设置中间文字
        mPieChart.setDrawCenterText(true);//是否绘制中间的文字
        mPieChart.setCenterText(generateCenterSpannableText());
        mPieChart.setCenterTextSize(10f);

//        mPieChart.setNoDataText("暂无数据");// 如果没有数据的时候,会显示这个,类似ListView的EmptyView

        mPieChart.setDrawHoleEnabled(true);//是否绘制饼状图中间的圆
        mPieChart.setHoleColor(Color.WHITE);//饼状图中间的圆的绘制颜色

        mPieChart.setTransparentCircleColor(Color.WHITE);//设置圆环的颜色
        mPieChart.setTransparentCircleAlpha(110);//设置圆环的透明度[0,255]

        mPieChart.setHoleRadius(58f);//饼状图中间的圆的半径大小
        mPieChart.setTransparentCircleRadius(61f);//设置圆环的半径值

        mPieChart.setRotationAngle(0);//设置饼状图旋转的角度
        // 触摸旋转
        mPieChart.setRotationEnabled(true);
        ;//设置饼状图是否可以旋转(默认为true)
        mPieChart.setHighlightPerTapEnabled(true);//设置旋转的时候点中的tab是否高亮(默认为true)

        //变化监听
        mPieChart.setOnChartValueSelectedListener(this);
    }

    private void initData() {
        //模拟数据
        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
        entries.add(new PieEntry(40, "优秀"));
        entries.add(new PieEntry(20, "满分"));
        entries.add(new PieEntry(30, "及格"));
        entries.add(new PieEntry(10, "不及格"));

        //设置数据
        setData(entries);

        mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

        //设置每个tab比例块的显示位置
        Legend l = mPieChart.getLegend();//设置比例块
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setXEntrySpace(10f);//设置tab比例块之间X轴方向上的空白间距值(水平排列时)
        l.setYEntrySpace(0f);//设置tab比例块之间Y轴方向上的空白间距值(垂直排列时)
        l.setYOffset(10f);
        l.setFormSize(10f);//设置比例块大小
        l.setTextSize(12f);//设置比例块字体大小
        l.setForm(Legend.LegendForm.CIRCLE);//设置比例块图标形状,默认为方块
        l.setEnabled(true);//设置是否启用比例块,默认启用
//        l.setWordWrapEnabled(true);//设置比例块换行...

        // 输入标签样式
        mPieChart.setDrawEntryLabels(true);//设置是否绘制Label
        mPieChart.setEntryLabelColor(Color.WHITE);//设置绘制Label的颜色
        mPieChart.setEntryLabelTextSize(12f);//设置绘制Label的字体大小
    }


    //设置中间文字
    private SpannableString generateCenterSpannableText() {
        //原文:MPAndroidChart\ndeveloped by Philipp Jahoda
        SpannableString s = new SpannableString("设置中间标题\n\n设置属性控制文字样式");
        s.setSpan(new RelativeSizeSpan(1.7f), 0, 6, 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 setData(ArrayList<PieEntry> entries) {
        PieDataSet dataSet = new PieDataSet(entries, "三年级一班");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);

        //数据和颜色
        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());
        dataSet.setColors(colors);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);
        mPieChart.setData(data);
        mPieChart.highlightValues(null);
        //刷新
        mPieChart.invalidate();
        mPieChart.saveToGallery("chart01",100);
    }

    @Override
    public void onValueSelected(Entry e, Highlight h) {
        Log.i("Chart","e:"+e.toString()+" h:"+h.toString());
    }

    @Override
    public void onNothingSelected() {

    }
}`

三、资源下载:
Android Studio Demo
https://download.csdn.net/download/pillar1066527881/10357290

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值