From 55a2b2e5fda7287683b4d537c84eb5cac1599089 Mon Sep 17 00:00:00 2001 From: TheExecutor Date: Wed, 12 Jan 2022 09:34:52 +0100 Subject: [PATCH 1/3] removed dup code from Shape classes --- .idea/runConfigurations/MPChartExample.xml | 14 +++-- .../renderer/scatter/CircleShapeRenderer.java | 32 ++++++------ .../charting/renderer/scatter/Shape.java | 52 +++++++++++++++++++ .../renderer/scatter/SquareShapeRenderer.java | 50 ++++++++++-------- 4 files changed, 106 insertions(+), 42 deletions(-) create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/Shape.java diff --git a/.idea/runConfigurations/MPChartExample.xml b/.idea/runConfigurations/MPChartExample.xml index e6bcf50331..ef44d74145 100644 --- a/.idea/runConfigurations/MPChartExample.xml +++ b/.idea/runConfigurations/MPChartExample.xml @@ -1,10 +1,13 @@ - + \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java index ac7abb92de..6d96925b9d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java @@ -19,33 +19,35 @@ public class CircleShapeRenderer implements IShapeRenderer public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, float posX, float posY, Paint renderPaint) { - final float shapeSize = dataSet.getScatterShapeSize(); - final float shapeHalf = shapeSize / 2f; - final float shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); - final float shapeHoleSize = shapeHoleSizeHalf * 2.f; - final float shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; - final float shapeStrokeSizeHalf = shapeStrokeSize / 2.f; + final Shape shape = new Shape(dataSet); + /** + final float shapeSize = dataSet.getScatterShapeSize(); + final float shapeHalf = shapeSize / 2f; + final float shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); + final float shapeHoleSize = shapeHoleSizeHalf * 2.f; + final float shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; + final float shapeStrokeSizeHalf = shapeStrokeSize / 2.f; - final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); - - if (shapeSize > 0.0) { + final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); + **/ + if (shape.getShapeSize() > 0.0) { renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(shapeStrokeSize); + renderPaint.setStrokeWidth(shape.getShapeStrokeSize()); c.drawCircle( posX, posY, - shapeHoleSizeHalf + shapeStrokeSizeHalf, + shape.getShapeHoleSizeHalf() + shape.getShapeStrokeSizeHalf(), renderPaint); - if (shapeHoleColor != ColorTemplate.COLOR_NONE) { + if (shape.getShapeHoleColor() != ColorTemplate.COLOR_NONE) { renderPaint.setStyle(Paint.Style.FILL); - renderPaint.setColor(shapeHoleColor); + renderPaint.setColor(shape.getShapeHoleColor()); c.drawCircle( posX, posY, - shapeHoleSizeHalf, + shape.getShapeHoleSizeHalf(), renderPaint); } } else { @@ -54,7 +56,7 @@ public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewP c.drawCircle( posX, posY, - shapeHalf, + shape.getShapeHalf(), renderPaint); } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/Shape.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/Shape.java new file mode 100644 index 0000000000..5686963c49 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/Shape.java @@ -0,0 +1,52 @@ +package com.github.mikephil.charting.renderer.scatter; + +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; +import com.github.mikephil.charting.utils.Utils; + +public class Shape { + private float shapeSize; + private float shapeHalf; + private float shapeHoleSizeHalf; + private float shapeHoleSize; + private float shapeStrokeSize; + private float shapeStrokeSizeHalf; + private int shapeHoleColor; + + public Shape(IScatterDataSet dataSet) { + this.shapeSize = dataSet.getScatterShapeSize(); + this.shapeHalf = shapeSize / 2f; + this.shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); + this.shapeHoleSize = shapeHoleSizeHalf * 2.f; + this.shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; + this.shapeStrokeSizeHalf = shapeStrokeSize / 2.f; + this.shapeHoleColor = dataSet.getScatterShapeHoleColor(); + } + + public float getShapeSize() { + return shapeSize; + } + + public float getShapeHalf() { + return shapeHalf; + } + + public float getShapeHoleSizeHalf() { + return shapeHoleSizeHalf; + } + + public float getShapeHoleSize() { + return shapeHoleSize; + } + + public float getShapeStrokeSize() { + return shapeStrokeSize; + } + + public float getShapeStrokeSizeHalf() { + return shapeStrokeSizeHalf; + } + + public int getShapeHoleColor() { + return shapeHoleColor; + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java index ac98679233..c3522f4e59 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java @@ -20,43 +20,47 @@ public class SquareShapeRenderer implements IShapeRenderer public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, float posX, float posY, Paint renderPaint) { - final float shapeSize = dataSet.getScatterShapeSize(); - final float shapeHalf = shapeSize / 2f; - final float shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); - final float shapeHoleSize = shapeHoleSizeHalf * 2.f; - final float shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; - final float shapeStrokeSizeHalf = shapeStrokeSize / 2.f; + final Shape shape = new Shape(dataSet); - final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); + /** + final float shapeSize = dataSet.getScatterShapeSize(); + final float shapeHalf = shapeSize / 2f; + final float shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); + final float shapeHoleSize = shapeHoleSizeHalf * 2.f; + final float shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; + final float shapeStrokeSizeHalf = shapeStrokeSize / 2.f; - if (shapeSize > 0.0) { + final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); + **/ + + if (shape.getShapeSize() > 0.0) { renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(shapeStrokeSize); + renderPaint.setStrokeWidth(shape.getShapeStrokeSize()); - c.drawRect(posX - shapeHoleSizeHalf - shapeStrokeSizeHalf, - posY - shapeHoleSizeHalf - shapeStrokeSizeHalf, - posX + shapeHoleSizeHalf + shapeStrokeSizeHalf, - posY + shapeHoleSizeHalf + shapeStrokeSizeHalf, + c.drawRect(posX - shape.getShapeHoleSizeHalf() - shape.getShapeStrokeSizeHalf(), + posY - shape.getShapeHoleSizeHalf() - shape.getShapeStrokeSizeHalf(), + posX + shape.getShapeHoleSizeHalf() + shape.getShapeStrokeSizeHalf(), + posY + shape.getShapeHoleSizeHalf() + shape.getShapeStrokeSizeHalf(), renderPaint); - if (shapeHoleColor != ColorTemplate.COLOR_NONE) { + if (shape.getShapeHoleColor() != ColorTemplate.COLOR_NONE) { renderPaint.setStyle(Paint.Style.FILL); - renderPaint.setColor(shapeHoleColor); - c.drawRect(posX - shapeHoleSizeHalf, - posY - shapeHoleSizeHalf, - posX + shapeHoleSizeHalf, - posY + shapeHoleSizeHalf, + renderPaint.setColor(shape.getShapeHoleColor()); + c.drawRect(posX - shape.getShapeHoleSizeHalf(), + posY - shape.getShapeHoleSizeHalf(), + posX + shape.getShapeHoleSizeHalf(), + posY + shape.getShapeHoleSizeHalf(), renderPaint); } } else { renderPaint.setStyle(Paint.Style.FILL); - c.drawRect(posX - shapeHalf, - posY - shapeHalf, - posX + shapeHalf, - posY + shapeHalf, + c.drawRect(posX - shape.getShapeHalf(), + posY - shape.getShapeHalf(), + posX + shape.getShapeHalf(), + posY + shape.getShapeHalf(), renderPaint); } } From 572ac8c148fe0c69864673cc639aeea99f38bbe2 Mon Sep 17 00:00:00 2001 From: TheExecutor Date: Fri, 14 Jan 2022 11:41:38 +0100 Subject: [PATCH 2/3] make class shape abstract and extract render method --- .../charting/renderer/scatter/Shape.java | 52 --------- .../renderer/scatter/ShapeRenderer.java | 102 ++++++++++++++++++ 2 files changed, 102 insertions(+), 52 deletions(-) delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/Shape.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ShapeRenderer.java diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/Shape.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/Shape.java deleted file mode 100644 index 5686963c49..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/Shape.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.github.mikephil.charting.renderer.scatter; - -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.Utils; - -public class Shape { - private float shapeSize; - private float shapeHalf; - private float shapeHoleSizeHalf; - private float shapeHoleSize; - private float shapeStrokeSize; - private float shapeStrokeSizeHalf; - private int shapeHoleColor; - - public Shape(IScatterDataSet dataSet) { - this.shapeSize = dataSet.getScatterShapeSize(); - this.shapeHalf = shapeSize / 2f; - this.shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); - this.shapeHoleSize = shapeHoleSizeHalf * 2.f; - this.shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; - this.shapeStrokeSizeHalf = shapeStrokeSize / 2.f; - this.shapeHoleColor = dataSet.getScatterShapeHoleColor(); - } - - public float getShapeSize() { - return shapeSize; - } - - public float getShapeHalf() { - return shapeHalf; - } - - public float getShapeHoleSizeHalf() { - return shapeHoleSizeHalf; - } - - public float getShapeHoleSize() { - return shapeHoleSize; - } - - public float getShapeStrokeSize() { - return shapeStrokeSize; - } - - public float getShapeStrokeSizeHalf() { - return shapeStrokeSizeHalf; - } - - public int getShapeHoleColor() { - return shapeHoleColor; - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ShapeRenderer.java new file mode 100644 index 0000000000..5d2e2bc45b --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ShapeRenderer.java @@ -0,0 +1,102 @@ +package com.github.mikephil.charting.renderer.scatter; + +import android.graphics.Canvas; +import android.graphics.Paint; + +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; +import com.github.mikephil.charting.utils.ColorTemplate; +import com.github.mikephil.charting.utils.Utils; +import com.github.mikephil.charting.utils.ViewPortHandler; + +public abstract class Shape implements IShapeRenderer { + private float shapeSize; + private float shapeHalf; + private float shapeHoleSizeHalf; + private float shapeHoleSize; + private float shapeStrokeSize; + private float shapeStrokeSizeHalf; + private int shapeHoleColor; + + public Shape(IScatterDataSet dataSet) { + this.shapeSize = dataSet.getScatterShapeSize(); + this.shapeHalf = shapeSize / 2f; + this.shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); + this.shapeHoleSize = shapeHoleSizeHalf * 2.f; + this.shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; + this.shapeStrokeSizeHalf = shapeStrokeSize / 2.f; + this.shapeHoleColor = dataSet.getScatterShapeHoleColor(); + } + + public float getShapeSize() { + return shapeSize; + } + + public float getShapeHalf() { + return shapeHalf; + } + + public float getShapeHoleSizeHalf() { + return shapeHoleSizeHalf; + } + + public float getShapeHoleSize() { + return shapeHoleSize; + } + + public float getShapeStrokeSize() { + return shapeStrokeSize; + } + + public float getShapeStrokeSizeHalf() { + return shapeStrokeSizeHalf; + } + + public int getShapeHoleColor() { + return shapeHoleColor; + } + + @Override + public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, float posX, float posY, Paint renderPaint) { + + final float shapeSize = dataSet.getScatterShapeSize(); + final float shapeHalf = shapeSize / 2f; + final float shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); + final float shapeHoleSize = shapeHoleSizeHalf * 2.f; + final float shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; + final float shapeStrokeSizeHalf = shapeStrokeSize / 2.f; + + final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); + + if (shape.getShapeSize() > 0.0) { + renderPaint.setStyle(Paint.Style.STROKE); + renderPaint.setStrokeWidth(shape.getShapeStrokeSize()); + + c.drawCircle( + posX, + posY, + shape.getShapeHoleSizeHalf() + shape.getShapeStrokeSizeHalf(), + renderPaint); + + if (shape.getShapeHoleColor() != ColorTemplate.COLOR_NONE) { + renderPaint.setStyle(Paint.Style.FILL); + + renderPaint.setColor(shape.getShapeHoleColor()); + c.drawCircle( + posX, + posY, + shape.getShapeHoleSizeHalf(), + renderPaint); + } + } else { + renderPaint.setStyle(Paint.Style.FILL); + + c.drawCircle( + posX, + posY, + shape.getShapeHalf(), + renderPaint); + } + + } + } +} From 8afa294da11fdaa1305a6ef79c7936aef7659a7f Mon Sep 17 00:00:00 2001 From: TheExecutor Date: Fri, 14 Jan 2022 11:51:05 +0100 Subject: [PATCH 3/3] forgot to add classes in commit before --- .../renderer/scatter/CircleShapeRenderer.java | 57 ++----------- .../renderer/scatter/ShapeRenderer.java | 84 ++++--------------- .../renderer/scatter/SquareShapeRenderer.java | 69 +++++---------- 3 files changed, 43 insertions(+), 167 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java index 6d96925b9d..c3a8118748 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/CircleShapeRenderer.java @@ -3,63 +3,22 @@ import android.graphics.Canvas; import android.graphics.Paint; -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; - /** * Created by wajdic on 15/06/2016. * Created at Time 09:08 */ -public class CircleShapeRenderer implements IShapeRenderer +public class CircleShapeRenderer extends ShapeRenderer { @Override - public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, - float posX, float posY, Paint renderPaint) { - - final Shape shape = new Shape(dataSet); - /** - final float shapeSize = dataSet.getScatterShapeSize(); - final float shapeHalf = shapeSize / 2f; - final float shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); - final float shapeHoleSize = shapeHoleSizeHalf * 2.f; - final float shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; - final float shapeStrokeSizeHalf = shapeStrokeSize / 2.f; - - final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); - **/ - if (shape.getShapeSize() > 0.0) { - renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(shape.getShapeStrokeSize()); - - c.drawCircle( - posX, - posY, - shape.getShapeHoleSizeHalf() + shape.getShapeStrokeSizeHalf(), - renderPaint); - - if (shape.getShapeHoleColor() != ColorTemplate.COLOR_NONE) { - renderPaint.setStyle(Paint.Style.FILL); - - renderPaint.setColor(shape.getShapeHoleColor()); - c.drawCircle( - posX, - posY, - shape.getShapeHoleSizeHalf(), - renderPaint); - } - } else { - renderPaint.setStyle(Paint.Style.FILL); - - c.drawCircle( - posX, - posY, - shape.getShapeHalf(), - renderPaint); - } + protected void render(Canvas c, float shapeHoleSizeHalf, float shapeStrokeSizeHalf, + Paint renderPaint, float x, float y) { + renderHole(c, shapeHoleSizeHalf, renderPaint, x, y); + } + @Override + protected void renderHole(Canvas c, float radius, Paint renderPaint, float x, float y) { + c.drawCircle(x, y, radius, renderPaint); } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ShapeRenderer.java index 5d2e2bc45b..0d9b55d6d2 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ShapeRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ShapeRenderer.java @@ -8,52 +8,7 @@ import com.github.mikephil.charting.utils.Utils; import com.github.mikephil.charting.utils.ViewPortHandler; -public abstract class Shape implements IShapeRenderer { - private float shapeSize; - private float shapeHalf; - private float shapeHoleSizeHalf; - private float shapeHoleSize; - private float shapeStrokeSize; - private float shapeStrokeSizeHalf; - private int shapeHoleColor; - - public Shape(IScatterDataSet dataSet) { - this.shapeSize = dataSet.getScatterShapeSize(); - this.shapeHalf = shapeSize / 2f; - this.shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); - this.shapeHoleSize = shapeHoleSizeHalf * 2.f; - this.shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; - this.shapeStrokeSizeHalf = shapeStrokeSize / 2.f; - this.shapeHoleColor = dataSet.getScatterShapeHoleColor(); - } - - public float getShapeSize() { - return shapeSize; - } - - public float getShapeHalf() { - return shapeHalf; - } - - public float getShapeHoleSizeHalf() { - return shapeHoleSizeHalf; - } - - public float getShapeHoleSize() { - return shapeHoleSize; - } - - public float getShapeStrokeSize() { - return shapeStrokeSize; - } - - public float getShapeStrokeSizeHalf() { - return shapeStrokeSizeHalf; - } - - public int getShapeHoleColor() { - return shapeHoleColor; - } +public abstract class ShapeRenderer implements IShapeRenderer { @Override public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, float posX, float posY, Paint renderPaint) { @@ -66,37 +21,30 @@ public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewP final float shapeStrokeSizeHalf = shapeStrokeSize / 2.f; final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); - - if (shape.getShapeSize() > 0.0) { + + if (shapeSize > 0.0) { renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(shape.getShapeStrokeSize()); + renderPaint.setStrokeWidth(shapeStrokeSize); - c.drawCircle( - posX, - posY, - shape.getShapeHoleSizeHalf() + shape.getShapeStrokeSizeHalf(), - renderPaint); + render(c, shapeHoleSizeHalf,shapeStrokeSizeHalf, renderPaint, posX, posY); - if (shape.getShapeHoleColor() != ColorTemplate.COLOR_NONE) { + if (shapeHoleColor != ColorTemplate.COLOR_NONE) { renderPaint.setStyle(Paint.Style.FILL); - renderPaint.setColor(shape.getShapeHoleColor()); - c.drawCircle( - posX, - posY, - shape.getShapeHoleSizeHalf(), - renderPaint); + renderPaint.setColor(shapeHoleColor); + renderHole(c, shapeHoleSizeHalf, renderPaint, posX, posY); } } else { renderPaint.setStyle(Paint.Style.FILL); - - c.drawCircle( - posX, - posY, - shape.getShapeHalf(), - renderPaint); + renderHole(c, shapeHoleSizeHalf, renderPaint, posX, posY); } } - } + + protected abstract void render(Canvas c, float shapeHoleSizeHalf, float shapeStrokeSizeHalf, + Paint renderPaint, float x, float y); + + protected abstract void renderHole(Canvas c, float radius, Paint renderPaint, + float x, float y); + } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java index c3522f4e59..4fee0df94f 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/SquareShapeRenderer.java @@ -3,65 +3,34 @@ import android.graphics.Canvas; import android.graphics.Paint; -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; - /** * Created by wajdic on 15/06/2016. * Created at Time 09:08 */ -public class SquareShapeRenderer implements IShapeRenderer +public class SquareShapeRenderer extends ShapeRenderer { @Override - public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, - float posX, float posY, Paint renderPaint) { - - final Shape shape = new Shape(dataSet); - - /** - final float shapeSize = dataSet.getScatterShapeSize(); - final float shapeHalf = shapeSize / 2f; - final float shapeHoleSizeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeHoleRadius()); - final float shapeHoleSize = shapeHoleSizeHalf * 2.f; - final float shapeStrokeSize = (shapeSize - shapeHoleSize) / 2.f; - final float shapeStrokeSizeHalf = shapeStrokeSize / 2.f; - - final int shapeHoleColor = dataSet.getScatterShapeHoleColor(); - **/ - - if (shape.getShapeSize() > 0.0) { - renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(shape.getShapeStrokeSize()); - - c.drawRect(posX - shape.getShapeHoleSizeHalf() - shape.getShapeStrokeSizeHalf(), - posY - shape.getShapeHoleSizeHalf() - shape.getShapeStrokeSizeHalf(), - posX + shape.getShapeHoleSizeHalf() + shape.getShapeStrokeSizeHalf(), - posY + shape.getShapeHoleSizeHalf() + shape.getShapeStrokeSizeHalf(), - renderPaint); - - if (shape.getShapeHoleColor() != ColorTemplate.COLOR_NONE) { - renderPaint.setStyle(Paint.Style.FILL); - - renderPaint.setColor(shape.getShapeHoleColor()); - c.drawRect(posX - shape.getShapeHoleSizeHalf(), - posY - shape.getShapeHoleSizeHalf(), - posX + shape.getShapeHoleSizeHalf(), - posY + shape.getShapeHoleSizeHalf(), - renderPaint); - } + protected void render(Canvas c, float shapeHoleSizeHalf, float shapeStrokeSizeHalf, Paint renderPaint, float x, float y) { + drawRect(c, shapeHoleSizeHalf - shapeStrokeSizeHalf, + shapeHoleSizeHalf + shapeStrokeSizeHalf, + renderPaint, x, y); + } - } else { - renderPaint.setStyle(Paint.Style.FILL); + @Override + protected void renderHole(Canvas c, float radius, Paint renderPaint, float x, float y) { + drawRect(c, radius, radius, renderPaint, x, y); + } - c.drawRect(posX - shape.getShapeHalf(), - posY - shape.getShapeHalf(), - posX + shape.getShapeHalf(), - posY + shape.getShapeHalf(), - renderPaint); - } + private void drawRect(Canvas c, float offsetTopLeft, float offsetBottomRight, + Paint renderPaint, float x, float y){ + c.drawRect( + x-offsetTopLeft, + y-offsetTopLeft, + x+offsetBottomRight, + y+offsetBottomRight, + renderPaint + ); } }