Android MPAndroidChart PieChart

Android MPAndroidChart PieChart

继续上篇

https://blog.csdn.net/weixin_44889138/article/details/103498294
导入依赖,使用方式,可以参考上一篇

饼图
在这里插入图片描述

常用方法解释
setExtraOffsets()设置左,上,右,下的偏移量
setRotationEnabled()是否可以转动
setDrawHoleEnabled()中间是否是空的
getLegend()获得图例的描述
getDescription()获得图表的描述
getDescription()获得图表的描述

xml

<?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"
    tools:context=".MainActivity">

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

</LinearLayout>

使用

  1. setPieEntry
	private List<PieEntry> setPieEntry(){
        PieEntry pie1 = new PieEntry(71.33f,"有违章");
        PieEntry pie2 = new PieEntry(28.66f,"无违章");

        List<PieEntry> pieEntryList = new ArrayList<>();
        pieEntryList.add(pie1);pieEntryList.add(pie2);

        return pieEntryList;
    }
  1. setPieData
	private PieData setPieData(List<PieEntry> pieEntryList){
        PieDataSet set = new PieDataSet(pieEntryList,"");
        List<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#4A92FC"));
        colors.add(Color.parseColor("#ee6e55"));

        set.setColors(colors);//添加颜色
        set.setSliceSpace(3f);//切割空间
        set.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//值在图表外显示
        final String [] strings = {"28.6%", "71.4%"};//格式值
        set.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                String flag="";
                if((int) value%2==0){
                    flag="有违章:";
                }else{
                    flag="无违章:";
                }
                return flag+strings[(int) value%2];
            }
        });
        PieData pieData = new PieData(set);

        return pieData;
    }

一些PieDataSet常用的方法

常用方法解释
setColors()添加颜色
setSliceSpace()切割空间
setYValuePosition值在图表中哪显示
setValueFormatter格式化值
set.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//值在图表外显示(所占百分比)
  1. setDescription
	private void setDescription(){
        Description description = pieChart.getDescription();//获得图表的描述
        description.setText("平台有违章车辆和没违章车辆占比统计");
    }
  1. setLegend
	private void setLegend(){
        Legend legend = pieChart.getLegend();//获得图例的描述
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setTextSize(15f);
        legend.setFormSize(15f);
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setDrawInside(true);//再里面显示
    }

整体代码

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.YAxis;
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.ValueFormatter;

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

public class MainActivity extends AppCompatActivity {
    private PieChart pieChart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pieChart = findViewById(R.id.pie_chart);

        List<PieEntry> pieEntryList = setPieEntry();//设置PieEntry

        PieData pieData = setPieData(pieEntryList);//设置PieData

        setDescription();//设置图表的描述

        setLegend();//设置图例描述

        pieChart.setRotationEnabled(false);//禁止转动
        pieChart.setDrawHoleEnabled(false);//中间不留空洞
        pieChart.setUsePercentValues(true);
        pieChart.setDrawEntryLabels(false);//不使用label
        pieChart.setExtraOffsets(5f, 10f, 5f, 10f);//距离
        pieChart.setData(pieData);
        pieChart.invalidate();
    }

    /**
     * 设置PieEntry
     * @return
     */
    private List<PieEntry> setPieEntry(){
        PieEntry pie1 = new PieEntry(71.33f,"有违章");
        PieEntry pie2 = new PieEntry(28.66f,"无违章");

        List<PieEntry> pieEntryList = new ArrayList<>();
        pieEntryList.add(pie1);pieEntryList.add(pie2);

        return pieEntryList;
    }

    /**
     * 设置PieData
     * @param pieEntryList
     * @return
     */
    private PieData setPieData(List<PieEntry> pieEntryList){
        PieDataSet set = new PieDataSet(pieEntryList,"");
        List<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor("#4A92FC"));
        colors.add(Color.parseColor("#ee6e55"));

        set.setColors(colors);//添加颜色
        set.setSliceSpace(3f);//切割空间
        set.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//值在图表外显示
        final String [] strings = {"28.6%", "71.4%"};//格式值
        set.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                String flag="";
                if((int) value%2==0){
                    flag="有违章:";
                }else{
                    flag="无违章:";
                }
                return flag+strings[(int) value%2];
            }
        });
        PieData pieData = new PieData(set);

        return pieData;
    }

    /**
     * 设置图表的描述
     */
    private void setDescription(){
        Description description = pieChart.getDescription();//获得图表的描述
        description.setText("平台有违章车辆和没违章车辆占比统计");
    }

    /**
     * 设置图例描述
     */
    private void setLegend(){
        Legend legend = pieChart.getLegend();//获得图例的描述
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setTextSize(15f);
        legend.setFormSize(15f);
        legend.setForm(Legend.LegendForm.CIRCLE);
        legend.setDrawInside(true);//再里面显示
    }
}

完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值