Skip to content

Commit e1b3882

Browse files
committed
another cursor.dataIdx demo
1 parent de8b038 commit e1b3882

File tree

2 files changed

+63
-30
lines changed

2 files changed

+63
-30
lines changed

demos/nearest-non-null.html

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -339,38 +339,19 @@
339339
];
340340

341341
let opts = {
342-
title: 'Snap hover points and cursor to previous non-null',
343-
width: 1000,
344-
height: 500,
342+
title: 'Snap hover points to previous non-null (cursor.dataIdx)',
343+
width: 600,
344+
height: 300,
345345
cursor: {
346346
y: false,
347+
dataIdx: (self, seriesIdx, hoveredIdx, cursorXVal) => {
348+
let prevIdx = self.data[0][hoveredIdx] > cursorXVal ? hoveredIdx - 1 : hoveredIdx;
349+
let yVals = self.data[seriesIdx];
347350

348-
// this manually implements custom skipping and bias behavior
349-
move: (u, mouseLeft, mouseTop) => {
350-
if (mouseLeft < 0)
351-
return [mouseLeft, mouseTop];
352-
353-
let curXVal = u.posToVal(mouseLeft, 'x');
354-
let curIdx = u.valToIdx(curXVal);
355-
let dataX = u.data[0];
356-
let dataY = u.data[1];
357-
let idxXVal = dataX[curIdx];
358-
let idxYVal = dataY[curIdx];
359-
360-
// backward bias
361-
if (curXVal < idxXVal || idxYVal == null) {
362-
while (curIdx > 0) {
363-
idxYVal = dataY[--curIdx];
364-
365-
if (idxYVal != null)
366-
break;
367-
}
368-
}
351+
while (yVals[prevIdx] == null)
352+
prevIdx--;
369353

370-
return [
371-
Math.round(u.valToPos(dataX[curIdx], 'x')),
372-
mouseTop,
373-
];
354+
return prevIdx;
374355
},
375356
},
376357
scales: {
@@ -382,12 +363,64 @@
382363
{},
383364
{
384365
label: 'foo',
385-
stroke: 'blue',
366+
stroke: 'red',
386367
}
387368
],
388369
};
389370

390371
let u = new uPlot(opts, data, document.body);
372+
373+
// this manually implements custom skipping and bias behavior
374+
let move = (u, mouseLeft, mouseTop) => {
375+
if (mouseLeft < 0)
376+
return [mouseLeft, mouseTop];
377+
378+
let curXVal = u.posToVal(mouseLeft, 'x');
379+
let curIdx = u.valToIdx(curXVal);
380+
let dataX = u.data[0];
381+
let dataY = u.data[1];
382+
let idxXVal = dataX[curIdx];
383+
let idxYVal = dataY[curIdx];
384+
385+
// backward bias
386+
if (curXVal < idxXVal || idxYVal == null) {
387+
while (curIdx > 0) {
388+
idxYVal = dataY[--curIdx];
389+
390+
if (idxYVal != null)
391+
break;
392+
}
393+
}
394+
395+
return [
396+
Math.round(u.valToPos(dataX[curIdx], 'x')),
397+
mouseTop,
398+
];
399+
};
400+
401+
let opts2 = {
402+
title: 'Snap hover points and cursor to previous non-null (cursor.move)',
403+
width: 600,
404+
height: 300,
405+
cursor: {
406+
y: false,
407+
move,
408+
},
409+
scales: {
410+
x: {
411+
time: false,
412+
}
413+
},
414+
series: [
415+
{},
416+
{
417+
label: 'foo',
418+
stroke: 'blue',
419+
}
420+
],
421+
};
422+
423+
let u2 = new uPlot(opts2, data, document.body);
391424
}
392425
</script>
393426
</body>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
"homepage": "https://github.com/leeoniya/uPlot#readme",
4141
"devDependencies": {
4242
"@rollup/plugin-terser": "^0.4.4",
43-
"rollup": "^4.46.2"
43+
"rollup": "^4.50.0"
4444
}
4545
}

0 commit comments

Comments
 (0)