Skip to content

Commit

Permalink
Add radar layer custom color support
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoupeng05 authored and hannesa2 committed Mar 7, 2022
1 parent db1f18f commit dc917c5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
Expand Up @@ -29,6 +29,7 @@
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

import java.util.ArrayList;
import java.util.List;

public class RadarChartActivity extends DemoBase {

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

chart.setData(data);
List<Integer> colorList = new ArrayList<>();
colorList.add(Color.rgb(222, 166, 111));
colorList.add(Color.rgb(220, 206, 138));
colorList.add(Color.rgb(243, 255, 192));
colorList.add(Color.rgb(240, 255, 240));
colorList.add(Color.rgb(250, 255, 250));
chart.setLayerColorList(colorList);
chart.invalidate();
}

Expand Down
Expand Up @@ -16,6 +16,8 @@
import com.github.mikephil.charting.renderer.YAxisRendererRadarChart;
import com.github.mikephil.charting.utils.Utils;

import java.util.List;

/**
* Implementation of the RadarChart, a "spidernet"-like chart. It works best
* when displaying 5-10 entries per DataSet.
Expand Down Expand Up @@ -64,6 +66,8 @@ public class RadarChart extends PieRadarChartBase<RadarData> {
*/
private YAxis mYAxis;

private List<Integer> colorList;

protected YAxisRendererRadarChart mYAxisRenderer;
protected XAxisRendererRadarChart mXAxisRenderer;

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


public void setLayerColorList(List<Integer> colorList) {
if (colorList == null || colorList.size() == 0) {
return;
}
this.colorList = colorList;
}

public boolean isCustomLayerColorEnable() {
if (mData == null) {
return false;
}
return colorList != null && colorList.size() == getYAxis().mEntryCount;
}

public List<Integer> getLayerColorList() {
return colorList;
}

@Override
public int getIndexForAngle(float angle) {

Expand Down
Expand Up @@ -28,6 +28,12 @@ public class RadarChartRenderer extends LineRadarRenderer {
protected Paint mWebPaint;
protected Paint mHighlightCirclePaint;

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Path previousPath = new Path();
private Path innerArea = new Path();
private Path temp = new Path();


public RadarChartRenderer(RadarChart chart, ChartAnimator animator,
ViewPortHandler viewPortHandler) {
super(animator, viewPortHandler);
Expand All @@ -38,6 +44,10 @@ public RadarChartRenderer(RadarChart chart, ChartAnimator animator,
mHighlightPaint.setStrokeWidth(2f);
mHighlightPaint.setColor(Color.rgb(255, 187, 115));

paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(2f);
paint.setColor(Color.RED);

mWebPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mWebPaint.setStyle(Paint.Style.STROKE);

Expand Down Expand Up @@ -282,18 +292,40 @@ protected void drawWeb(Canvas c) {
MPPointF p1out = MPPointF.getInstance(0, 0);
MPPointF p2out = MPPointF.getInstance(0, 0);
for (int j = 0; j < labelCount; j++) {

if (mChart.isCustomLayerColorEnable()) {
innerArea.rewind();
paint.setColor(mChart.getLayerColorList().get(j));
}
for (int i = 0; i < mChart.getData().getEntryCount(); i++) {

float r = (mChart.getYAxis().mEntries[j] - mChart.getYChartMin()) * factor;

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

c.drawLine(p1out.x, p1out.y, p2out.x, p2out.y, mWebPaint);
if (mChart.isCustomLayerColorEnable()) {
if (p1out.x != p2out.x) {
if (i == 0) {
innerArea.moveTo(p1out.x, p1out.y);
} else {
innerArea.lineTo(p1out.x, p1out.y);
}
innerArea.lineTo(p2out.x, p2out.y);
}
}


}
if (mChart.isCustomLayerColorEnable()) {
temp.set(innerArea);
if (!innerArea.isEmpty()) {
boolean result = innerArea.op(previousPath, Path.Op.DIFFERENCE);
if (result) {
c.drawPath(innerArea, paint);
}
}
previousPath.set(temp);
}
}
MPPointF.recycleInstance(p1out);
MPPointF.recycleInstance(p2out);
Expand Down

0 comments on commit dc917c5

Please sign in to comment.