1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 /* jshint undef: false */
8 openerp.web_graph = function (instance) {
11 var _lt = instance.web._lt;
12 var _t = instance.web._t;
13 var QWeb = instance.web.qweb;
15 instance.web.views.add('graph', 'instance.web_graph.GraphView');
18 * GraphView view. It mostly contains a widget (PivotTable), some data, and
19 * calls to charts function.
21 instance.web_graph.GraphView = instance.web.View.extend({
22 template: 'GraphView',
23 display_name: _lt('Graph'),
27 'click .graph_mode_selection li' : function (event) {
28 event.preventDefault();
29 var mode = event.target.attributes['data-mode'].nodeValue;
30 this.switch_mode(mode);
33 'click .web_graph_click' : function (event) {
34 event.preventDefault();
35 var id = event.target.attributes['data-id'].nodeValue;
36 this.handle_header_event({id:id, event:event});
39 'click a.field-selection' : function (event) {
40 var id = event.target.attributes['data-id'].nodeValue,
41 field_id = event.target.attributes['data-field-id'].nodeValue;
42 event.preventDefault();
43 this.dropdown.remove();
44 this.pivot_table.expand(id, field_id)
45 .then(this.proxy('draw_table'));
48 'click label.graph_swap_axis' : function (event) {
49 this.pivot_table.swap_axis();
53 'click label.graph_fold_all' : function (event) {
54 this.pivot_table.fold_all();
58 'click label.graph_fold_rows' : function (event) {
59 this.pivot_table.fold_rows();
63 'click label.graph_fold_cols' : function (event) {
64 this.pivot_table.fold_cols();
68 'click label.graph_heat_map' : function (event) {
69 this.heat_map_mode = !this.heat_map_mode;
73 'click label.graph_expand_all' : function (event) {
74 this.pivot_table.expand_all().then(this.proxy('draw_table'));
77 'click label.graph_update_values' : function (event) {
78 this.pivot_table.update_values().then(this.proxy('draw_table'));
82 view_loading: function (fields_view_get) {
84 model = new instance.web.Model(fields_view_get.model, {group_by_no_leaf: true}),
88 important_fields = [],
92 this.pivot_table = null;
93 this.heat_map_mode = false;
95 // get the default groupbys and measure defined in the field view
96 _.each(fields_view_get.arch.children, function (field) {
97 if ('name' in field.attrs) {
98 if ('operator' in field.attrs) {
99 measure = field.attrs.name;
101 row_groupby.push(field.attrs.name);
106 // get the most important fields (of the model) by looking at the
107 // groupby filters defined in the search view
108 var load_view = instance.web.fields_view_get({
113 var important_fields_def = $.when(load_view).then(function (search_view) {
114 var groups = _.select(search_view.arch.children, function (c) {
115 return (c.tag == 'group') && (c.attrs.string != 'Display');
117 _.each(groups, function(g) {
118 _.each(g.children, function (g) {
119 if (g.attrs.context) {
120 var field_id = py.eval(g.attrs.context).group_by;
121 important_fields.push(field_id);
127 // get the fields descriptions from the model
128 var field_descr_def = model.call('fields_get', [])
129 .then(function (fs) { fields = fs; });
131 return $.when(important_fields_def, field_descr_def)
137 important_fields: important_fields,
139 measure_label: fields[measure].string,
141 row_groupby: row_groupby,
149 this.table = $('<table></table>');
150 this.svg = $('<div><svg></svg></div>');
151 this.$el.filter('.graph_main_content').append(this.table);
152 this.$el.filter('.graph_main_content').append(this.svg);
153 return this.load_view();
156 do_search: function (domain, context, group_by) {
158 this.data.domain = new instance.web.CompoundDomain(domain);
160 if (!this.pivot_table) {
161 self.pivot_table = new PivotTable(self.data);
162 self.pivot_table.start().then(self.proxy('draw_table'));
164 this.pivot_table.domain = domain;
165 this.pivot_table.update_values().done(function () {
171 do_show: function () {
172 this.do_push_state({});
173 return this._super();
176 switch_mode: function (mode) {
178 if (mode === 'pivot') {
179 this.table.css('display', 'block');
180 this.svg.css('display','none');
182 this.table.css('display', 'none');
184 this.svg = $('<div><svg></svg></div>');
185 this.$el.filter('.graph_main_content').append(this.svg);
186 draw_chart(mode, this.pivot_table);
191 handle_header_event: function (options) {
192 var pivot = this.pivot_table,
194 header = pivot.get_header(id);
196 if (header.is_expanded) {
200 if (header.path.length < header.root.groupby.length) {
201 var field = header.root.groupby[header.path.length];
202 pivot.expand(id, field).then(this.proxy('draw_table'));
204 this.display_dropdown({id:header.id,
205 target: $(options.event.target),
206 x: options.event.pageX,
207 y: options.event.pageY});
212 display_dropdown: function (options) {
214 pivot = this.pivot_table,
215 already_grouped = pivot.rows.groupby.concat(pivot.cols.groupby),
216 possible_groups = _.difference(self.data.important_fields, already_grouped),
218 header_id: options.id,
219 fields: _.map(possible_groups, function (field) {
220 return {id: field, value: self.data.fields[field].string};
223 this.dropdown = $(QWeb.render('field_selection', dropdown_options));
224 options.target.after(this.dropdown);
225 this.dropdown.css({position:'absolute',
228 $('.field-selection').next('.dropdown-menu').toggle();
231 draw_table: function () {
233 this.draw_top_headers();
234 _.each(this.pivot_table.rows.headers, this.proxy('draw_row'));
237 make_border_cell: function (colspan, rowspan) {
238 return $('<td></td>').addClass('graph_border')
239 .attr('colspan', colspan)
240 .attr('rowspan', rowspan);
243 make_header_title: function (header) {
244 return $('<span> </span>')
245 .addClass('web_graph_click')
247 .addClass((header.is_expanded) ? 'icon-minus-sign' : 'icon-plus-sign')
248 .append((header.title !== undefined) ? header.title : 'Undefined');
251 draw_top_headers: function () {
253 pivot = this.pivot_table,
254 height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})),
255 header_cells = [[this.make_border_cell(1, height)]];
257 function set_dim (cols) {
258 _.each(cols.children, set_dim);
259 if (cols.children.length === 0) {
260 cols.height = height - cols.path.length + 1;
264 cols.width = _.reduce(cols.children, function (sum,c) { return sum + c.width;}, 0);
268 function make_col_header (col) {
269 var cell = self.make_border_cell(col.width, col.height);
270 return cell.append(self.make_header_title(col).attr('data-id', col.id));
273 function make_cells (queue, level) {
275 queue = _.rest(queue).concat(col.children);
276 if (col.path.length == level) {
277 _.last(header_cells).push(make_col_header(col));
280 header_cells.push([make_col_header(col)]);
282 if (queue.length !== 0) {
283 make_cells(queue, level);
287 set_dim(pivot.cols.main); // add width and height info to columns headers
288 if (pivot.cols.main.children.length === 0) {
289 make_cells(pivot.cols.headers, 0);
291 make_cells(pivot.cols.main.children, 1);
292 header_cells[0].push(self.make_border_cell(1, height).append('Total').css('font-weight', 'bold'));
295 _.each(header_cells, function (cells) {
296 self.table.append($("<tr></tr>").append(cells));
300 draw_row: function (row) {
302 pivot = this.pivot_table,
303 html_row = $('<tr></tr>'),
304 row_header = this.make_border_cell(1,1)
305 .append(this.make_header_title(row).attr('data-id', row.id))
306 .addClass('graph_border');
308 for (var i in _.range(row.path.length)) {
309 row_header.prepend($('<span/>', {class:'web_graph_indent'}));
312 html_row.append(row_header);
314 _.each(pivot.cols.headers, function (col) {
315 if (col.children.length === 0) {
316 var value = pivot.get_value(row.id, col.id),
317 cell = $('<td></td>');
319 cell.append((value === undefined) ? '' : value);
320 if ((self.heat_map_mode) && (value !== undefined)) {
321 var color = Math.floor(50 + 205*(pivot.total - value)/pivot.total);
322 cell.css("background-color", "rgb(255," + color + "," + color + ")");
324 if (row.is_expanded && (row.path.length <= 2)) {
325 var color = row.path.length * 5 + 240;
326 cell.css("background-color", "rgb(" + [color, color, color].join() + ")");
328 html_row.append(cell);
331 var total = pivot.get_value(row.id, pivot.cols.main.id);
332 var cell = $('<td></td>')
334 .css('font-weight', 'bold');
335 if (row.is_expanded && (row.path.length <= 2)) {
336 var color = row.path.length * 5 + 240;
337 cell.css("background-color", "rgb(" + [color, color, color].join() + ")");
338 } else if (self.heat_map_mode) {
339 var color = Math.floor(50 + 205*(pivot.total - total)/pivot.total);
340 cell.css("background-color", "rgb(255," + color + "," + color + ")");
343 if (pivot.cols.main.children.length > 0) {
344 html_row.append(cell);
347 this.table.append(html_row);