效果展示
设计图

最终实现

要实现阴影效果需要自定义PieChartRenderer
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IPieDataSet;
import com.github.mikephil.charting.renderer.PieChartRenderer;
import com.github.mikephil.charting.utils.ViewPortHandler;
public class CustomPieChartRenderer extends PieChartRenderer {
public CustomPieChartRenderer(PieChart chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
super(chart, animator, viewPortHandler);
}
@Override
protected void drawDataSet(Canvas c, IPieDataSet dataSet) {
c.save();
float rotationAngle = mChart.getRotationAngle();
float radius = mChart.getRadius();
int entryCount = dataSet.getEntryCount();
float totalAngle = 360f;
float totalValue = 0f;
for (int i = 0; i < entryCount; i++) {
totalValue += dataSet.getEntryForIndex(i).getValue();
}
float currentStartAngle = rotationAngle;
for (int i = 0; i < entryCount; i++) {
PieEntry entry = dataSet.getEntryForIndex(i);
int pieColor = dataSet.getColor(i);
Paint renderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint highlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
int lighterShadowColor = lightenColor(pieColor, 0.1f);
renderPaint.setColor(pieColor);
renderPaint.setShadowLayer(40f, 0f, 0f, lighterShadowColor);
float sliceAngle = totalAngle * (entry.getValue() / totalValue);
if (isEntryHighlighted(i)) {
highlightPaint.setColor(pieColor);
highlightPaint.setShadowLayer(60f, 0f, 0f, lightenColor(pieColor, 0.1f));
c.drawArc(
mChart.getCircleBox(),
currentStartAngle,
sliceAngle,
true,
highlightPaint
);
} else {
c.drawArc(
mChart.getCircleBox(),
currentStartAngle,
sliceAngle,
true,
renderPaint
);
}
currentStartAngle += sliceAngle;
}
c.restore();
}
private boolean isEntryHighlighted(int index) {
Highlight[] highlights = mChart.getHighlighted();
if (highlights != null && highlights.length > 0