From 76216fd3f33fca6461bc96caf525cbcdc0f24d6a Mon Sep 17 00:00:00 2001 From: Gery Debongnie Date: Wed, 27 Nov 2013 12:19:53 +0100 Subject: [PATCH] [REF] puts helper functions inside a closure to simplify the calls to these functions (addon web_graph) bzr revid: ged@openerp.com-20131127111953-yfzdyllt1ty70xtu --- addons/web_graph/static/src/js/charts.js | 127 ++++++++++++----------- addons/web_graph/static/src/js/pivot.js | 29 +++--- 2 files changed, 80 insertions(+), 76 deletions(-) diff --git a/addons/web_graph/static/src/js/charts.js b/addons/web_graph/static/src/js/charts.js index 3f4bed6e001..2ac84085ce1 100644 --- a/addons/web_graph/static/src/js/charts.js +++ b/addons/web_graph/static/src/js/charts.js @@ -1,77 +1,78 @@ openerp.web_graph.draw_chart = function (mode, pivot, svg) { - var values = _.map(pivot.rows.main.children, function (pt) { + + var data = _.map(pivot.rows.main.children, function (pt) { var val = pivot.get_value(pt.id, pivot.cols.main.id); return {x: pt.title, y: val}; }); - + switch (mode) { case 'bar_chart': - openerp.web_graph.bar_chart(values, svg); + bar_chart(); break; case 'line_chart': - openerp.web_graph.line_chart(values, svg); + line_chart(); break; case 'pie_chart': - openerp.web_graph.pie_chart(values, svg); + pie_chart(); break; } + + function bar_chart () { + nv.addGraph(function () { + var chart = nv.models.discreteBarChart() + .tooltips(false) + .showValues(true) + .staggerLabels(true) + .width(650) + .height(400); + + d3.select(svg) + .datum([{key: 'Bar chart', values:data}]) + .attr('width', 650) + .attr('height', 400) + .call(chart); + + nv.utils.windowResize(chart.update); + return chart; + }); + }; + + function line_chart () { + nv.addGraph(function () { + var chart = nv.models.lineChart() + .x(function (d,u) { return u; }) + .width(600) + .height(300) + .margin({top: 30, right: 20, bottom: 20, left: 60}); + + d3.select(svg) + .attr('width', 600) + .attr('height', 300) + .datum([{key: 'Bar chart', values: data}]) + .call(chart); + + return chart; + }); + }; + + function pie_chart () { + nv.addGraph(function () { + var chart = nv.models.pieChart() + .color(d3.scale.category10().range()) + .width(650) + .height(400); + + d3.select(svg) + .datum(data) + .transition().duration(1200) + .attr('width', 650) + .attr('height', 400) + .call(chart); + + nv.utils.windowResize(chart.update); + return chart; + }); + }; + } - -openerp.web_graph.bar_chart = function (data, svg) { - nv.addGraph(function () { - var chart = nv.models.discreteBarChart() - .tooltips(false) - .showValues(true) - .staggerLabels(true) - .width(650) - .height(400); - - d3.select(svg) - .datum([{key: 'Bar chart', values:data}]) - .attr('width', 650) - .attr('height', 400) - .call(chart); - - nv.utils.windowResize(chart.update); - return chart; - }); -}; - -openerp.web_graph.line_chart = function (data, svg) { - nv.addGraph(function () { - var chart = nv.models.lineChart() - .x(function (d,u) { return u; }) - .width(600) - .height(300) - .margin({top: 30, right: 20, bottom: 20, left: 60}); - - d3.select(svg) - .attr('width', 600) - .attr('height', 300) - .datum([{key: 'Bar chart', values: data}]) - .call(chart); - - return chart; - }); -}; - -openerp.web_graph.pie_chart = function(data, svg) { - nv.addGraph(function () { - var chart = nv.models.pieChart() - .color(d3.scale.category10().range()) - .width(650) - .height(400); - - d3.select(svg) - .datum(data) - .transition().duration(1200) - .attr('width', 650) - .attr('height', 400) - .call(chart); - - 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 2359ed30601..08b406661cd 100644 --- a/addons/web_graph/static/src/js/pivot.js +++ b/addons/web_graph/static/src/js/pivot.js @@ -1,4 +1,6 @@ +(function () { + openerp.web_graph.PivotTable = openerp.web.Class.extend({ id_seed: 0, @@ -49,7 +51,7 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({ start: function () { var self = this, initial_group = this.expand(this.rows.main.id, this.rows.groupby[0]), - total = openerp.web_graph.query_groups(this.model, this.measure, this.domain, []) + total = query_groups(this.model, this.measure, this.domain, []) .then(function (total) { var val = total[0].attributes.aggregates[self.measure]; self.set_value(self.rows.main.id, self.cols.main.id, val); @@ -125,7 +127,7 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({ } var otherDim = (row.root === this.cols) ? this.rows : this.cols; - return openerp.web_graph.query_groups_data(this.model, this.visible_fields(), row.domain, otherDim.groupby, field_id) + return query_groups_data(this.model, this.visible_fields(), row.domain, otherDim.groupby, field_id) .then(function (groups) { _.each(groups.reverse(), function (group) { var new_row_id = self.make_header(group, row); @@ -159,7 +161,7 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({ root: parent.root, }; parent.children.splice(0,0, new_header); - openerp.web_graph.insertAfter(parent.root.headers, parent, new_header); + insertAfter(parent.root.headers, parent, new_header); return new_header.id; }, @@ -319,7 +321,7 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({ groupbys = [rows, []]; } def_array = _.map(groupbys, function (groupby) { - return openerp.web_graph.query_groups(self.model, self.visible_fields(), self.domain, groupby); + return query_groups(self.model, self.visible_fields(), self.domain, groupby); }); return $.when.apply(null, def_array).then(function () { @@ -434,14 +436,14 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({ }); -openerp.web_graph.removeFromArray = function (array, element) { +removeFromArray = function (array, element) { var index = array.indexOf(element); if (index > -1) { array.splice(index, 1); } } -openerp.web_graph.insertAfter = function (array, after, elem) { +insertAfter = function (array, after, elem) { array.splice(array.indexOf(after) + 1, 0, elem); } @@ -450,7 +452,7 @@ openerp.web_graph.insertAfter = function (array, after, elem) { * with all the groupbys applied (this is done for now, but the goal * is to modify read_group in order to allow eager and lazy groupbys */ -openerp.web_graph.query_groups = function (model, fields, domain, groupbys) { +query_groups = function (model, fields, domain, groupbys) { return model.query(fields) .filter(domain) .group_by(groupbys) @@ -464,7 +466,7 @@ openerp.web_graph.query_groups = function (model, fields, domain, groupbys) { var get_subgroups = $.when.apply(null, _.map(non_empty_results, function (result) { var new_domain = result.model._domain; var new_groupings = groupbys.slice(1); - return openerp.web_graph.query_groups(model, fields,new_domain, new_groupings).then(function (subgroups) { + return query_groups(model, fields,new_domain, new_groupings).then(function (subgroups) { result.subgroups_data = subgroups; }); })); @@ -475,20 +477,21 @@ openerp.web_graph.query_groups = function (model, fields, domain, groupbys) { }); } -openerp.web_graph.query_groups_data = function (model, fields, domain, row_groupbys, col_groupby) { - return openerp.web_graph.query_groups(model, fields, domain, [col_groupby].concat(row_groupbys)).then(function (groups) { +query_groups_data = function (model, fields, domain, row_groupbys, col_groupby) { + return query_groups(model, fields, domain, [col_groupby].concat(row_groupbys)).then(function (groups) { return _.map(groups, function (group) { - return openerp.web_graph.format_group(group, []); + return format_group(group, []); }); }); } -openerp.web_graph.format_group = function (group, path) { +format_group = function (group, path) { group.path = path.concat(group.attributes.value[1]); result = [group]; _.each(group.subgroups_data, function (subgroup) { - result = result.concat(openerp.web_graph.format_group (subgroup, group.path)); + result = result.concat(format_group (subgroup, group.path)); }); return result; } +})(); \ No newline at end of file