TimeSeries: Simplify hover proximity code (#81518)

This commit is contained in:
Leon Sorokin
2024-01-29 15:34:43 -06:00
committed by GitHub
parent 12e691831d
commit 226c76dc93
6 changed files with 24 additions and 54 deletions

View File

@ -397,7 +397,7 @@
"tinycolor2": "1.6.0",
"tslib": "2.6.2",
"tween-functions": "^1.2.0",
"uplot": "1.6.29",
"uplot": "1.6.30",
"uuid": "9.0.1",
"visjs-network": "4.25.0",
"webpack-assets-manifest": "^5.1.0",

View File

@ -58,7 +58,7 @@
"string-hash": "^1.1.3",
"tinycolor2": "1.6.0",
"tslib": "2.6.2",
"uplot": "1.6.29",
"uplot": "1.6.30",
"xss": "^1.0.14"
},
"devDependencies": {

View File

@ -106,7 +106,7 @@
"slate-react": "0.22.10",
"tinycolor2": "1.6.0",
"tslib": "2.6.2",
"uplot": "1.6.29",
"uplot": "1.6.30",
"uuid": "9.0.1"
},
"devDependencies": {

View File

@ -61,13 +61,18 @@ exports[`GraphNG utils preparePlotConfigBuilder 1`] = `
},
],
"cursor": {
"dataIdx": [Function],
"drag": {
"setScale": false,
},
"focus": {
"prox": 30,
},
"hover": {
"prox": [Function],
"skip": [
null,
],
},
"points": {
"fill": [Function],
"size": [Function],

View File

@ -561,51 +561,16 @@ export const preparePlotConfigBuilder: UPlotConfigPrepFn<{
const hoverProximityPx = 15;
let cursor: Partial<uPlot.Cursor> = {
// this scans left and right from cursor position to find nearest data index with value != null
// TODO: do we want to only scan past undefined values, but halt at explicit null values?
dataIdx: (self, seriesIdx, hoveredIdx, cursorXVal) => {
let seriesData = self.data[seriesIdx];
if (seriesData[hoveredIdx] == null) {
let nonNullLft = null,
nonNullRgt = null,
i;
i = hoveredIdx;
while (nonNullLft == null && i-- > 0) {
if (seriesData[i] != null) {
nonNullLft = i;
}
hover: {
prox: (self, seriesIdx, hoveredIdx) => {
const yVal = self.data[seriesIdx][hoveredIdx];
if (yVal === null) {
return hoverProximityPx;
}
i = hoveredIdx;
while (nonNullRgt == null && i++ < seriesData.length) {
if (seriesData[i] != null) {
nonNullRgt = i;
}
}
let xVals = self.data[0];
let curPos = self.valToPos(cursorXVal, 'x');
let rgtPos = nonNullRgt == null ? Infinity : self.valToPos(xVals[nonNullRgt], 'x');
let lftPos = nonNullLft == null ? -Infinity : self.valToPos(xVals[nonNullLft], 'x');
let lftDelta = curPos - lftPos;
let rgtDelta = rgtPos - curPos;
if (lftDelta <= rgtDelta) {
if (lftDelta <= hoverProximityPx) {
hoveredIdx = nonNullLft!;
}
} else {
if (rgtDelta <= hoverProximityPx) {
hoveredIdx = nonNullRgt!;
}
}
}
return hoveredIdx;
return null;
},
skip: [null],
},
};

View File

@ -3276,7 +3276,7 @@ __metadata:
tinycolor2: "npm:1.6.0"
tslib: "npm:2.6.2"
typescript: "npm:5.3.3"
uplot: "npm:1.6.29"
uplot: "npm:1.6.30"
xss: "npm:^1.0.14"
peerDependencies:
react: ^17.0.0 || ^18.0.0
@ -3864,7 +3864,7 @@ __metadata:
tinycolor2: "npm:1.6.0"
tslib: "npm:2.6.2"
typescript: "npm:5.3.3"
uplot: "npm:1.6.29"
uplot: "npm:1.6.30"
uuid: "npm:9.0.1"
webpack: "npm:5.90.0"
peerDependencies:
@ -17680,7 +17680,7 @@ __metadata:
tslib: "npm:2.6.2"
tween-functions: "npm:^1.2.0"
typescript: "npm:5.3.3"
uplot: "npm:1.6.29"
uplot: "npm:1.6.30"
uuid: "npm:9.0.1"
visjs-network: "npm:4.25.0"
webpack: "npm:5.90.0"
@ -29633,10 +29633,10 @@ __metadata:
languageName: node
linkType: hard
"uplot@npm:1.6.29":
version: 1.6.29
resolution: "uplot@npm:1.6.29"
checksum: 0cda54a89cbe7f1ff5eb4a3e75bafa145a6af79e534cef6e8e4a1ace042b18a63ea56b25407d87fc7f073711ebb947a59de8068aa3576bc86b2514f1d058c187
"uplot@npm:1.6.30":
version: 1.6.30
resolution: "uplot@npm:1.6.30"
checksum: 08ad2f9441b8cfa26d9219362cdbb6f2dc6e5ec9403382df71798ed3e9d3666e7712c8a245e652ebb4a25c5d911cbdc614b52dfd891c0f7ce8705320d2eff81f
languageName: node
linkType: hard