summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views/graph/graph_renderer.js
diff options
context:
space:
mode:
authorMathieu Duckerts-Antoine <dam@odoo.com>2020-08-10 14:27:58 +0000
committerMathieu Duckerts-Antoine <dam@odoo.com>2020-08-12 08:52:20 +0000
commitae58021255274d3733e58ee8b020ca275aaa430d (patch)
tree926009fc46bb86c608d76044a3a4de52119555a2 /addons/web/static/src/js/views/graph/graph_renderer.js
parentfd6182443f68af3b6418195b13a4ab6c40599572 (diff)
[IMP] web: sample data in graph view
A previous commit has paved the way for displaying sample data in the reporting views. The present commit brings the required changes to have sample data in graph views. Task ID: 2282196 Co-authored-by: Aaron Bohy <aab@odoo.com> Co-authored-by: Mathieu Duckerts-Antoine <dam@odoo.com>
Diffstat (limited to 'addons/web/static/src/js/views/graph/graph_renderer.js')
-rw-r--r--addons/web/static/src/js/views/graph/graph_renderer.js79
1 files changed, 45 insertions, 34 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 52608e7b9cd..5cbe3f75324 100644
--- a/addons/web/static/src/js/views/graph/graph_renderer.js
+++ b/addons/web/static/src/js/views/graph/graph_renderer.js
@@ -54,6 +54,7 @@ var MAX_LEGEND_LENGTH = 4 * (Math.max(1, config.device.size_class));
return AbstractRenderer.extend({
className: "o_graph_renderer",
+ sampleDataTargets: ['.o_graph_canvas_container'],
/**
* @override
* @param {Widget} parent
@@ -762,9 +763,30 @@ return AbstractRenderer.extend({
}
var dataPoints = this._filterDataPoints();
dataPoints = this._sortDataPoints(dataPoints);
- if (!dataPoints.length && this.state.mode !== 'pie') {
- this.$el.append(qweb.render('View.NoContentHelper'));
- } else if (this.isInDOM) {
+ if (this.isInDOM) {
+ this._renderTitle();
+
+ // detect if some pathologies are still present after the filtering
+ if (this.state.mode === 'pie') {
+ const someNegative = dataPoints.some(dataPt => dataPt.value < 0);
+ const somePositive = dataPoints.some(dataPt => dataPt.value > 0);
+ if (someNegative && somePositive) {
+ const context = {
+ title: _t("Invalid data"),
+ description: [
+ _t("Pie chart cannot mix positive and negative numbers. "),
+ _t("Try to change your domain to only display positive results")
+ ].join("")
+ };
+ this._renderNoContentHelper(context);
+ return;
+ }
+ }
+
+ if (this.state.isSample && !this.isEmbedded) {
+ this._renderNoContentHelper();
+ }
+
// only render the graph if the widget is already in the DOM (this
// happens typically after an update), otherwise, it will be
// rendered when the widget will be attached to the DOM (see
@@ -785,8 +807,6 @@ return AbstractRenderer.extend({
} else if (this.state.mode === 'pie') {
this._renderPieChart(dataPoints);
}
-
- this._renderTitle();
}
},
/**
@@ -876,6 +896,25 @@ return AbstractRenderer.extend({
});
},
/**
+ * @private
+ */
+ _renderNoContentHelper: function (context) {
+ let templateName;
+ if (!context && this.noContentHelp) {
+ templateName = "web.ActionHelper";
+ context = { noContentHelp: this.noContentHelp };
+ } else {
+ templateName = "web.NoContentHelper";
+ }
+ const template = document.createElement('template');
+ // FIXME: retrieve owl qweb instance via the env set on Component s.t.
+ // it also works in the tests (importing 'web.env' wouldn't). This won't
+ // be necessary as soon as this rendering will be written in owl.
+ const OwlQweb = owl.Component.env.qweb;
+ template.innerHTML = OwlQweb.renderToString(templateName, context);
+ this.el.append(template.content.firstChild);
+ },
+ /**
* create pie chart
*
* @private
@@ -883,38 +922,10 @@ return AbstractRenderer.extend({
*/
_renderPieChart: function (dataPoints) {
var self = this;
-
- // try to see if some pathologies are still present after the filtering
- var allNegative = true;
- var someNegative = false;
- var allZero = true;
- dataPoints.forEach(function (datapt) {
- allNegative = allNegative && (datapt.value < 0);
- someNegative = someNegative || (datapt.value < 0);
- allZero = allZero && (datapt.value === 0);
- });
- if (someNegative && !allNegative) {
- this.$el.empty();
- this.$el.append(qweb.render('View.NoContentHelper', {
- title: _t("Invalid data"),
- description: _t("Pie chart cannot mix positive and negative numbers. " +
- "Try to change your domain to only display positive results"),
- }));
- return;
- }
- if (allZero && !this.isEmbedded && this.state.origins.length === 1) {
- this.$el.empty();
- this.$el.append(qweb.render('View.NoContentHelper', {
- title: _t("Invalid data"),
- description: _t("Pie chart cannot display all zero numbers.. " +
- "Try to change your domain to display positive results"),
- }));
- return;
- }
-
// prepare data
var data = {};
var colors = [];
+ const allZero = dataPoints.every(dataPt => dataPt.value === 0);
if (allZero) {
// add fake data to display a pie chart with a grey zone associated
// with every origin