一、设置权限
allprojects {
repositories {
maven {
url "https://jitpack.io" }
}
}
dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'}二、初始化图表
private void initChart() {
//饼状图
pieChart.setUsePercentValues(
true)
;//设置为TRUE的话,图标中的数据自动变为percent
pieChart.getDescription().setEnabled(
false)
;
pieChart.setExtraOffsets(
5, 10, 5, 5)
;//设置额外的偏移量(在图表视图周围)
pieChart.setDragDecelerationFrictionCoef(
0.95f)
;//设置滑动减速摩擦系数,在0~1之间
//设置中间文件
// pieChart.setCenterText(generateCenterSpannableText());
pieChart.setDrawSliceText(
false)
;//设置隐藏饼图上文字,只显示百分比
pieChart.setDrawHoleEnabled(
false)
;//设置为TRUE时,饼中心透明
pieChart.setHoleColor(Color.
WHITE)
;//设置饼中心颜色
pieChart.setTransparentCircleColor(Color.
WHITE)
;//透明的圆
pieChart.setTransparentCircleAlpha(
110)
;//透明度
//pieChart.setHoleRadius(58f);//中间圆的半径占总半径的百分数
pieChart.setHoleRadius(
0)
;//实心圆
//pieChart.setTransparentCircleRadius(61f); 半透明圈
pieChart.setDrawCenterText(
true)
;//绘制显示在饼图中心的文本
pieChart.setRotationAngle(
0)
;//设置一个抵消RadarChart的旋转度
// 触摸旋转
pieChart.setRotationEnabled(
true)
;//通过触摸使图表旋转
pieChart.setHighlightPerTapEnabled(
true)
;//通过点击手势突出显示的值
//变化监听
// pieChart.setOnChartValueSelectedListener(this);
//模拟数据
ArrayList<PieEntry> entries =
new ArrayList<PieEntry>()
;
entries.add(
new PieEntry(
40, "未完成"))
;
entries.add(
new PieEntry(
10, "进行中"))
;
entries.add(
new PieEntry(
20, "进行中(超期)"))
;
entries.add(
new PieEntry(
15, "已完成"))
;
entries.add(
new PieEntry(
10, "以往成(超期)"))
;
entries.add(
new PieEntry(
5, "已终止"))
;
//设置数据
setData(entries)
;
pieChart.animateY(
1400, Easing.EasingOption.
EaseInOutQuad)
;
Legend l =
pieChart.getLegend()
;
l.setVerticalAlignment(Legend.LegendVerticalAlignment.
TOP)
;
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.
RIGHT)
;
l.setOrientation(Legend.LegendOrientation.
VERTICAL)
;
l.setDrawInside(
false)
;
l.setXEntrySpace(
7f)
;
l.setYEntrySpace(
0f)
;
l.setYOffset(
0f)
;
// 输入标签样式
pieChart.setEntryLabelColor(Color.
WHITE)
;
pieChart.setEntryLabelTextSize(
12f)
;
/**
* 设置比例图
*/
Legend mLegend =
pieChart.getLegend()
;
mLegend.setPosition(Legend.LegendPosition.
LEFT_OF_CHART_CENTER)
; //在左边中间显示比例图
mLegend.setFormSize(
14f)
;//比例块字体大小
mLegend.setXEntrySpace(
4f)
;//设置距离饼图的距离,防止与饼图重合
mLegend.setYEntrySpace(
4f)
;
//设置比例块换行...
mLegend.setWordWrapEnabled(
true)
;
mLegend.setDirection(Legend.LegendDirection.
LEFT_TO_RIGHT)
;//设置字跟图表的左右顺序
//mLegend.setTextColor(getResources().getColor(R.color.alpha_80));
mLegend.setForm(Legend.LegendForm.
SQUARE)
;//设置比例块形状,默认为方块
// mLegend.setEnabled(false);//设置禁用比例块
}
//设置中间文字
// private SpannableString generateCenterSpannableText() {
// //原文:MPAndroidChart\ndeveloped by Philipp Jahoda
// SpannableString s = new SpannableString("项目");
// //s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 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(
2f)
;//饼图区块之间的距离
dataSet.setSelectionShift(
5f)
;//
//数据和颜色
Integer[] colors=
new Integer[]{Color.
parseColor(
"#d87a80")
, Color.
parseColor(
"#2ec7c9")
, Color.
parseColor(
"#b6a2de")
,
Color.
parseColor(
"#5ab1ef")
, Color.
parseColor(
"#ffb980")
, Color.
parseColor(
"#8d98b3")}
;
//添加对应的颜色值
List<Integer> colorSum =
new ArrayList<>()
;
for (Integer color : colors) {
colorSum.add(color)
;
}
dataSet.setColors(colorSum)
;
PieData data =
new PieData(dataSet)
;
data.setValueFormatter(
new PercentFormatter())
;
data.setValueTextSize(
11f)
;
data.setValueTextColor(Color.
BLACK)
;
pieChart.setData(data)
;
pieChart.highlightValues(
null)
;//在给定的数据集中突出显示给定索引的值
//刷新
pieChart.invalidate()
;
}