MPAndroidChart 3.0以后绘制饼状图

本文介绍了如何在MPAndroidChart 3.0版本中绘制饼状图,包括添加依赖、XML布局使用PieChart、创建PieChart工具类以及在MainActivity中的调用。此外,还讲解了如何将百分比转换为具体数值,以及3.0版本相较于2.x版本在LineData参数上的变化。并提供了相关方法的详细解释链接。
摘要由CSDN通过智能技术生成

第一步:添加依赖

在app中的build.gradle中添加:

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
        google()
    }
}

 implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

 

第二步:activity_main.xml中使用PieChart

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/pie_chart"
        android:padding="20dp"
        android:layout_width="match_parent"
        android:layout_height="300dp"/>

</LinearLayout>

第三步:创建PieChart工具类(PieChartUtil)

import android.graphics.Color;

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.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.interfaces.datasets.IPieDataSet;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class PieChartUtil {
    //设置各区域颜色
    public  final int[]  PIE_COLORS={
            Color.rgb(181, 194, 202), Color.rgb(129, 216, 200)
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值