Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add radar layer custom color support #52

Merged
merged 2 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MPChartExample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
compileSdkVersion 31
defaultConfig {
applicationId "com.xxmassdeveloper.mpchartexample"
minSdkVersion 16
minSdkVersion 19
targetSdkVersion 31
versionCode 57
versionName '3.1.0'
Expand Down
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion MPChartLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
android {
compileSdkVersion 31
defaultConfig {
minSdkVersion 14
minSdkVersion 19
targetSdkVersion 31

consumerProguardFiles 'proguard-project.txt'
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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