MPAndroidChart饼图和圆环详细使用(一)

利用第三方框架MPAndroidChart,制作圆形和圆环图标

这里写图片描述

1.添加依赖,建议下载lib包,进行依赖

https://github.com/PhilJay/MPAndroidChart

2.填写布局文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:paddingLeft="10dp">

            <TextView
                android:layout_width="2dp"
                android:layout_height="13dp"
                android:background="#4a6def"
                android:paddingLeft="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="11dp"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:text="民族分布"
                android:textColor="#222222"
                android:textSize="14sp" />
        </LinearLayout>

        <com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pie_chat1"
            android:layout_width="match_parent"
            android:layout_height="250dp"
             />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:paddingLeft="10dp">

            <TextView
                android:layout_width="2dp"
                android:layout_height="13dp"
                android:background="#4a6def"
                android:paddingLeft="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="11dp"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:text="学历分布"
                android:textColor="#222222"
                android:textSize="14sp" />
        </LinearLayout>

        <com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pie_chat2"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            />
    </LinearLayout>
</LinearLayout>
</ScrollView>

3. 初始化控件,并设置数据

package com.phone.mpandroid;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieEntry;

import java.util.ArrayList;
import java.util.List;

public class HodleCricle extends AppCompatActivity {

    private PieChart pieChart,pie_chat2;

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

        initView();
    }

    private void initView() {
        pieChart = (PieChart) findViewById(R.id.pie_chat1);
        pie_chat2= (PieChart) findViewById(R.id.pie_chat2);
        showhodlePieChart();

        showRingPieChart();
    }

    private void showRingPieChart() {
//设置每份所占数量
        List<PieEntry> yvals = new ArrayList<>();
        yvals.add(new PieEntry(2.0f, "本科"));
        yvals.add(new PieEntry(3.0f, "硕士"));
        yvals.add(new PieEntry(4.0f, "博士"));
        yvals.add(new PieEntry(5.0f, "大专"));
        yvals.add(new PieEntry(1.0f, "其他"));
// 设置每份的颜色
        List<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#6785f2"));
        colors.add(Color.parseColor("#675cf2"));
        colors.add(Color.parseColor("#496cef"));
        colors.add(Color.parseColor("#aa63fa"));
        colors.add(Color.parseColor("#f5a658"));
        PieChartManagger pieChartManagger=new PieChartManagger(pie_chat2);
        pieChartManagger.showRingPieChart(yvals,colors);
    }

    private void showhodlePieChart() {
        // 设置每份所占数量
        List<PieEntry> yvals = new ArrayList<>();
        yvals.add(new PieEntry(2.0f, "汉族"));
        yvals.add(new PieEntry(3.0f, "回族"));
        yvals.add(new PieEntry(4.0f, "壮族"));
        yvals.add(new PieEntry(5.0f, "维吾尔族"));
        yvals.add(new PieEntry(6.0f, "土家族"));
        //设置每份的颜色
        List<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#6785f2"));
        colors.add(Color.parseColor("#675cf2"));
        colors.add(Color.parseColor("#496cef"));
        colors.add(Color.parseColor("#aa63fa"));
        colors.add(Color.parseColor("#58a9f5"));

        PieChartManagger pieChartManagger=new PieChartManagger(pieChart);
        pieChartManagger.showSolidPieChart(yvals,colors);
    }
}

4.编写显示圆形圆环的的管理类

package com.phone.mpandroid;

import android.graphics.Color;
import android.graphics.Typeface;

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.formatter.PercentFormatter;

import java.util.List;

/**
 * Created by zhang on 2018/7/5.
 */

public class PieChartManagger {

    public PieChart pieChart;

    public PieChartManagger(PieChart pieChart) {
        this.pieChart = pieChart;
        initPieChart();
    }

