From 205dc0a2f6eb24643c0019273450c8774860b5ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 15 Feb 2021 13:57:18 +0100 Subject: [PATCH] Graph: Make axes unit option work even when field option unit is set (#31205) * Graph: Make axes unit option work even when field option unit is set * Updated tests --- public/app/plugins/panel/graph/axes_editor.ts | 8 +++++++- public/app/plugins/panel/graph/module.ts | 7 ++++++- public/app/plugins/panel/graph/specs/graph.test.ts | 3 +++ public/app/plugins/panel/graph/specs/graph_ctrl.test.ts | 3 +++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/panel/graph/axes_editor.ts b/public/app/plugins/panel/graph/axes_editor.ts index 40a12f3fecf..25816f09270 100644 --- a/public/app/plugins/panel/graph/axes_editor.ts +++ b/public/app/plugins/panel/graph/axes_editor.ts @@ -51,7 +51,13 @@ export class AxesEditorCtrl { setUnitFormat(axis: { format: any }) { return (unit: string) => { axis.format = unit; - this.panelCtrl.render(); + // if already set via field config we need to update that as well + if (this.panel.fieldConfig.defaults.unit) { + this.panel.fieldConfig.defaults.unit = unit; + this.panelCtrl.refresh(); + } else { + this.panelCtrl.render(); + } }; } diff --git a/public/app/plugins/panel/graph/module.ts b/public/app/plugins/panel/graph/module.ts index da49ff8253d..0ecf54024d1 100644 --- a/public/app/plugins/panel/graph/module.ts +++ b/public/app/plugins/panel/graph/module.ts @@ -156,6 +156,7 @@ export class GraphCtrl extends MetricsPanelCtrl { this.useDataFrames = true; this.processor = new DataProcessor(this.panel); this.contextMenuCtrl = new GraphContextMenuCtrl($scope); + this.annotationsPromise = Promise.resolve({ annotations: [] }); this.events.on(PanelEvents.render, this.onRender.bind(this)); this.events.on(PanelEvents.dataFramesReceived, this.onDataFramesReceived.bind(this)); @@ -163,7 +164,11 @@ export class GraphCtrl extends MetricsPanelCtrl { this.events.on(PanelEvents.editModeInitialized, this.onInitEditMode.bind(this)); this.events.on(PanelEvents.initPanelActions, this.onInitPanelActions.bind(this)); - this.annotationsPromise = Promise.resolve({ annotations: [] }); + // set axes format from field config + const fieldConfigUnit = this.panel.fieldConfig.defaults.unit; + if (fieldConfigUnit) { + this.panel.yaxes[0].format = fieldConfigUnit; + } } onInitEditMode() { diff --git a/public/app/plugins/panel/graph/specs/graph.test.ts b/public/app/plugins/panel/graph/specs/graph.test.ts index b528330dd00..082953b8441 100644 --- a/public/app/plugins/panel/graph/specs/graph.test.ts +++ b/public/app/plugins/panel/graph/specs/graph.test.ts @@ -78,6 +78,9 @@ describe('grafanaGraph', () => { tooltip: { shared: true, }, + fieldConfig: { + defaults: {}, + }, }, renderingCompleted: jest.fn(), hiddenSeries: {}, diff --git a/public/app/plugins/panel/graph/specs/graph_ctrl.test.ts b/public/app/plugins/panel/graph/specs/graph_ctrl.test.ts index 3f65a2ab747..a3404e6de84 100644 --- a/public/app/plugins/panel/graph/specs/graph_ctrl.test.ts +++ b/public/app/plugins/panel/graph/specs/graph_ctrl.test.ts @@ -30,6 +30,9 @@ describe('GraphCtrl', () => { gridPos: { w: 100, }, + fieldConfig: { + defaults: {}, + }, }; const ctx = {} as any;