in src/visual/VisualMapping.ts [475:540]
static findPieceIndex(value: number, pieceList: VisualMappingPiece[], findClosestWhenOutside?: boolean): number {
let possibleI: number;
let abs = Infinity;
// value has the higher priority.
for (let i = 0, len = pieceList.length; i < len; i++) {
const pieceValue = pieceList[i].value;
if (pieceValue != null) {
if (pieceValue === value
// FIXME
// It is supposed to compare value according to value type of dimension,
// but currently value type can exactly be string or number.
// Compromise for numeric-like string (like '12'), especially
// in the case that visualMap.categories is ['22', '33'].
|| (zrUtil.isString(pieceValue) && pieceValue === value + '')
) {
return i;
}
findClosestWhenOutside && updatePossible(pieceValue as number, i);
}
}
for (let i = 0, len = pieceList.length; i < len; i++) {
const piece = pieceList[i];
const interval = piece.interval;
const close = piece.close;
if (interval) {
if (interval[0] === -Infinity) {
if (littleThan(close[1], value, interval[1])) {
return i;
}
}
else if (interval[1] === Infinity) {
if (littleThan(close[0], interval[0], value)) {
return i;
}
}
else if (
littleThan(close[0], interval[0], value)
&& littleThan(close[1], value, interval[1])
) {
return i;
}
findClosestWhenOutside && updatePossible(interval[0], i);
findClosestWhenOutside && updatePossible(interval[1], i);
}
}
if (findClosestWhenOutside) {
return value === Infinity
? pieceList.length - 1
: value === -Infinity
? 0
: possibleI;
}
function updatePossible(val: number, index: number) {
const newAbs = Math.abs(val - value);
if (newAbs < abs) {
abs = newAbs;
possibleI = index;
}
}
}