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);//再裏面顯示
    }
}

完成

發佈了20 篇原創文章 · 獲贊 6 · 訪問量 4904
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章