Skip to content

Commit

Permalink
Add half line width to clipping rect of grid/limit lines (Closes Phil…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Aug 14, 2016
1 parent 4190a7e commit 998b89c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
Expand All @@ -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;
}

/**
Expand All @@ -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.
*
Expand All @@ -303,13 +319,20 @@ 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;

mTrans.pointValuesToPixel(position);

renderLimitLineLine(c, l, position);
renderLimitLineLabel(c, l, position, 2.f + l.getYOffset());

c.restoreToCount(clipRestoreCount);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -290,6 +303,8 @@ public void renderLimitLines(Canvas c) {
pts[1] + yOffset, mLimitLinePaint);
}
}

c.restoreToCount(clipRestoreCount);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -150,13 +154,23 @@ public void renderGridLines(Canvas c) {
c.drawPath(linePath(gridLinePath, i, positions), mGridPaint);
gridLinePath.reset();
}

c.restoreToCount(clipRestoreCount);
}

if (mYAxis.isDrawZeroLineEnabled()) {
drawZeroLine(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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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());
Expand Down Expand Up @@ -309,6 +329,8 @@ public void renderLimitLines(Canvas c) {
pts[1] + yOffset, mLimitLinePaint);
}
}

c.restoreToCount(clipRestoreCount);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -281,6 +294,8 @@ public void renderLimitLines(Canvas c) {
c.drawText(label, pts[0] - xOffset, mViewPortHandler.contentBottom() - yOffset, mLimitLinePaint);
}
}

c.restoreToCount(clipRestoreCount);
}
}
}

0 comments on commit 998b89c

Please sign in to comment.