Skip to content

Commit dc917c5

Browse files
zhoupeng05hannesa2
zhoupeng05
authored andcommitted
Add radar layer custom color support
PhilJay#5273
1 parent db1f18f commit dc917c5

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/RadarChartActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
3030

3131
import java.util.ArrayList;
32+
import java.util.List;
3233

3334
public class RadarChartActivity extends DemoBase {
3435

@@ -147,6 +148,13 @@ private void setData() {
147148
data.setValueTextColor(Color.WHITE);
148149

149150
chart.setData(data);
151+
List<Integer> colorList = new ArrayList<>();
152+
colorList.add(Color.rgb(222, 166, 111));
153+
colorList.add(Color.rgb(220, 206, 138));
154+
colorList.add(Color.rgb(243, 255, 192));
155+
colorList.add(Color.rgb(240, 255, 240));
156+
colorList.add(Color.rgb(250, 255, 250));
157+
chart.setLayerColorList(colorList);
150158
chart.invalidate();
151159
}
152160

MPChartLib/src/main/java/com/github/mikephil/charting/charts/RadarChart.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import com.github.mikephil.charting.renderer.YAxisRendererRadarChart;
1717
import com.github.mikephil.charting.utils.Utils;
1818

19+
import java.util.List;
20+
1921
/**
2022
* Implementation of the RadarChart, a "spidernet"-like chart. It works best
2123
* when displaying 5-10 entries per DataSet.
@@ -64,6 +66,8 @@ public class RadarChart extends PieRadarChartBase<RadarData> {
6466
*/
6567
private YAxis mYAxis;
6668

69+
private List<Integer> colorList;
70+
6771
protected YAxisRendererRadarChart mYAxisRenderer;
6872
protected XAxisRendererRadarChart mXAxisRenderer;
6973

@@ -179,6 +183,25 @@ public float getSliceAngle() {
179183
return 360f / (float) mData.getMaxEntryCountSet().getEntryCount();
180184
}
181185

186+
187+
public void setLayerColorList(List<Integer> colorList) {
188+
if (colorList == null || colorList.size() == 0) {
189+
return;
190+
}
191+
this.colorList = colorList;
192+
}
193+
194+
public boolean isCustomLayerColorEnable() {
195+
if (mData == null) {
196+
return false;
197+
}
198+
return colorList != null && colorList.size() == getYAxis().mEntryCount;
199+
}
200+
201+
public List<Integer> getLayerColorList() {
202+
return colorList;
203+
}
204+
182205
@Override
183206
public int getIndexForAngle(float angle) {
184207

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public class RadarChartRenderer extends LineRadarRenderer {
2828
protected Paint mWebPaint;
2929
protected Paint mHighlightCirclePaint;
3030

31+
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
32+
private Path previousPath = new Path();
33+
private Path innerArea = new Path();
34+
private Path temp = new Path();
35+
36+
3137
public RadarChartRenderer(RadarChart chart, ChartAnimator animator,
3238
ViewPortHandler viewPortHandler) {
3339
super(animator, viewPortHandler);
@@ -38,6 +44,10 @@ public RadarChartRenderer(RadarChart chart, ChartAnimator animator,
3844
mHighlightPaint.setStrokeWidth(2f);
3945
mHighlightPaint.setColor(Color.rgb(255, 187, 115));
4046

47+
paint.setStyle(Paint.Style.FILL);
48+
paint.setStrokeWidth(2f);
49+
paint.setColor(Color.RED);
50+
4151
mWebPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
4252
mWebPaint.setStyle(Paint.Style.STROKE);
4353

@@ -282,18 +292,40 @@ protected void drawWeb(Canvas c) {
282292
MPPointF p1out = MPPointF.getInstance(0, 0);
283293
MPPointF p2out = MPPointF.getInstance(0, 0);
284294
for (int j = 0; j < labelCount; j++) {
285-
295+
if (mChart.isCustomLayerColorEnable()) {
296+
innerArea.rewind();
297+
paint.setColor(mChart.getLayerColorList().get(j));
298+
}
286299
for (int i = 0; i < mChart.getData().getEntryCount(); i++) {
287-
288300
float r = (mChart.getYAxis().mEntries[j] - mChart.getYChartMin()) * factor;
289301

290302
Utils.getPosition(center, r, sliceangle * i + rotationangle, p1out);
291303
Utils.getPosition(center, r, sliceangle * (i + 1) + rotationangle, p2out);
292304

293305
c.drawLine(p1out.x, p1out.y, p2out.x, p2out.y, mWebPaint);
306+
if (mChart.isCustomLayerColorEnable()) {
307+
if (p1out.x != p2out.x) {
308+
if (i == 0) {
309+
innerArea.moveTo(p1out.x, p1out.y);
310+
} else {
311+
innerArea.lineTo(p1out.x, p1out.y);
312+
}
313+
innerArea.lineTo(p2out.x, p2out.y);
314+
}
315+
}
294316

295317

296318
}
319+
if (mChart.isCustomLayerColorEnable()) {
320+
temp.set(innerArea);
321+
if (!innerArea.isEmpty()) {
322+
boolean result = innerArea.op(previousPath, Path.Op.DIFFERENCE);
323+
if (result) {
324+
c.drawPath(innerArea, paint);
325+
}
326+
}
327+
previousPath.set(temp);
328+
}
297329
}
298330
MPPointF.recycleInstance(p1out);
299331
MPPointF.recycleInstance(p2out);

0 commit comments

Comments
 (0)