[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
This commit is contained in:
Gery Debongnie 2013-12-10 16:39:51 +01:00
parent 1419b07939
commit 28f27da638
2 changed files with 124 additions and 25 deletions

View File

@ -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);
d3.select(svg)
.datum(data)
.attr('width', 650)
.attr('height', 400)
.call(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;
});
}
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;
});
};

View File

@ -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) {