diff --git a/spark/src/main/java/com/robinhood/spark/SparkView.java b/spark/src/main/java/com/robinhood/spark/SparkView.java index 4a92c0d..c8d4c17 100644 --- a/spark/src/main/java/com/robinhood/spark/SparkView.java +++ b/spark/src/main/java/com/robinhood/spark/SparkView.java @@ -22,6 +22,7 @@ import android.content.res.TypedArray; import android.database.DataSetObserver; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.CornerPathEffect; import android.graphics.Paint; import android.graphics.Path; @@ -111,6 +112,7 @@ public class SparkView extends View implements ScrubGestureDetector.ScrubListene // misc fields private ScaleHelper scaleHelper; private Paint sparkLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); + private Paint sparkLineUnderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Paint sparkFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Paint baseLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Paint scrubLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); @@ -118,6 +120,9 @@ public class SparkView extends View implements ScrubGestureDetector.ScrubListene private ScrubGestureDetector scrubGestureDetector; private Animator pathAnimator; private final RectF contentRect = new RectF(); + private float clipAmount = 1f; + private RectF clippedRect = new RectF(); + public boolean clipOnScrub; private List xPoints; private List yPoints; @@ -172,8 +177,11 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr, int def sparkLinePaint.setColor(lineColor); sparkLinePaint.setStrokeWidth(lineWidth); sparkLinePaint.setStrokeCap(Paint.Cap.ROUND); + sparkLineUnderPaint = new Paint(sparkLinePaint); + sparkLineUnderPaint.setColor(Color.GRAY); if (cornerRadius != 0) { sparkLinePaint.setPathEffect(new CornerPathEffect(cornerRadius)); + sparkLineUnderPaint.setPathEffect(new CornerPathEffect(cornerRadius)); } sparkFillPaint.set(sparkLinePaint); @@ -389,10 +397,28 @@ protected void onDraw(Canvas canvas) { canvas.drawPath(renderPath, sparkFillPaint); } + if(clipAmount < 1) { + canvas.drawPath(renderPath, sparkLineUnderPaint); + canvas.save(); + canvas.clipRect(clippedRect); + } canvas.drawPath(renderPath, sparkLinePaint); + if(clipAmount < 1) { + canvas.restore(); + } canvas.drawPath(scrubLinePath, scrubLinePaint); } + /** + * Set horizontal clip amount, set to 1 to disable clipping + * @param amount [0..1] + */ + public void setClipAmount(float amount) { + clipAmount = Math.max(0f, Math.min(1f, amount)); + this.clippedRect.right = contentRect.right * clipAmount; + invalidate(); + } + /** * Get the color of the sparkline */ @@ -847,6 +873,10 @@ private void updateContentRect() { getWidth() - getPaddingEnd(), getHeight() - getPaddingBottom() ); + + //also update clipping rect + clippedRect.set(contentRect); + setClipAmount(clipAmount); } /** @@ -890,6 +920,9 @@ public void onScrubbed(float x, float y) { } setScrubLine(x); + if(clipOnScrub) { + setClipAmount(x / contentRect.right); + } } @Override