    //初始化
    private void initPieChart() {
        //  是否显示中间的洞
        pieChart.setDrawHoleEnabled(false);
        pieChart.setHoleRadius(40f);//设置中间洞的大小
        // 半透明圈
        pieChart.setTransparentCircleRadius(30f);
        pieChart.setTransparentCircleColor(Color.WHITE); //设置半透明圆圈的颜色
        pieChart.setTransparentCircleAlpha(125); //设置半透明圆圈的透明度

        //饼状图中间可以添加文字
        pieChart.setDrawCenterText(false);
        pieChart.setCenterText("民族"); //设置中间文字
        pieChart.setCenterTextColor(Color.parseColor("#a1a1a1")); //中间问题的颜色
        pieChart.setCenterTextSizePixels(36);  //中间文字的大小px
        pieChart.setCenterTextRadiusPercent(1f);
        pieChart.setCenterTextTypeface(Typeface.DEFAULT); //中间文字的样式
        pieChart.setCenterTextOffset(0, 0); //中间文字的偏移量


        pieChart.setRotationAngle(0);// 初始旋转角度
        pieChart.setRotationEnabled(true);// 可以手动旋转
        pieChart.setUsePercentValues(true);//显示成百分比
        pieChart.getDescription().setEnabled(false); //取消右下角描述

        //是否显示每个部分的文字描述
        pieChart.setDrawEntryLabels(false);
        pieChart.setEntryLabelColor(Color.RED); //描述文字的颜色
        pieChart.setEntryLabelTextSize(14);//描述文字的大小
        pieChart.setEntryLabelTypeface(Typeface.DEFAULT_BOLD); //描述文字的样式

        //图相对于上下左右的偏移
        pieChart.setExtraOffsets(20, 8, 75, 8);
        //图标的背景色
        pieChart.setBackgroundColor(Color.TRANSPARENT);
//        设置pieChart图表转动阻力摩擦系数[0,1]
        pieChart.setDragDecelerationFrictionCoef(0.75f);

        //获取图例
        Legend legend = pieChart.getLegend();
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);  //设置图例水平显示
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); //顶部
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); //右对其

        legend.setXEntrySpace(7f);//x轴的间距
        legend.setYEntrySpace(10f); //y轴的间距
        legend.setYOffset(10f);  //图例的y偏移量
        legend.setXOffset(10f);  //图例x的偏移量
        legend.setTextColor(Color.parseColor("#a1a1a1")); //图例文字的颜色
        legend.setTextSize(13);  //图例文字的大小

    }


    /**
     * 显示实心圆
     * @param yvals
     * @param colors
     */
    public void showSolidPieChart(List<PieEntry> yvals, List<Integer> colors) {
        //数据集合
        PieDataSet dataset = new PieDataSet(yvals, "");
        //填充每个区域的颜色
        dataset.setColors(colors);
        //是否在图上显示数值
        dataset.setDrawValues(true);
//        文字的大小
        dataset.setValueTextSize(14);
//        文字的颜色
        dataset.setValueTextColor(Color.RED);
//        文字的样式
        dataset.setValueTypeface(Typeface.DEFAULT_BOLD);

//      当值位置为外边线时,表示线的前半段长度。
        dataset.setValueLinePart1Length(0.4f);
//      当值位置为外边线时,表示线的后半段长度。
        dataset.setValueLinePart2Length(0.4f);
//      当ValuePosits为OutsiDice时,指示偏移为切片大小的百分比
        dataset.setValueLinePart1OffsetPercentage(80f);
        // 当值位置为外边线时,表示线的颜色。
        dataset.setValueLineColor(Color.parseColor("#a1a1a1"));
//        设置Y值的位置是在圆内还是圆外
        dataset.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
//        设置Y轴描述线和填充区域的颜色一致
        dataset.setUsingSliceColorAsValueLineColor(false);
//        设置每条之前的间隙
        dataset.setSliceSpace(0);

        //设置饼状Item被选中时变化的距离
        dataset.setSelectionShift(5f);
        //填充数据
        PieData pieData = new PieData(dataset);
//        格式化显示的数据为%百分比
        pieData.setValueFormatter(new PercentFormatter());
//        显示试图
        pieChart.setData(pieData);
    }


    /**
     * 显示圆环
     * @param yvals
     * @param colors
     */
    public void  showRingPieChart(List<PieEntry> yvals, List<Integer> colors){
        //显示为圆环
        pieChart.setDrawHoleEnabled(true);
        pieChart.setHoleRadius(85f);//设置中间洞的大小

        //数据集合
        PieDataSet dataset = new PieDataSet(yvals, "");
        //填充每个区域的颜色
        dataset.setColors(colors);
        //是否在图上显示数值
        dataset.setDrawValues(true);
//        文字的大小
        dataset.setValueTextSize(14);
//        文字的颜色
        dataset.setValueTextColor(Color.RED);
//        文字的样式
        dataset.setValueTypeface(Typeface.DEFAULT_BOLD);

//      当值位置为外边线时,表示线的前半段长度。
        dataset.setValueLinePart1Length(0.4f);
//      当值位置为外边线时,表示线的后半段长度。
        dataset.setValueLinePart2Length(0.4f);
//      当ValuePosits为OutsiDice时,指示偏移为切片大小的百分比
        dataset.setValueLinePart1OffsetPercentage(80f);
        // 当值位置为外边线时,表示线的颜色。
        dataset.setValueLineColor(Color.parseColor("#a1a1a1"));
//        设置Y值的位置是在圆内还是圆外
        dataset.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
//        设置Y轴描述线和填充区域的颜色一致
        dataset.setUsingSliceColorAsValueLineColor(false);
//        设置每条之前的间隙
        dataset.setSliceSpace(0);

        //设置饼状Item被选中时变化的距离
        dataset.setSelectionShift(5f);
        //填充数据
        PieData pieData = new PieData(dataset);
//        格式化显示的数据为%百分比
        pieData.setValueFormatter(new PercentFormatter());
//        显示试图
        pieChart.setData(pieData);

    }


}
  • 9
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
使用 MPAndroidChart 绘制饼图,你需要进行以下步骤: 1.在你的项目中添加 MPAndroidChart 依赖。你可以在 gradle 文件中添加以下代码: ``` implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' ``` 2.在你的布局文件中添加一个 PieChart 视图: ``` <com.github.mikephil.charting.charts.PieChart android:id="@+id/pie_chart" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` 3.在你的 Activity 或 Fragment 中找到这个 PieChart 视图,并为它设置一些属性: ``` PieChart pieChart = findViewById(R.id.pie_chart); pieChart.setUsePercentValues(true); // 设置使用百分比值 pieChart.getDescription().setEnabled(false); // 隐藏描述 pieChart.setExtraOffsets(5, 10, 5, 5); // 设置间距 pieChart.setDrawHoleEnabled(true); // 绘制中间的空心圆 pieChart.setHoleColor(Color.WHITE); // 设置空心圆的颜色 pieChart.setTransparentCircleRadius(61f); // 设置透明圆的半径 pieChart.setHoleRadius(58f); // 设置空心圆的半径 pieChart.setRotationAngle(0); // 设置起始角度 pieChart.setRotationEnabled(true); // 可以旋转 pieChart.setHighlightPerTapEnabled(true); // 可以高亮显示 ``` 4.为饼图添加数据。首先创建一个 ArrayList<PieEntry>,然后将每个数据添加到其中: ``` ArrayList<PieEntry> entries = new ArrayList<>(); entries.add(new PieEntry(18.5f, "Green")); entries.add(new PieEntry(26.7f, "Yellow")); entries.add(new PieEntry(24.0f, "Red")); entries.add(new PieEntry(30.8f, "Blue")); ``` 5.将数据添加到 PieDataSet 中,并为数据集设置一些属性: ``` PieDataSet dataSet = new PieDataSet(entries, "Election Results"); dataSet.setSliceSpace(3f); // 设置每个扇形之间的间距 dataSet.setSelectionShift(5f); // 设置点击时的扇形半径 dataSet.setColors(ColorTemplate.JOYFUL_COLORS); // 设置颜色 ``` 6.创建一个 PieData 对象,将数据集传递给它: ``` PieData data = new PieData(dataSet); data.setValueTextSize(10f); // 设置文字大小 data.setValueTextColor(Color.YELLOW); // 设置文字颜色 ``` 7.将 PieData 对象设置到 PieChart 中: ``` pieChart.setData(data); pieChart.invalidate(); // 刷新图表 ``` 现在,你就可以显示你的饼图了。上面的代码只是一个简单的示例,你可以根据自己的需求调整图表的属性和样式。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值