diff options
| author | Gery Debongnie <ged@openerp.com> | 2013-12-10 16:39:51 +0100 |
|---|---|---|
| committer | Gery Debongnie <ged@openerp.com> | 2013-12-10 16:39:51 +0100 |
| commit | 28f27da6387b3f852cc2bc0408e2fd9f21b88d0a (patch) | |
| tree | 319662701774898953d499141bd66804ee131f4c /addons/web_graph/static/src | |
| parent | 1419b0793939de96bf67fc2e48d05a4fc55773d7 (diff) | |
[IMP] makes the graph view much smarter about the way it displays bar charts depending on the various groupby compbinations (addon web_graph)
bzr revid: ged@openerp.com-20131210153951-d36yymirvo09wbxd
Diffstat (limited to 'addons/web_graph/static/src')
| -rw-r--r-- | addons/web_graph/static/src/js/charts.js | 143 | ||||
| -rw-r--r-- | addons/web_graph/static/src/js/pivot.js | 4 |
2 files changed, 123 insertions, 24 deletions
diff --git a/addons/web_graph/static/src/js/charts.js b/addons/web_graph/static/src/js/charts.js index b56f06acd9f..b714e42392e 100644 --- a/addons/web_graph/static/src/js/charts.js +++ b/addons/web_graph/static/src/js/charts.js @@ -9,11 +9,32 @@ openerp.web_graph.bar_chart = function (pivot, svg) { dim_y = pivot.cols.groupby.length, data = []; + // No groupby ************************************************************** if ((dim_x === 0) && (dim_y === 0)) { - data = [{key: 'Expected Revenue', values:[{ + data = [{key: 'Total', values:[{ title: 'Total', value: pivot.get_value(pivot.rows.main.id, pivot.cols.main.id), }]}]; + nv.addGraph(function () { + var chart = nv.models.discreteBarChart() + .x(function(d) { return d.title;}) + .y(function(d) { return d.value;}) + .tooltips(false) + .showValues(true) + .staggerLabels(true) + .width(650) + .height(400); + + d3.select(svg) + .datum(data) + .attr('width', 650) + .attr('height', 400) + .call(chart); + + nv.utils.windowResize(chart.update); + return chart; + }); + // Only column groupbys **************************************************** } else if ((dim_x === 0) && (dim_y >= 1)){ _.each(pivot.cols.headers, function (header) { if (header.path.length === 1) { @@ -43,35 +64,113 @@ openerp.web_graph.bar_chart = function (pivot, svg) { return chart; }); - return; - } else - { + // Just 1 row groupby ****************************************************** + } else if ((dim_x === 1) && (dim_y === 0)) { data = _.map(pivot.rows.main.children, function (pt) { var value = pivot.get_value(pt.id, pivot.cols.main.id), title = (pt.title !== undefined) ? pt.title : 'Undefined'; return {title: title, value: value}; }); data = [{key: 'Bar chart', values:data}]; - } - nv.addGraph(function () { - var chart = nv.models.discreteBarChart() - .x(function(d) { return d.title;}) - .y(function(d) { return d.value;}) - .tooltips(false) - .showValues(true) - .staggerLabels(true) - .width(650) - .height(400); + nv.addGraph(function () { + var chart = nv.models.discreteBarChart() + .x(function(d) { return d.title;}) + .y(function(d) { return d.value;}) + .tooltips(false) + .showValues(true) + .staggerLabels(true) + .width(650) + .height(400); - d3.select(svg) - .datum(data) - .attr('width', 650) - .attr('height', 400) - .call(chart); + d3.select(svg) + .datum(data) + .attr('width', 650) + .attr('height', 400) + .call(chart); - nv.utils.windowResize(chart.update); - return chart; - }); + nv.utils.windowResize(chart.update); + return chart; + }); + // 1 row groupby and some col groupbys************************************** + } else if ((dim_x === 1) && (dim_y >= 1)) { + data = []; + _.each(pivot.cols.headers, function (colhdr) { + if (colhdr.path.length === 1) { + var values = []; + _.each(pivot.rows.headers, function (header) { + if (header.path.length === 1) { + values.push({ + x: header.title || 'Undefined', + y: pivot.get_value(header.id, colhdr.id, 0) + }); + } + }); + data.push({key: colhdr.title || 'Undefined', values: values}); + } + }); + + nv.addGraph(function () { + var chart = nv.models.multiBarChart() + .stacked(true) + .staggerLabels(true) + .tooltips(false) + .width(650) + .height(400); + + d3.select(svg) + .datum(data) + .attr('width', 650) + .attr('height', 400) + .call(chart); + + nv.utils.windowResize(chart.update); + return chart; + }); + // At least two row groupby************************************************* + } else { + data = []; + var keys = _.uniq(_.map(_.filter(pivot.rows.headers, function (hdr) { + return hdr.path.length === 2; + }), function (hdr) { + return hdr.title || 'Undefined'; + })); + data = _.map(keys, function (key) { + var values = []; + _.each(pivot.rows.headers, function (hdr) { + if (hdr.path.length === 1) { + var subhdr = _.find(hdr.children, function (child) { + return ((child.title === key) || ((child.title === undefined) && (key === 'Undefined'))); + }); + values.push({ + x: hdr.title || 'Undefined', + y: (subhdr) ? pivot.get_total(subhdr) : 0 + }); + } + }); + + return {key:key, values: values}; + }); + + nv.addGraph(function () { + var chart = nv.models.multiBarChart() + .stacked(true) + .staggerLabels(true) + .tooltips(false) + .transitionDuration(0) + .width(650) + .height(400); + + d3.select(svg) + .datum(data) + .attr('width', 650) + .attr('height', 400) + .call(chart); + debugger; + + nv.utils.windowResize(chart.update); + return chart; + }); + } }; diff --git a/addons/web_graph/static/src/js/pivot.js b/addons/web_graph/static/src/js/pivot.js index 9b1901e3817..b73939193a0 100644 --- a/addons/web_graph/static/src/js/pivot.js +++ b/addons/web_graph/static/src/js/pivot.js @@ -73,13 +73,13 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({ } }, - get_value: function (id1, id2) { + get_value: function (id1, id2, default_value) { var x = Math.min(id1, id2), y = Math.max(id1, id2), cell = _.find(this.cells, function (c) { return ((c.x == x) && (c.y == y)); }); - return (cell === undefined) ? undefined : cell.value; + return (cell === undefined) ? default_value : cell.value; }, is_row: function (id) { |
