GeezerVison is showing github.com — Add half line width to clipping rect of grid/limit lines (Closes #2081) · FlyingAndroid/MPAndroidChart@7747c34
File treeExpand file treeCollapse file tree Open diff view settings MPChartLib/src/main/java/com/github/mikephil/charting
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java +5-11Lines changed: 5 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -222,11 +222,6 @@ protected void onDraw(Canvas canvas) { | 222 | 222 | } | 223 | 223 | } | 224 | 224 |
| 225 | | - // make sure the graph values and grid cannot be drawn outside the | 226 | | - // content-rect | 227 | | - int clipRestoreCount = canvas.save(); | 228 | | - canvas.clipRect(mViewPortHandler.getContentRect()); | 229 | | - | 230 | 225 | mXAxisRenderer.renderGridLines(canvas); | 231 | 226 | mAxisRendererLeft.renderGridLines(canvas); | 232 | 227 | mAxisRendererRight.renderGridLines(canvas); | @@ -240,6 +235,10 @@ protected void onDraw(Canvas canvas) { | 240 | 235 | if (mAxisRight.isDrawLimitLinesBehindDataEnabled()) | 241 | 236 | mAxisRendererRight.renderLimitLines(canvas); | 242 | 237 |
| | 238 | + // make sure the data cannot be drawn outside the content-rect | | 239 | + int clipRestoreCount = canvas.save(); | | 240 | + canvas.clipRect(mViewPortHandler.getContentRect()); | | 241 | + | 243 | 242 | mRenderer.drawData(canvas); | 244 | 243 |
| 245 | 244 | // if highlighting is enabled | @@ -251,9 +250,6 @@ protected void onDraw(Canvas canvas) { | 251 | 250 |
| 252 | 251 | mRenderer.drawExtras(canvas); | 253 | 252 |
| 254 | | - clipRestoreCount = canvas.save(); | 255 | | - canvas.clipRect(mViewPortHandler.getContentRect()); | 256 | | - | 257 | 253 | if (!mXAxis.isDrawLimitLinesBehindDataEnabled()) | 258 | 254 | mXAxisRenderer.renderLimitLines(canvas); | 259 | 255 |
| @@ -262,9 +258,7 @@ protected void onDraw(Canvas canvas) { | 262 | 258 |
| 263 | 259 | if (!mAxisRight.isDrawLimitLinesBehindDataEnabled()) | 264 | 260 | mAxisRendererRight.renderLimitLines(canvas); | 265 | | - | 266 | | - canvas.restoreToCount(clipRestoreCount); | 267 | | - | | 261 | + | 268 | 262 | mXAxisRenderer.renderAxisLabels(canvas); | 269 | 263 | mAxisRendererLeft.renderAxisLabels(canvas); | 270 | 264 | mAxisRendererRight.renderAxisLabels(canvas); | |
Collapse file Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.java +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | import android.graphics.Paint; | 7 | 7 | import android.graphics.Paint.Align; | 8 | 8 | import android.graphics.Path; | | 9 | +import android.graphics.RectF; | 9 | 10 |
| 10 | 11 | import com.github.mikephil.charting.components.LimitLine; | 11 | 12 | import com.github.mikephil.charting.components.XAxis; | @@ -236,6 +237,9 @@ public void renderGridLines(Canvas c) { | 236 | 237 | if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled()) | 237 | 238 | return; | 238 | 239 |
| | 240 | + int clipRestoreCount = c.save(); | | 241 | + c.clipRect(getGridClippingRect()); | | 242 | + | 239 | 243 | if(mRenderGridLinesBuffer.length != mAxis.mEntryCount * 2){ | 240 | 244 | mRenderGridLinesBuffer = new float[mXAxis.mEntryCount * 2]; | 241 | 245 | } | @@ -257,6 +261,16 @@ public void renderGridLines(Canvas c) { | 257 | 261 |
| 258 | 262 | drawGridLine(c, positions[i], positions[i + 1], gridLinePath); | 259 | 263 | } | | 264 | + | | 265 | + c.restoreToCount(clipRestoreCount); | | 266 | + } | | 267 | + | | 268 | + protected RectF mGridClippingRect = new RectF(); | | 269 | + | | 270 | + public RectF getGridClippingRect() { | | 271 | + mGridClippingRect.set(mViewPortHandler.getContentRect()); | | 272 | + mGridClippingRect.inset(-mAxis.getGridLineWidth() / 2.f, 0.f); | | 273 | + return mGridClippingRect; | 260 | 274 | } | 261 | 275 |
| 262 | 276 | /** | @@ -279,6 +293,8 @@ protected void drawGridLine(Canvas c, float x, float y, Path gridLinePath) { | 279 | 293 | } | 280 | 294 |
| 281 | 295 | protected float[] mRenderLimitLinesBuffer = new float[2]; | | 296 | + protected RectF mLimitLineClippingRect = new RectF(); | | 297 | + | 282 | 298 | /** | 283 | 299 | * Draws the LimitLines associated with this axis to the screen. | 284 | 300 | * | @@ -303,13 +319,20 @@ public void renderLimitLines(Canvas c) { | 303 | 319 | if (!l.isEnabled()) | 304 | 320 | continue; | 305 | 321 |
| | 322 | + int clipRestoreCount = c.save(); | | 323 | + mLimitLineClippingRect.set(mViewPortHandler.getContentRect()); | | 324 | + mLimitLineClippingRect.inset(-l.getLineWidth() / 2.f, 0.f); | | 325 | + c.clipRect(mLimitLineClippingRect); | | 326 | + | 306 | 327 | position[0] = l.getLimit(); | 307 | 328 | position[1] = 0.f; | 308 | 329 |
| 309 | 330 | mTrans.pointValuesToPixel(position); | 310 | 331 |
| 311 | 332 | renderLimitLineLine(c, l, position); | 312 | 333 | renderLimitLineLabel(c, l, position, 2.f + l.getYOffset()); | | 334 | + | | 335 | + c.restoreToCount(clipRestoreCount); | 313 | 336 | } | 314 | 337 | } | 315 | 338 |
| |
Collapse file Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.java +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | import android.graphics.Paint; | 6 | 6 | import android.graphics.Paint.Align; | 7 | 7 | import android.graphics.Path; | | 8 | +import android.graphics.RectF; | 8 | 9 |
| 9 | 10 | import com.github.mikephil.charting.charts.BarChart; | 10 | 11 | import com.github.mikephil.charting.components.LimitLine; | @@ -161,6 +162,13 @@ protected void drawLabels(Canvas c, float pos, MPPointF anchor) { | 161 | 162 | } | 162 | 163 | } | 163 | 164 |
| | 165 | + @Override | | 166 | + public RectF getGridClippingRect() { | | 167 | + mGridClippingRect.set(mViewPortHandler.getContentRect()); | | 168 | + mGridClippingRect.inset(0.f, -mAxis.getGridLineWidth() / 2.f); | | 169 | + return mGridClippingRect; | | 170 | + } | | 171 | + | 164 | 172 | @Override | 165 | 173 | protected void drawGridLine(Canvas c, float x, float y, Path gridLinePath) { | 166 | 174 |
| @@ -228,6 +236,11 @@ public void renderLimitLines(Canvas c) { | 228 | 236 | if(!l.isEnabled()) | 229 | 237 | continue; | 230 | 238 |
| | 239 | + int clipRestoreCount = c.save(); | | 240 | + mLimitLineClippingRect.set(mViewPortHandler.getContentRect()); | | 241 | + mLimitLineClippingRect.inset(0.f, -l.getLineWidth() / 2.f); | | 242 | + c.clipRect(mLimitLineClippingRect); | | 243 | + | 231 | 244 | mLimitLinePaint.setStyle(Paint.Style.STROKE); | 232 | 245 | mLimitLinePaint.setColor(l.getLineColor()); | 233 | 246 | mLimitLinePaint.setStrokeWidth(l.getLineWidth()); | @@ -290,6 +303,8 @@ public void renderLimitLines(Canvas c) { | 290 | 303 | pts[1] + yOffset, mLimitLinePaint); | 291 | 304 | } | 292 | 305 | } | | 306 | + | | 307 | + c.restoreToCount(clipRestoreCount); | 293 | 308 | } | 294 | 309 | } | 295 | 310 | } |
Collapse file Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRenderer.java +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | import android.graphics.Paint; | 6 | 6 | import android.graphics.Paint.Align; | 7 | 7 | import android.graphics.Path; | | 8 | +import android.graphics.RectF; | 8 | 9 |
| 9 | 10 | import com.github.mikephil.charting.components.LimitLine; | 10 | 11 | import com.github.mikephil.charting.components.YAxis; | @@ -134,6 +135,9 @@ public void renderGridLines(Canvas c) { | 134 | 135 |
| 135 | 136 | if (mYAxis.isDrawGridLinesEnabled()) { | 136 | 137 |
| | 138 | + int clipRestoreCount = c.save(); | | 139 | + c.clipRect(getGridClippingRect()); | | 140 | + | 137 | 141 | float[] positions = getTransformedPositions(); | 138 | 142 |
| 139 | 143 | mGridPaint.setColor(mYAxis.getGridColor()); | @@ -150,13 +154,23 @@ public void renderGridLines(Canvas c) { | 150 | 154 | c.drawPath(linePath(gridLinePath, i, positions), mGridPaint); | 151 | 155 | gridLinePath.reset(); | 152 | 156 | } | | 157 | + | | 158 | + c.restoreToCount(clipRestoreCount); | 153 | 159 | } | 154 | 160 |
| 155 | 161 | if (mYAxis.isDrawZeroLineEnabled()) { | 156 | 162 | drawZeroLine(c); | 157 | 163 | } | 158 | 164 | } | 159 | 165 |
| | 166 | + protected RectF mGridClippingRect = new RectF(); | | 167 | + | | 168 | + public RectF getGridClippingRect() { | | 169 | + mGridClippingRect.set(mViewPortHandler.getContentRect()); | | 170 | + mGridClippingRect.inset(0.f, -mAxis.getGridLineWidth() / 2.f); | | 171 | + return mGridClippingRect; | | 172 | + } | | 173 | + | 160 | 174 | /** | 161 | 175 | * Calculates the path for a grid line. | 162 | 176 | * | @@ -220,6 +234,7 @@ protected void drawZeroLine(Canvas c) { | 220 | 234 |
| 221 | 235 | protected Path mRenderLimitLines = new Path(); | 222 | 236 | protected float[] mRenderLimitLinesBuffer = new float[2]; | | 237 | + protected RectF mLimitLineClippingRect = new RectF(); | 223 | 238 | /** | 224 | 239 | * Draws the LimitLines associated with this axis to the screen. | 225 | 240 | * | @@ -246,6 +261,11 @@ public void renderLimitLines(Canvas c) { | 246 | 261 | if (!l.isEnabled()) | 247 | 262 | continue; | 248 | 263 |
| | 264 | + int clipRestoreCount = c.save(); | | 265 | + mLimitLineClippingRect.set(mViewPortHandler.getContentRect()); | | 266 | + mLimitLineClippingRect.inset(0.f, -l.getLineWidth() / 2.f); | | 267 | + c.clipRect(mLimitLineClippingRect); | | 268 | + | 249 | 269 | mLimitLinePaint.setStyle(Paint.Style.STROKE); | 250 | 270 | mLimitLinePaint.setColor(l.getLineColor()); | 251 | 271 | mLimitLinePaint.setStrokeWidth(l.getLineWidth()); | @@ -309,6 +329,8 @@ public void renderLimitLines(Canvas c) { | 309 | 329 | pts[1] + yOffset, mLimitLinePaint); | 310 | 330 | } | 311 | 331 | } | | 332 | + | | 333 | + c.restoreToCount(clipRestoreCount); | 312 | 334 | } | 313 | 335 | } | 314 | 336 | } |
Collapse file Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererHorizontalBarChart.java +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | import android.graphics.Paint; | 6 | 6 | import android.graphics.Paint.Align; | 7 | 7 | import android.graphics.Path; | | 8 | +import android.graphics.RectF; | 8 | 9 |
| 9 | 10 | import com.github.mikephil.charting.components.LimitLine; | 10 | 11 | import com.github.mikephil.charting.components.YAxis; | @@ -164,6 +165,13 @@ protected float[] getTransformedPositions() { | 164 | 165 | return positions; | 165 | 166 | } | 166 | 167 |
| | 168 | + @Override | | 169 | + public RectF getGridClippingRect() { | | 170 | + mGridClippingRect.set(mViewPortHandler.getContentRect()); | | 171 | + mGridClippingRect.inset(-mAxis.getGridLineWidth() / 2.f, 0.f); | | 172 | + return mGridClippingRect; | | 173 | + } | | 174 | + | 167 | 175 | @Override | 168 | 176 | protected Path linePath(Path p, int i, float[] positions) { | 169 | 177 |
| @@ -225,6 +233,11 @@ public void renderLimitLines(Canvas c) { | 225 | 233 | if (!l.isEnabled()) | 226 | 234 | continue; | 227 | 235 |
| | 236 | + int clipRestoreCount = c.save(); | | 237 | + mLimitLineClippingRect.set(mViewPortHandler.getContentRect()); | | 238 | + mLimitLineClippingRect.inset(-l.getLineWidth() / 2.f, 0.f); | | 239 | + c.clipRect(mLimitLineClippingRect); | | 240 | + | 228 | 241 | pts[0] = l.getLimit(); | 229 | 242 | pts[2] = l.getLimit(); | 230 | 243 |
| @@ -281,6 +294,8 @@ public void renderLimitLines(Canvas c) { | 281 | 294 | c.drawText(label, pts[0] - xOffset, mViewPortHandler.contentBottom() - yOffset, mLimitLinePaint); | 282 | 295 | } | 283 | 296 | } | | 297 | + | | 298 | + c.restoreToCount(clipRestoreCount); | 284 | 299 | } | 285 | 300 | } | 286 | 301 | } |
|