Skip to content

Commit 7df6851

Browse files
committed
demo of hover/cursor snapping. close #972.
1 parent 57a94e9 commit 7df6851

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

demos/nearest-non-null.html

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,44 @@
339339
];
340340

341341
let opts = {
342-
title: 'Snap cursor to last',
342+
title: 'Snap hover points (and/or cusror) to previous non-null',
343343
width: 1000,
344344
height: 500,
345345
cursor: {
346+
// can use this if only snapping hover points, but keeping cursor at mouse coords
346347
hover: {
347348
bias: -1,
348349
skip: [null],
349350
},
351+
/*
352+
// OR...
353+
354+
// this manually implements bias instead of relying on dataIdx/hover opts since they
355+
// are invoked later (and currently depend on the output coords of cursor.move)
356+
move: (u, mouseLeft, mouseTop) => {
357+
let curXVal = u.posToVal(mouseLeft, 'x');
358+
let curIdx = u.valToIdx(curXVal);
359+
let dataX = u.data[0];
360+
let dataY = u.data[1];
361+
let idxXVal = dataX[curIdx];
362+
let idxYVal = dataY[curIdx];
363+
364+
// backward bias
365+
if (curXVal < idxXVal || idxYVal == null) {
366+
while (curIdx > 0) {
367+
idxYVal = dataY[--curIdx];
368+
369+
if (idxYVal != null)
370+
break;
371+
}
372+
}
373+
374+
return [
375+
Math.round(u.valToPos(dataX[curIdx], 'x')),
376+
mouseTop,
377+
];
378+
},
379+
*/
350380
},
351381
scales: {
352382
x: {

0 commit comments

Comments
 (0)