MPAndroidChart餅圖用法系列一

一、效果展示:


01.png
02.png
03.png

二、用法:
1、配置倉庫:工程路徑下面的build.gradle文件中引入 maven { url "https://jitpack.io" }

04.png

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

3、XML中使用餅圖控件


05.png

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 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.setCenterText(generateCenterSpannableText());

// mPieChart.setCenterText("總學生數:100人");

mPieChart.setDrawHoleEnabled(true);//是否繪制餅狀圖中間的圓

mPieChart.setHoleColor(Color.WHITE);//餅狀圖中間的圓的繪制顏色

mPieChart.setTransparentCircleColor(Color.WHITE);//設置圓環的顏色

mPieChart.setTransparentCircleAlpha(110);//設置圓環的透明度[0,255]

mPieChart.setHoleRadius(58f);//餅狀圖中間的圓的半徑大小

mPieChart.setTransparentCircleRadius(61f);//設置圓環的半徑值

mPieChart.setDrawCenterText(true);//是否繪制中間的文字

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.TOP);

l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);

l.setOrientation(Legend.LegendOrientation.VERTICAL);

l.setDrawInside(false);

l.setXEntrySpace(0f);

l.setYEntrySpace(0f);//設置tab之間Y軸方向上的空白間距值

l.setYOffset(0f);

// 輸入標簽樣式

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();

}

@Override

public void onValueSelected(Entry e, Highlight h) {

}

@Override

public void onNothingSelected() {

}

}

資源下載:
Android Studio Demo
https://download.csdn.net/download/pillar1066527881/10357290

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。