From 7747c345523f355e807451601a7fbb4d4d5910b1 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Sun, 14 Aug 2016 11:07:14 +0300 Subject: [PATCH] Add half line width to clipping rect of grid/limit lines (Closes #2081) https://github.com/danielgindi/Charts/issues/1204 --- .../charting/charts/BarLineChartBase.java | 16 ++++--------- .../charting/renderer/XAxisRenderer.java | 23 +++++++++++++++++++ .../XAxisRendererHorizontalBarChart.java | 15 ++++++++++++ .../charting/renderer/YAxisRenderer.java | 22 ++++++++++++++++++ .../YAxisRendererHorizontalBarChart.java | 15 ++++++++++++ 5 files changed, 80 insertions(+), 11 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java index fbc20a44b3..494c8bcedb 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -222,11 +222,6 @@ protected void onDraw(Canvas canvas) { } } - // make sure the graph values and grid cannot be drawn outside the - // content-rect - int clipRestoreCount = canvas.save(); - canvas.clipRect(mViewPortHandler.getContentRect()); - mXAxisRenderer.renderGridLines(canvas); mAxisRendererLeft.renderGridLines(canvas); mAxisRendererRight.renderGridLines(canvas); @@ -240,6 +235,10 @@ protected void onDraw(Canvas canvas) { if (mAxisRight.isDrawLimitLinesBehindDataEnabled()) mAxisRendererRight.renderLimitLines(canvas); + // make sure the data cannot be drawn outside the content-rect + int clipRestoreCount = canvas.save(); + canvas.clipRect(mViewPortHandler.getContentRect()); + mRenderer.drawData(canvas); // if highlighting is enabled @@ -251,9 +250,6 @@ protected void onDraw(Canvas canvas) { mRenderer.drawExtras(canvas); - clipRestoreCount = canvas.save(); - canvas.clipRect(mViewPortHandler.getContentRect()); - if (!mXAxis.isDrawLimitLinesBehindDataEnabled()) mXAxisRenderer.renderLimitLines(canvas); @@ -262,9 +258,7 @@ protected void onDraw(Canvas canvas) { if (!mAxisRight.isDrawLimitLinesBehindDataEnabled()) mAxisRendererRight.renderLimitLines(canvas); - - canvas.restoreToCount(clipRestoreCount); - + mXAxisRenderer.renderAxisLabels(canvas); mAxisRendererLeft.renderAxisLabels(canvas); mAxisRendererRight.renderAxisLabels(canvas); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java index 2a93910d72..43ce279e8e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java @@ -6,6 +6,7 @@ import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Path; +import android.graphics.RectF; import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.XAxis; @@ -236,6 +237,9 @@ public void renderGridLines(Canvas c) { if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled()) return; + int clipRestoreCount = c.save(); + c.clipRect(getGridClippingRect()); + if(mRenderGridLinesBuffer.length != mAxis.mEntryCount * 2){ mRenderGridLinesBuffer = new float[mXAxis.mEntryCount * 2]; } @@ -257,6 +261,16 @@ public void renderGridLines(Canvas c) { drawGridLine(c, positions[i], positions[i + 1], gridLinePath); } + + c.restoreToCount(clipRestoreCount); + } + + protected RectF mGridClippingRect = new RectF(); + + public RectF getGridClippingRect() { + mGridClippingRect.set(mViewPortHandler.getContentRect()); + mGridClippingRect.inset(-mAxis.getGridLineWidth() / 2.f, 0.f); + return mGridClippingRect; } /** @@ -279,6 +293,8 @@ protected void drawGridLine(Canvas c, float x, float y, Path gridLinePath) { } protected float[] mRenderLimitLinesBuffer = new float[2]; + protected RectF mLimitLineClippingRect = new RectF(); + /** * Draws the LimitLines associated with this axis to the screen. * @@ -303,6 +319,11 @@ public void renderLimitLines(Canvas c) { if (!l.isEnabled()) continue; + int clipRestoreCount = c.save(); + mLimitLineClippingRect.set(mViewPortHandler.getContentRect()); + mLimitLineClippingRect.inset(-l.getLineWidth() / 2.f, 0.f); + c.clipRect(mLimitLineClippingRect); + position[0] = l.getLimit(); position[1] = 0.f; @@ -310,6 +331,8 @@ public void renderLimitLines(Canvas c) { renderLimitLineLine(c, l, position); renderLimitLineLabel(c, l, position, 2.f + l.getYOffset()); + + c.restoreToCount(clipRestoreCount); } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java index 02192f5a47..48e1ece8b5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java @@ -5,6 +5,7 @@ import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Path; +import android.graphics.RectF; import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.components.LimitLine; @@ -161,6 +162,13 @@ protected void drawLabels(Canvas c, float pos, MPPointF anchor) { } } + @Override + public RectF getGridClippingRect() { + mGridClippingRect.set(mViewPortHandler.getContentRect()); + mGridClippingRect.inset(0.f, -mAxis.getGridLineWidth() / 2.f); + return mGridClippingRect; + } + @Override protected void drawGridLine(Canvas c, float x, float y, Path gridLinePath) { @@ -228,6 +236,11 @@ public void renderLimitLines(Canvas c) { if(!l.isEnabled()) continue; + int clipRestoreCount = c.save(); + mLimitLineClippingRect.set(mViewPortHandler.getContentRect()); + mLimitLineClippingRect.inset(0.f, -l.getLineWidth() / 2.f); + c.clipRect(mLimitLineClippingRect); + mLimitLinePaint.setStyle(Paint.Style.STROKE); mLimitLinePaint.setColor(l.getLineColor()); mLimitLinePaint.setStrokeWidth(l.getLineWidth()); @@ -290,6 +303,8 @@ public void renderLimitLines(Canvas c) { pts[1] + yOffset, mLimitLinePaint); } } + + c.restoreToCount(clipRestoreCount); } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java index 09e25d1e1e..092714788a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java @@ -5,6 +5,7 @@ import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Path; +import android.graphics.RectF; import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.YAxis; @@ -134,6 +135,9 @@ public void renderGridLines(Canvas c) { if (mYAxis.isDrawGridLinesEnabled()) { + int clipRestoreCount = c.save(); + c.clipRect(getGridClippingRect()); + float[] positions = getTransformedPositions(); mGridPaint.setColor(mYAxis.getGridColor()); @@ -150,6 +154,8 @@ public void renderGridLines(Canvas c) { c.drawPath(linePath(gridLinePath, i, positions), mGridPaint); gridLinePath.reset(); } + + c.restoreToCount(clipRestoreCount); } if (mYAxis.isDrawZeroLineEnabled()) { @@ -157,6 +163,14 @@ public void renderGridLines(Canvas c) { } } + protected RectF mGridClippingRect = new RectF(); + + public RectF getGridClippingRect() { + mGridClippingRect.set(mViewPortHandler.getContentRect()); + mGridClippingRect.inset(0.f, -mAxis.getGridLineWidth() / 2.f); + return mGridClippingRect; + } + /** * Calculates the path for a grid line. * @@ -220,6 +234,7 @@ protected void drawZeroLine(Canvas c) { protected Path mRenderLimitLines = new Path(); protected float[] mRenderLimitLinesBuffer = new float[2]; + protected RectF mLimitLineClippingRect = new RectF(); /** * Draws the LimitLines associated with this axis to the screen. * @@ -246,6 +261,11 @@ public void renderLimitLines(Canvas c) { if (!l.isEnabled()) continue; + int clipRestoreCount = c.save(); + mLimitLineClippingRect.set(mViewPortHandler.getContentRect()); + mLimitLineClippingRect.inset(0.f, -l.getLineWidth() / 2.f); + c.clipRect(mLimitLineClippingRect); + mLimitLinePaint.setStyle(Paint.Style.STROKE); mLimitLinePaint.setColor(l.getLineColor()); mLimitLinePaint.setStrokeWidth(l.getLineWidth()); @@ -309,6 +329,8 @@ public void renderLimitLines(Canvas c) { pts[1] + yOffset, mLimitLinePaint); } } + + c.restoreToCount(clipRestoreCount); } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java index 6cf7985ea2..f867cf5f3a 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java @@ -5,6 +5,7 @@ import android.graphics.Paint; import android.graphics.Paint.Align; import android.graphics.Path; +import android.graphics.RectF; import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.YAxis; @@ -164,6 +165,13 @@ protected float[] getTransformedPositions() { return positions; } + @Override + public RectF getGridClippingRect() { + mGridClippingRect.set(mViewPortHandler.getContentRect()); + mGridClippingRect.inset(-mAxis.getGridLineWidth() / 2.f, 0.f); + return mGridClippingRect; + } + @Override protected Path linePath(Path p, int i, float[] positions) { @@ -225,6 +233,11 @@ public void renderLimitLines(Canvas c) { if (!l.isEnabled()) continue; + int clipRestoreCount = c.save(); + mLimitLineClippingRect.set(mViewPortHandler.getContentRect()); + mLimitLineClippingRect.inset(-l.getLineWidth() / 2.f, 0.f); + c.clipRect(mLimitLineClippingRect); + pts[0] = l.getLimit(); pts[2] = l.getLimit(); @@ -281,6 +294,8 @@ public void renderLimitLines(Canvas c) { c.drawText(label, pts[0] - xOffset, mViewPortHandler.contentBottom() - yOffset, mLimitLinePaint); } } + + c.restoreToCount(clipRestoreCount); } } }