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..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,61 +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 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) { - renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(shapeStrokeSize); - - c.drawCircle( - posX, - posY, - shapeHoleSizeHalf + shapeStrokeSizeHalf, - renderPaint); - - if (shapeHoleColor != ColorTemplate.COLOR_NONE) { - renderPaint.setStyle(Paint.Style.FILL); - - renderPaint.setColor(shapeHoleColor); - c.drawCircle( - posX, - posY, - shapeHoleSizeHalf, - renderPaint); - } - } else { - renderPaint.setStyle(Paint.Style.FILL); - - c.drawCircle( - posX, - posY, - shapeHalf, - 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 new file mode 100644 index 0000000000..0d9b55d6d2 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/scatter/ShapeRenderer.java @@ -0,0 +1,50 @@ +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 ShapeRenderer implements IShapeRenderer { + + @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 (shapeSize > 0.0) { + renderPaint.setStyle(Paint.Style.STROKE); + renderPaint.setStrokeWidth(shapeStrokeSize); + + render(c, shapeHoleSizeHalf,shapeStrokeSizeHalf, renderPaint, posX, posY); + + if (shapeHoleColor != ColorTemplate.COLOR_NONE) { + renderPaint.setStyle(Paint.Style.FILL); + + renderPaint.setColor(shapeHoleColor); + renderHole(c, shapeHoleSizeHalf, renderPaint, posX, posY); + } + } else { + renderPaint.setStyle(Paint.Style.FILL); + 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 ac98679233..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,61 +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 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) { - renderPaint.setStyle(Paint.Style.STROKE); - renderPaint.setStrokeWidth(shapeStrokeSize); - - c.drawRect(posX - shapeHoleSizeHalf - shapeStrokeSizeHalf, - posY - shapeHoleSizeHalf - shapeStrokeSizeHalf, - posX + shapeHoleSizeHalf + shapeStrokeSizeHalf, - posY + shapeHoleSizeHalf + shapeStrokeSizeHalf, - renderPaint); - - if (shapeHoleColor != ColorTemplate.COLOR_NONE) { - renderPaint.setStyle(Paint.Style.FILL); - - renderPaint.setColor(shapeHoleColor); - c.drawRect(posX - shapeHoleSizeHalf, - posY - shapeHoleSizeHalf, - posX + shapeHoleSizeHalf, - posY + shapeHoleSizeHalf, - 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 - shapeHalf, - posY - shapeHalf, - posX + shapeHalf, - posY + shapeHalf, - 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 + ); } }