一、效果展示:
二、用法:
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);//设置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