From f28ef544bfe31475f6d1ce2708d261946ccaa8ed Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Tue, 13 Dec 2022 21:43:26 -0600 Subject: [PATCH 1/3] StateTimeline: Fix negative infinity legend/tooltip from thresholds --- public/app/plugins/panel/state-timeline/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/app/plugins/panel/state-timeline/utils.ts b/public/app/plugins/panel/state-timeline/utils.ts index 4cc969aa25c2..09228a825c62 100644 --- a/public/app/plugins/panel/state-timeline/utils.ts +++ b/public/app/plugins/panel/state-timeline/utils.ts @@ -481,7 +481,7 @@ export function getThresholdItems(fieldConfig: FieldConfig, theme: GrafanaTheme2 for (let i = 1; i <= steps.length; i++) { const step = steps[i - 1]; items.push({ - label: i === 1 ? `< ${fmt(step.value)}` : `${fmt(step.value)}+`, + label: i === 1 ? `< ${fmt(steps[i].value)}` : `${fmt(step.value)}+`, color: theme.visualization.getColorByName(step.color), yAxis: 1, }); From 83b34341f210b14e8934b750908391a84823d86c Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Tue, 13 Dec 2022 22:25:09 -0600 Subject: [PATCH 2/3] fixy fixy --- public/app/plugins/panel/state-timeline/utils.test.ts | 2 +- public/app/plugins/panel/state-timeline/utils.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/panel/state-timeline/utils.test.ts b/public/app/plugins/panel/state-timeline/utils.test.ts index 9644a27dd697..cff8d46a5959 100644 --- a/public/app/plugins/panel/state-timeline/utils.test.ts +++ b/public/app/plugins/panel/state-timeline/utils.test.ts @@ -182,7 +182,7 @@ describe('getThresholdItems', () => { }); describe('prepareTimelineLegendItems', () => { - it('should return legend items', () => { + it('should return legend items without crashing when single (base) threshold', () => { const frame: any = [ { refId: 'A', diff --git a/public/app/plugins/panel/state-timeline/utils.ts b/public/app/plugins/panel/state-timeline/utils.ts index 09228a825c62..6d46ddee495e 100644 --- a/public/app/plugins/panel/state-timeline/utils.ts +++ b/public/app/plugins/panel/state-timeline/utils.ts @@ -481,7 +481,7 @@ export function getThresholdItems(fieldConfig: FieldConfig, theme: GrafanaTheme2 for (let i = 1; i <= steps.length; i++) { const step = steps[i - 1]; items.push({ - label: i === 1 ? `< ${fmt(steps[i].value)}` : `${fmt(step.value)}+`, + label: i > 1 || steps.length === 1 ? `${fmt(step.value)}+` : `< ${fmt(steps[i].value)}`, color: theme.visualization.getColorByName(step.color), yAxis: 1, }); @@ -510,10 +510,9 @@ export function getFieldLegendItem(fields: Field[], theme: GrafanaTheme2): VizLe const items: VizLegendItem[] = []; const fieldConfig = fields[0].config; const colorMode = fieldConfig.color?.mode ?? FieldColorModeId.Fixed; - const thresholds = fieldConfig.thresholds; // If thresholds are enabled show each step in the legend - if (colorMode === FieldColorModeId.Thresholds && thresholds?.steps && thresholds.steps.length > 1) { + if (colorMode === FieldColorModeId.Thresholds) { return getThresholdItems(fieldConfig, theme); } From 4480d7f6c1c201a383fcb91b79384faf4e4d23d0 Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Tue, 13 Dec 2022 23:27:40 -0600 Subject: [PATCH 3/3] more better --- .../app/plugins/panel/state-timeline/utils.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/panel/state-timeline/utils.ts b/public/app/plugins/panel/state-timeline/utils.ts index 6d46ddee495e..ba2b7db6a8c8 100644 --- a/public/app/plugins/panel/state-timeline/utils.ts +++ b/public/app/plugins/panel/state-timeline/utils.ts @@ -478,10 +478,21 @@ export function getThresholdItems(fieldConfig: FieldConfig, theme: GrafanaTheme2 const fmt = (v: number) => formattedValueToString(disp(v)); - for (let i = 1; i <= steps.length; i++) { - const step = steps[i - 1]; + for (let i = 0; i < steps.length; i++) { + let step = steps[i]; + let value = step.value; + let pre = ''; + let suf = ''; + + if (value === -Infinity && i < steps.length - 1) { + value = steps[i + 1].value; + pre = '< '; + } else { + suf = '+'; + } + items.push({ - label: i > 1 || steps.length === 1 ? `${fmt(step.value)}+` : `< ${fmt(steps[i].value)}`, + label: `${pre}${fmt(value)}${suf}`, color: theme.visualization.getColorByName(step.color), yAxis: 1, });