From b3b29f3b86f22b3157aba35e66e8ec3c16ee47bc Mon Sep 17 00:00:00 2001 From: Juhil Somaiya Date: Wed, 19 Dec 2018 12:17:19 +0000 Subject: [PATCH] [IMP] web: graph view legend and tooltips improvements This commit ameliorates the tooltips and the legend of the graph view in its different modes (bar, line, and pie). Related task id: 1917940 Co-authored-by: Mohammed Shekha Co-authored-by: Mathieu Duckerts-Antoine closes odoo/odoo#29747 --- addons/web/__manifest__.py | 2 +- .../static/src/js/views/graph/graph_model.js | 12 ++- .../src/js/views/graph/graph_renderer.js | 78 ++++++++++++++++--- addons/web/static/src/js/widgets/pie_chart.js | 2 +- addons/web/static/src/xml/chart.xml | 39 ++++++++++ addons/web/static/src/xml/pie_chart.xml | 10 --- 6 files changed, 117 insertions(+), 26 deletions(-) create mode 100644 addons/web/static/src/xml/chart.xml delete mode 100644 addons/web/static/src/xml/pie_chart.xml diff --git a/addons/web/__manifest__.py b/addons/web/__manifest__.py index e14aa45c82e..cdee2ff32e9 100644 --- a/addons/web/__manifest__.py +++ b/addons/web/__manifest__.py @@ -23,9 +23,9 @@ This module provides the core of the Odoo Web Client. ], 'qweb': [ "static/src/xml/base.xml", + "static/src/xml/chart.xml", "static/src/xml/kanban.xml", "static/src/xml/menu.xml", - "static/src/xml/pie_chart.xml", "static/src/xml/rainbow_man.xml", "static/src/xml/report.xml", "static/src/xml/web_calendar.xml", diff --git a/addons/web/static/src/js/views/graph/graph_model.js b/addons/web/static/src/js/views/graph/graph_model.js index 26c1aec6545..4c359a4778f 100644 --- a/addons/web/static/src/js/views/graph/graph_model.js +++ b/addons/web/static/src/js/views/graph/graph_model.js @@ -61,6 +61,7 @@ return AbstractModel.extend({ this.chart = { compare: params.compare, comparisonTimeRange: params.comparisonTimeRange, + comparisonTimeRangeDescription: params.comparisonTimeRangeDescription, data: [], groupedBy: params.groupedBy.length ? params.groupedBy : groupBys, // this parameter is not used anywhere for now. @@ -72,6 +73,7 @@ return AbstractModel.extend({ measure: params.context.graph_measure || params.measure, mode: params.context.graph_mode || params.mode, timeRange: params.timeRange, + timeRangeDescription: params.timeRangeDescription, domain: params.domain, context: params.context, }; @@ -100,14 +102,18 @@ return AbstractModel.extend({ this.chart.mode = params.context.graph_mode || this.chart.mode; var timeRangeMenuData = params.context.timeRangeMenuData; if (timeRangeMenuData) { - this.chart.timeRange = timeRangeMenuData.timeRange || []; this.chart.comparisonTimeRange = timeRangeMenuData.comparisonTimeRange || []; this.chart.compare = this.chart.comparisonTimeRange.length > 0; + this.chart.timeRange = timeRangeMenuData.timeRange || []; + this.chart.comparisonTimeRangeDescription = timeRangeMenuData.comparisonTimeRangeDescription; + this.chart.timeRangeDescription = timeRangeMenuData.timeRangeDescription; } else { - this.chart.timeRange = []; + this.chart = _.omit(this.chart, 'comparisonData'); this.chart.comparisonTimeRange = []; this.chart.compare = false; - this.chart = _.omit(this.chart, 'comparisonData'); + this.chart.timeRange = []; + this.chart.comparisonTimeRangeDescription = undefined; + this.chart.timeRangeDescription = undefined; } } if ('domain' in params) { 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 7de98b29e6b..549fa56eefa 100644 --- a/addons/web/static/src/js/views/graph/graph_renderer.js +++ b/addons/web/static/src/js/views/graph/graph_renderer.js @@ -145,9 +145,11 @@ return AbstractRenderer.extend({ if (this.state.groupedBy.length === 0) { data = [{ values: [{ - x: measure, + x: _t('Total'), y: this.state.data[0].value}], - key: measure + key: this.state.timeRangeDescription ? + this.state.timeRangeDescription: + measure, }]; } else if (this.state.groupedBy.length === 1) { values = this.state.data.map(function (datapt, index) { @@ -155,7 +157,9 @@ return AbstractRenderer.extend({ }); data.push({ values: values, - key: measure, + key: this.state.timeRangeDescription ? + this.state.timeRangeDescription: + measure, }); if (this.state.comparisonData) { values = this.state.comparisonData.map(function (datapt, index) { @@ -163,7 +167,9 @@ return AbstractRenderer.extend({ }); data.push({ values: values, - key: measure + ' (compare)', + key: this.state.comparisonTimeRangeDescription ? + this.state.comparisonTimeRangeDescription: + measure, color: '#ff7f0e', }); } @@ -237,6 +243,24 @@ return AbstractRenderer.extend({ }); }); + chart.tooltip.contentGenerator(function (data) { + var lines = data.series.map(function (serie) { + var label = data.value; + if (self.state.groupedBy.length > 1 || self.isComparison) { + label = label + "/" + serie.key; + } + return { + color: serie.color, + label: label, + value: serie.value, + }; + }); + return qweb.render("web.Chart.Tooltip", { + title: self.state.fields[self.state.measure].string, + lines: lines, + }); + }); + chart(svg); return chart; }, @@ -330,6 +354,17 @@ return AbstractRenderer.extend({ showLabels: all_zero ? false: true, }); + chart.tooltip.contentGenerator(function (data) { + return qweb.render("web.Chart.Tooltip", { + title: self.state.fields[self.state.measure].string, + lines : [{ + color: data.color, + label: data.data.x, + value: data.data.y, + }], + }); + }); + chart(svg); return chart; }, @@ -362,7 +397,9 @@ return AbstractRenderer.extend({ data.push({ area: true, values: values, - key: measure, + key: this.state.timeRangeDescription ? + this.state.timeRangeDescription: + measure, }); if (this.state.comparisonData && this.state.comparisonData.length > 0) { values = this.state.comparisonData.map(function (datapt, index) { @@ -370,17 +407,19 @@ return AbstractRenderer.extend({ }); data.push({ values: values, - key: measure + ' (compare)', + key: this.state.comparisonTimeRangeDescription ? + this.state.comparisonTimeRangeDescription: + measure, color: '#ff7f0e', }); } - for (i = 0; i < graphData.length; i++) { + for (var i = 0; i < graphData.length; i++) { ticksLabels.push(graphData[i].labels); } if (this.state.comparisonData && this.state.comparisonData.length > this.state.data.length) { var diff = this.state.comparisonData.length - this.state.data.length; - var length = self.state.data.length + var length = self.state.data.length; var diffTime = 0; if (length < self.state.data.length) { var date1 = moment(self.state.data[length - 1].labels[0]); @@ -447,6 +486,24 @@ return AbstractRenderer.extend({ chart.yAxis.tickPadding(5); chart.yAxis.orient("right"); + chart.interactiveLayer.tooltip.contentGenerator(function (data) { + var lines = data.series.map(function (serie) { + var label = ticksLabels[data.value]; + if (self.state.groupedBy.length > 1 || self.isComparison) { + label = label + "/" + serie.key; + } + return { + color: serie.color, + label: label, + value: serie.value, + }; + }); + return qweb.render("web.Chart.Tooltip", { + title: self.state.fields[self.state.measure].string, + lines: lines, + }); + }); + chart(svg); // Bigger line (stroke-width 1.5 is hardcoded in nv.d3) @@ -493,8 +550,7 @@ return AbstractRenderer.extend({ if (this.state.mode === 'pie' && this.isComparison) { // Render graph title - var timeRangeMenuData = this.state.context.timeRangeMenuData; - var chartTitle = this.title + ' (' + timeRangeMenuData.timeRangeDescription + ')'; + var chartTitle = this.title + ' (' + this.state.timeRangeDescription + ')'; this.$('.o_graph_svg_container').last().prepend($('