From 536c004967cf0218a1e412aef233b370c58ebdc9 Mon Sep 17 00:00:00 2001 From: Mohammed Shekha Date: Wed, 16 Dec 2020 08:48:53 +0000 Subject: [PATCH] [IMP] web: graph: improve measure labels before this commit: when graph measure field has string attribute and apply that field as measure then graph measure label uses field definition string instead of view string, due to this measure dropdown and graph measure label has different strings. after this commit: when applying measure graph measure label will display field string defined on view first else field definition string will be displayed. task-2264636 closes odoo/odoo#63422 Signed-off-by: Aaron Bohy (aab) --- .../src/js/views/graph/graph_renderer.js | 17 ++++-- .../static/src/js/views/graph/graph_view.js | 1 + addons/web/static/tests/views/graph_tests.js | 53 +++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/addons/web/static/src/js/views/graph/graph_renderer.js b/addons/web/static/src/js/views/graph/graph_renderer.js index 3b576485b4c..d8cc39a29df 100644 --- a/addons/web/static/src/js/views/graph/graph_renderer.js +++ b/addons/web/static/src/js/views/graph/graph_renderer.js @@ -103,6 +103,15 @@ odoo.define("web/static/src/js/views/graph/graph_renderer", function (require) { this._renderChart(); } + //--------------------------------------------------------------------- + // Getters + //--------------------------------------------------------------------- + + get measureDescription() { + const measure = this.props.measures.find(m => m.fieldName === this.props.measure); + return measure ? measure.description : this.props.fields[this.props.measure].string; + } + //--------------------------------------------------------------------- // Private //--------------------------------------------------------------------- @@ -312,7 +321,7 @@ odoo.define("web/static/src/js/views/graph/graph_renderer", function (require) { const innerHTML = this.env.qweb.renderToString("web.GraphRenderer.CustomTooltip", { maxWidth: getMaxWidth(this.chart.chartArea), - measure: this.props.fields[this.props.measure].string, + measure: this.measureDescription, tooltipItems: this._getTooltipItems(tooltipModel), }); const template = Object.assign(document.createElement("template"), { innerHTML }); @@ -460,8 +469,7 @@ odoo.define("web/static/src/js/views/graph/graph_renderer", function (require) { datasetLabel ? ("/" + datasetLabel) : "" ); } - datasetLabel = datasetLabel || this.props.fields[this.props.measure].string; - return datasetLabel; + return datasetLabel || this.measureDescription; } /** @@ -640,7 +648,7 @@ odoo.define("web/static/src/js/views/graph/graph_renderer", function (require) { type: "linear", scaleLabel: { display: !this.props.isEmbedded, - labelString: this.props.fields[this.props.measure].string, + labelString: this.measureDescription, }, ticks: { callback: value => this._formatValue(value), @@ -1028,6 +1036,7 @@ odoo.define("web/static/src/js/views/graph/graph_renderer", function (require) { isEmbedded: Boolean, isSample: { type: Boolean, optional: 1 }, measure: String, + measures: { type: Array, element: Object }, mode: { validate: m => ["bar", "line", "pie"].includes(m) }, origins: { type: Array, element: String }, processedGroupBy: { type: Array, element: String }, diff --git a/addons/web/static/src/js/views/graph/graph_view.js b/addons/web/static/src/js/views/graph/graph_view.js index 69344186422..99863a9270c 100644 --- a/addons/web/static/src/js/views/graph/graph_view.js +++ b/addons/web/static/src/js/views/graph/graph_view.js @@ -145,6 +145,7 @@ var GraphView = AbstractView.extend({ ]; this.rendererParams.fields = this.fields; + this.rendererParams.measures = this.controllerParams.measures; this.rendererParams.title = this.arch.attrs.title; // TODO: use attrs.string instead this.rendererParams.disableLinking = !!JSON.parse(this.arch.attrs.disable_linking || '0'); diff --git a/addons/web/static/tests/views/graph_tests.js b/addons/web/static/tests/views/graph_tests.js index 60d63e842c2..b72f56975f8 100644 --- a/addons/web/static/tests/views/graph_tests.js +++ b/addons/web/static/tests/views/graph_tests.js @@ -872,6 +872,59 @@ QUnit.module('Views', { graph.destroy(); }); + QUnit.test('graph view with string attribute on view architecture fields', async function (assert) { + assert.expect(4); + + const graph = await createView({ + View: GraphView, + model: "foo", + data: this.data, + arch: ` + + + + `, + }); + + checkLegend(assert, graph, 'Foo'); + let chart = graph.renderer.componentRef.comp.chart; + let yAxisLabel = chart.config.options.scales.yAxes[0].scaleLabel.labelString; + assert.strictEqual(yAxisLabel, "Foo"); + + await cpHelpers.toggleMenu(graph, "Measures"); + await cpHelpers.toggleMenuItem(graph, "Jethalal"); + checkLegend(assert, graph, 'Jethalal'); + + chart = graph.renderer.componentRef.comp.chart; + yAxisLabel = chart.config.options.scales.yAxes[0].scaleLabel.labelString; + assert.strictEqual(yAxisLabel, "Jethalal"); + + graph.destroy(); + }); + + QUnit.test('graph view "graph_measure" field in context not in measures', async function (assert) { + assert.expect(2); + + const graph = await createView({ + View: GraphView, + model: "foo", + data: this.data, + arch: '', + viewOptions: { + context: { + graph_measure: 'product_id', + }, + }, + }); + + checkLegend(assert, graph, 'Product'); + let chart = graph.renderer.componentRef.comp.chart; + let yAxisLabel = chart.config.options.scales.yAxes[0].scaleLabel.labelString; + assert.strictEqual(yAxisLabel, "Product"); + + graph.destroy(); + }); + QUnit.test('Undefined should appear in bar, pie graph but not in line graph', async function (assert) { assert.expect(3);