MCPcopy Create free account
hub / github.com/apache/echarts / indicesOfNearest

Method indicesOfNearest

src/model/Series.ts:472–520  ·  view source on GitHub ↗

* Retrieve the index of nearest value in the view coordinate. * Data position is compared with each axis's dataToCoord. * * @param axisDim axis dimension * @param dim data dimension * @param value * @param [maxDistance=Infinity] The maximum distance in view coordinate s

(axisDim: DimensionName, dim: DimensionLoose, value: number, maxDistance?: number)

Source from the content-addressed store, hash-verified

470 * the same value, they are put to the result.
471 */
472 indicesOfNearest(axisDim: DimensionName, dim: DimensionLoose, value: number, maxDistance?: number): number[] {
473 const data = this.getData();
474 const coordSys = this.coordinateSystem;
475 const axis = coordSys && coordSys.getAxis(axisDim);
476 if (!coordSys || !axis) {
477 return [];
478 }
479 const targetCoord = axis.dataToCoord(value);
480
481 if (maxDistance == null) {
482 maxDistance = Infinity;
483 }
484
485 const nearestIndices: number[] = [];
486 let minDist = Infinity;
487 let minDiff = -1;
488 let nearestIndicesLen = 0;
489
490 // Performance-sensitive on large data (triggered by `tooltip`/`axisPointer` frequently).
491 const dimIdx = data.getDimensionIndex(dim);
492 const store = data.getStore();
493 for (let idx = 0, len = store.count(); idx < len; idx++) {
494 const dimValue = store.get(dimIdx, idx);
495 const dataCoord = axis.dataToCoord(dimValue);
496 const diff = targetCoord - dataCoord;
497 const dist = Math.abs(diff);
498 if (dist <= maxDistance) {
499 // When the `value` is at the middle of `this.get(dim, i)` and `this.get(dim, i+1)`,
500 // we'd better not push both of them to `nearestIndices`, otherwise it is easy to
501 // get more than one item in `nearestIndices` (more specifically, in `tooltip`).
502 // So we choose the one that `diff >= 0` in this case.
503 // But if `this.get(dim, i)` and `this.get(dim, j)` get the same value, both of them
504 // should be push to `nearestIndices`.
505 if (dist < minDist
506 || (dist === minDist && diff >= 0 && minDiff < 0)
507 ) {
508 minDist = dist;
509 minDiff = diff;
510 nearestIndicesLen = 0;
511 }
512 if (diff === minDiff) {
513 nearestIndices[nearestIndicesLen++] = idx;
514 }
515 }
516 }
517
518 nearestIndices.length = nearestIndicesLen;
519 return nearestIndices;
520 }
521
522 /**
523 * Default tooltip formatter

Callers 2

buildPayloadsBySeriesFunction · 0.80

Calls 7

getDataMethod · 0.95
dataToCoordMethod · 0.80
getDimensionIndexMethod · 0.80
getStoreMethod · 0.80
countMethod · 0.65
getAxisMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected