[v9.3.x] StateTimeline: Fix negative infinity legend/tooltip from thresholds (#60280)

StateTimeline: Fix negative infinity legend/tooltip from thresholds (#60279)

(cherry picked from commit e0eacf1b04)

Co-authored-by: Leon Sorokin <leeoniya@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2022-12-14 01:07:34 -05:00
committed by GitHub
co-authored by Leon Sorokin
parent 1437520da2
commit 32023f7122
2 changed files with 16 additions and 6 deletions
@@ -174,7 +174,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',
@@ -479,10 +479,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 ? `< ${fmt(step.value)}` : `${fmt(step.value)}+`,
label: `${pre}${fmt(value)}${suf}`,
color: theme.visualization.getColorByName(step.color),
yAxis: 1,
});
@@ -511,10 +522,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);
}