Skip to content

Commit

Permalink
WIP improvements to hover value intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronPlave committed Dec 13, 2023
1 parent 6410cd8 commit e30a7df
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/components/timeline/LayerLine.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@
}
}
function getClosestPointForXY(
x: number,
y: number,
points: LinePoint[],
yScale: ScaleLinear<number, number, never>,
): LinePoint | null {
const pointsAtX = points.filter(p => p.y !== null && p.x === x);
const closest = pointsAtX.reduce((closestPoint, nextPoint) => {
if (!closestPoint) {
return nextPoint;
}
const distanceA = Math.abs(yScale(closestPoint.y) - y);
const distanceB = Math.abs(yScale(nextPoint.y) - y);
return distanceA < distanceB ? closestPoint : nextPoint;
}, null);
return closest;
}
function onMousemove(e: MouseEvent | undefined): void {
if (e) {
const { offsetX: x, offsetY: y } = e;
Expand All @@ -234,6 +252,9 @@
let rightPoint: LinePoint | null = null;
// TODO search more efficiently?
// TODO deal with multiple values per X to improve hovering behavior on spikes
const yScale = computeYScale(yAxes);
points.forEach(point => {
if (point.x <= xDate) {
if (!leftPoint) {
Expand All @@ -254,16 +275,27 @@
}
});
// TODO type the processed points?
let mouseOverPoints: LinePoint[] = [];
// TODO clean this up
if (leftPoint) {
if (!interpolateHoverValue) {
const closestPoint = getClosestPointForXY((leftPoint as LinePoint).x, y, points, yScale);
if (closestPoint) {
leftPoint = closestPoint;
}
}
mouseOverPoints.push(leftPoint);
}
if (rightPoint) {
if (!interpolateHoverValue) {
const closestPoint = getClosestPointForXY((leftPoint as LinePoint).x, y, points, yScale);
if (closestPoint) {
rightPoint = closestPoint;
}
}
mouseOverPoints.push(rightPoint);
}
const yScale = computeYScale(yAxes);
if (interactionCtx) {
interactionCtx.resetTransform();
interactionCtx.scale(dpr, dpr);
Expand Down Expand Up @@ -321,22 +353,14 @@
const circle3 = new Path2D();
circle3.arc(x, y, scalar + 3, 0, 2 * Math.PI);
// interactionCtx.globalAlpha = 0.8;
interactionCtx.fillStyle = fill;
interactionCtx.fill(circle3);
// interactionCtx.globalAlpha = 1;
const circle2 = new Path2D();
circle2.arc(x, y, scalar + 2, 0, 2 * Math.PI);
interactionCtx.fillStyle = 'white';
interactionCtx.fill(circle2);
// interactionCtx.strokeStyle = 'fill';
// interactionCtx.beginPath();
// interactionCtx.moveTo(0, y);
// interactionCtx.lineTo(canvasWidthDpr, y);
// interactionCtx.stroke();
const circle = new Path2D();
interactionCtx.fillStyle = fill;
interactionCtx.lineWidth = lineWidth;
Expand Down
1 change: 1 addition & 0 deletions src/utilities/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ export function getYAxisBounds(
const isNumber = typeof value.y === 'number';
// Identify the first value to the left of the viewTimeRange
if (viewTimeRange && value.x < viewTimeRange.start) {
// TODO shouldn't we continue on to next value if this is a gap?
if (value.is_gap) {
leftValue = undefined;
} else {
Expand Down

0 comments on commit e30a7df

Please sign in to comment.