Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Horizontal Clipping" #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions spark/src/main/java/com/robinhood/spark/SparkView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -111,13 +112,17 @@ 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);
private OnScrubListener scrubListener;
private ScrubGestureDetector scrubGestureDetector;
private Animator pathAnimator;
private final RectF contentRect = new RectF();
private float clipAmount = 1f;
private RectF clippedRect = new RectF();
public boolean clipOnScrub;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd likely want this to a public API instead of something that can be directly set on the view.


private List<Float> xPoints;
private List<Float> yPoints;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -847,6 +873,10 @@ private void updateContentRect() {
getWidth() - getPaddingEnd(),
getHeight() - getPaddingBottom()
);

//also update clipping rect
clippedRect.set(contentRect);
setClipAmount(clipAmount);
}

/**
Expand Down Expand Up @@ -890,6 +920,9 @@ public void onScrubbed(float x, float y) {
}

setScrubLine(x);
if(clipOnScrub) {
setClipAmount(x / contentRect.right);
}
}

@Override
Expand Down