一、效果展示:
二、用法:
1、配置倉庫:工程路徑下面的build.gradle文件中引入 maven { url "https://jitpack.io" }
2、 引入jar包:
compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
3、XML中使用餅圖控件
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);//設(shè)置value是否用顯示百分?jǐn)?shù),默認(rèn)為false
mPieChart.getDescription().setEnabled(false);//設(shè)置描述
mPieChart.setExtraOffsets(5, 10, 5, 5);//設(shè)置餅狀圖距離上下左右的偏移量
mPieChart.setDragDecelerationFrictionCoef(0.95f);//設(shè)置阻尼系數(shù),范圍在[0,1]之間,越小餅狀圖轉(zhuǎn)動(dòng)越困難
//設(shè)置中間文件
mPieChart.setCenterText(generateCenterSpannableText());
// mPieChart.setCenterText("總學(xué)生數(shù):100人");
mPieChart.setDrawHoleEnabled(true);//是否繪制餅狀圖中間的圓
mPieChart.setHoleColor(Color.WHITE);//餅狀圖中間的圓的繪制顏色
mPieChart.setTransparentCircleColor(Color.WHITE);//設(shè)置圓環(huán)的顏色
mPieChart.setTransparentCircleAlpha(110);//設(shè)置圓環(huán)的透明度[0,255]
mPieChart.setHoleRadius(58f);//餅狀圖中間的圓的半徑大小
mPieChart.setTransparentCircleRadius(61f);//設(shè)置圓環(huán)的半徑值
mPieChart.setDrawCenterText(true);//是否繪制中間的文字
mPieChart.setRotationAngle(0);//設(shè)置餅狀圖旋轉(zhuǎn)的角度
// 觸摸旋轉(zhuǎn)
mPieChart.setRotationEnabled(true);;//設(shè)置餅狀圖是否可以旋轉(zhuǎn)(默認(rèn)為true)
mPieChart.setHighlightPerTapEnabled(true);//設(shè)置旋轉(zhuǎn)的時(shí)候點(diǎn)中的tab是否高亮(默認(rèn)為true)
//變化監(jiān)聽
mPieChart.setOnChartValueSelectedListener(this);
}
private void initData() {
//模擬數(shù)據(jù)
ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
entries.add(new PieEntry(40, "優(yōu)秀"));
entries.add(new PieEntry(20, "滿分"));
entries.add(new PieEntry(30, "及格"));
entries.add(new PieEntry(10, "不及格"));
//設(shè)置數(shù)據(jù)
setData(entries);
mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
//設(shè)置每個(gè)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);//設(shè)置tab之間Y軸方向上的空白間距值
l.setYOffset(0f);
// 輸入標(biāo)簽樣式
mPieChart.setDrawEntryLabels(true);//設(shè)置是否繪制Label
mPieChart.setEntryLabelColor(Color.WHITE);//設(shè)置繪制Label的顏色
mPieChart.setEntryLabelTextSize(12f);//設(shè)置繪制Label的字體大小
}
//設(shè)置中間文字
private SpannableString generateCenterSpannableText() {
//原文:MPAndroidChart\ndeveloped by Philipp Jahoda
SpannableString s = new SpannableString("設(shè)置中間標(biāo)題\n\n設(shè)置屬性控制文字樣式");
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;
}
//設(shè)置數(shù)據(jù)
private void setData(ArrayList<PieEntry> entries) {
PieDataSet dataSet = new PieDataSet(entries, "三年級(jí)一班");
dataSet.setSliceSpace(3f);
dataSet.setSelectionShift(5f);
//數(shù)據(jù)和顏色
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