Skip to content

Commit f504c6d

Browse files
committed
refactor: simplify generate thresholds logic
Signed-off-by: Yulong Ruan <[email protected]>
1 parent dede33c commit f504c6d

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

src/plugins/explore/public/components/visualizations/bar_gauge/bar_gauge_utils.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,37 +152,36 @@ export const generateThresholds = (
152152
) => {
153153
const defaultColor = baseColor ?? getColors().statusGreen;
154154

155-
const filteredThresholds = thresholds.filter((t) => t.value <= maxBase);
155+
// sort thresholds by value and dedupe threshold by value
156+
thresholds = thresholds
157+
.sort((t1, t2) => t1.value - t2.value)
158+
.reduce((acc, t) => {
159+
const last = acc.pop();
160+
if (last) {
161+
if (last.value === t.value) {
162+
return [...acc, t];
163+
} else {
164+
return [...acc, last, t];
165+
}
166+
}
167+
return [...acc, t];
168+
}, [] as Threshold[]);
169+
156170
const filteredValueStops = valueStops
157171
.filter((v) => v <= maxBase && v >= minBase)
158172
.sort((a, b) => a - b);
159173
const result: Threshold[] = [];
160-
let lastBelowIndex = -1;
161-
let lastThreshold: Threshold | undefined;
162174

163-
for (let i = 0; i < filteredThresholds.length; i++) {
164-
const currentThreshold = filteredThresholds[i];
165-
166-
// Handle duplicate values - keep the latest one
167-
if (lastThreshold && lastThreshold.value === currentThreshold.value) {
168-
result.pop();
175+
const minThreshold: Threshold = { value: minBase, color: defaultColor };
176+
for (const threshold of thresholds) {
177+
if (minThreshold.value >= threshold.value) {
178+
minThreshold.color = threshold.color;
169179
}
170-
171-
result.push(currentThreshold);
172-
lastThreshold = currentThreshold;
173-
174-
// Track last threshold below minBase
175-
if (minBase >= currentThreshold.value) {
176-
lastBelowIndex = i;
180+
if (threshold.value > minThreshold.value && threshold.value <= maxBase) {
181+
result.push(threshold);
177182
}
178183
}
179-
180-
if (lastBelowIndex !== -1) {
181-
result.splice(0, lastBelowIndex);
182-
result[0] = { ...result[0], value: minBase };
183-
} else {
184-
result.unshift({ value: minBase, color: defaultColor });
185-
}
184+
result.unshift(minThreshold);
186185

187186
const valueResults: Threshold[] = [];
188187
if (filteredValueStops.length > 0 && result.length > 0) {

0 commit comments

Comments
 (0)