* 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)
| 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 |
no test coverage detected