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 #5273

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions .idea/runConfigurations/MPChartExample.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
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 @@ -70,6 +80,7 @@ public void drawData(Canvas c) {
}

protected Path mDrawDataSetSurfacePathBuffer = new Path();

/**
* Draws the RadarDataSet
*
Expand All @@ -89,7 +100,7 @@ protected void drawDataSet(Canvas c, IRadarDataSet dataSet, int mostEntries) {
float factor = mChart.getFactor();

MPPointF center = mChart.getCenterOffsets();
MPPointF pOut = MPPointF.getInstance(0,0);
MPPointF pOut = MPPointF.getInstance(0, 0);
Path surface = mDrawDataSetSurfacePathBuffer;
surface.reset();

Expand Down Expand Up @@ -159,8 +170,8 @@ public void drawValues(Canvas c) {
float factor = mChart.getFactor();

MPPointF center = mChart.getCenterOffsets();
MPPointF pOut = MPPointF.getInstance(0,0);
MPPointF pIcon = MPPointF.getInstance(0,0);
MPPointF pOut = MPPointF.getInstance(0, 0);
MPPointF pIcon = MPPointF.getInstance(0, 0);

float yoffset = Utils.convertDpToPixel(5f);

Expand All @@ -182,11 +193,11 @@ public void drawValues(Canvas c) {

RadarEntry entry = dataSet.getEntryForIndex(j);

Utils.getPosition(
center,
(entry.getY() - mChart.getYChartMin()) * factor * phaseY,
sliceangle * j * phaseX + mChart.getRotationAngle(),
pOut);
Utils.getPosition(
center,
(entry.getY() - mChart.getYChartMin()) * factor * phaseY,
sliceangle * j * phaseX + mChart.getRotationAngle(),
pOut);

if (dataSet.isDrawValuesEnabled()) {
drawValue(c,
Expand Down Expand Up @@ -216,8 +227,8 @@ public void drawValues(Canvas c) {
Utils.drawImage(
c,
icon,
(int)pIcon.x,
(int)pIcon.y,
(int) pIcon.x,
(int) pIcon.y,
icon.getIntrinsicWidth(),
icon.getIntrinsicHeight());
}
Expand Down Expand Up @@ -255,7 +266,7 @@ protected void drawWeb(Canvas c) {
final int xIncrements = 1 + mChart.getSkipWebLineCount();
int maxEntryCount = mChart.getData().getMaxEntryCountSet().getEntryCount();

MPPointF p = MPPointF.getInstance(0,0);
MPPointF p = MPPointF.getInstance(0, 0);
for (int i = 0; i < maxEntryCount; i += xIncrements) {

Utils.getPosition(
Expand All @@ -275,21 +286,44 @@ protected void drawWeb(Canvas c) {

int labelCount = mChart.getYAxis().mEntryCount;

MPPointF p1out = MPPointF.getInstance(0,0);
MPPointF p2out = MPPointF.getInstance(0,0);
for (int j = 0; j < labelCount; j++) {
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes with this lint error
image

image

I solved and merged it in my updated and maintained fork

if (result) {
c.drawPath(innerArea, paint);
}
}
previousPath.set(temp);
}
}
MPPointF.recycleInstance(p1out);
MPPointF.recycleInstance(p2out);
Expand All @@ -305,7 +339,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
float factor = mChart.getFactor();

MPPointF center = mChart.getCenterOffsets();
MPPointF pOut = MPPointF.getInstance(0,0);
MPPointF pOut = MPPointF.getInstance(0, 0);

RadarData radarData = mChart.getData();

Expand Down Expand Up @@ -362,6 +396,7 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
}

protected Path mDrawHighlightCirclePathBuffer = new Path();

public void drawHighlightCircle(Canvas c,
MPPointF point,
float innerRadius,
Expand Down