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

This commit is contained in:
Leon Sorokin 2022-12-13 23:56:59 -06:00 committed by GitHub
parent 4064fa51c6
commit e0eacf1b04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -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',

View File

@ -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 ? `< ${fmt(step.value)}` : `${fmt(step.value)}+`,
label: `${pre}${fmt(value)}${suf}`,
color: theme.visualization.getColorByName(step.color),
yAxis: 1,
});
@ -510,10 +521,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);
}