* @param value * @param pieceList [{value: ..., interval: [min, max]}, ...] * Always from small to big. * @param findClosestWhenOutside Default to be false * @return index
(value: number, pieceList: VisualMappingPiece[], findClosestWhenOutside?: boolean)
| 473 | * @return index |
| 474 | */ |
| 475 | static findPieceIndex(value: number, pieceList: VisualMappingPiece[], findClosestWhenOutside?: boolean): number { |
| 476 | let possibleI: number; |
| 477 | let abs = Infinity; |
| 478 | |
| 479 | // value has the higher priority. |
| 480 | for (let i = 0, len = pieceList.length; i < len; i++) { |
| 481 | const pieceValue = pieceList[i].value; |
| 482 | if (pieceValue != null) { |
| 483 | if (pieceValue === value |
| 484 | // FIXME |
| 485 | // It is supposed to compare value according to value type of dimension, |
| 486 | // but currently value type can exactly be string or number. |
| 487 | // Compromise for numeric-like string (like '12'), especially |
| 488 | // in the case that visualMap.categories is ['22', '33']. |
| 489 | || (zrUtil.isString(pieceValue) && pieceValue === value + '') |
| 490 | ) { |
| 491 | return i; |
| 492 | } |
| 493 | findClosestWhenOutside && updatePossible(pieceValue as number, i); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | for (let i = 0, len = pieceList.length; i < len; i++) { |
| 498 | const piece = pieceList[i]; |
| 499 | const interval = piece.interval; |
| 500 | const close = piece.close; |
| 501 | |
| 502 | if (interval) { |
| 503 | if (interval[0] === -Infinity) { |
| 504 | if (littleThan(close[1], value, interval[1])) { |
| 505 | return i; |
| 506 | } |
| 507 | } |
| 508 | else if (interval[1] === Infinity) { |
| 509 | if (littleThan(close[0], interval[0], value)) { |
| 510 | return i; |
| 511 | } |
| 512 | } |
| 513 | else if ( |
| 514 | littleThan(close[0], interval[0], value) |
| 515 | && littleThan(close[1], value, interval[1]) |
| 516 | ) { |
| 517 | return i; |
| 518 | } |
| 519 | findClosestWhenOutside && updatePossible(interval[0], i); |
| 520 | findClosestWhenOutside && updatePossible(interval[1], i); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (findClosestWhenOutside) { |
| 525 | return value === Infinity |
| 526 | ? pieceList.length - 1 |
| 527 | : value === -Infinity |
| 528 | ? 0 |
| 529 | : possibleI; |
| 530 | } |
| 531 | |
| 532 | function updatePossible(val: number, index: number) { |
no test coverage detected