1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 /* jshint undef: false */
7 openerp.web_graph = function (instance) {
10 var _lt = instance.web._lt;
11 var _t = instance.web._t;
12 var QWeb = instance.web.qweb;
14 instance.web.views.add('graph', 'instance.web_graph.GraphView');
19 instance.web_graph.GraphView = instance.web.View.extend({
20 template: 'GraphView',
21 display_name: _lt('Graph'),
25 'click .graph_mode_selection li' : 'mode_selection',
26 'click .graph_measure_selection li' : 'measure_selection',
27 'click .graph_expand_selection li' : 'expand_selection',
28 'click .graph_options_selection li' : 'option_selection',
29 'click .web_graph_click' : 'cell_click_callback',
30 'click a.field-selection' : 'field_selection',
33 init: function(parent, dataset, view_id, options) {
35 this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});
36 this.dataset = dataset;
37 this.pivot_table = new openerp.web_graph.PivotTable(this.model, dataset.domain);
38 this.set_default_options(options);
40 this.mode = 'pivot'; // pivot, bar_chart, line_chart, pie_chart, heatmap, row_heatmap, col_heatmap
41 this.measure_list = [];
42 this.important_fields = [];
43 this.search_view = parent.searchview;
44 this.groupby_mode = 'default'; // 'default' or 'manual'
45 this.default_row_groupby = [];
46 this.default_col_groupby = [];
48 get_context: this.proxy('get_context'),
49 get_domain: function () {},
50 get_groupby: function () { },
54 get_context: function (facet) {
55 var col_group_by = _.map(facet.values.models, function (model) {
56 return model.attributes.value.attrs.context.col_group_by;
58 return {col_group_by : col_group_by};
62 this.table = $('<table></table>');
63 this.$('.graph_main_content').append(this.table);
64 instance.web.bus.on('click', this, function (ev) {
66 this.dropdown.remove();
70 return this.load_view();
73 view_loading: function (fields_view_get) {
77 if (fields_view_get.arch.attrs.type === 'bar') {
78 this.mode = 'bar_chart';
81 // get the default groupbys and measure defined in the field view
82 _.each(fields_view_get.arch.children, function (field) {
83 if ('name' in field.attrs) {
84 if ('operator' in field.attrs) {
85 self.measure_list.push(field.attrs.name);
87 if (self.measure_list.length) {
88 self.default_col_groupby.push(field.attrs.name);
90 self.default_row_groupby.push(field.attrs.name);
95 if (this.measure_list.length > 0) {
96 measure = this.measure_list[0];
97 this.pivot_table.set_measure(measure);
100 // get the most important fields (of the model) by looking at the
101 // groupby filters defined in the search view
102 var options = {model:this.model, view_type: 'search'},
103 deferred1 = instance.web.fields_view_get(options).then(function (search_view) {
104 var groups = _.select(search_view.arch.children, function (c) {
105 return (c.tag == 'group') && (c.attrs.string != 'Display');
107 _.each(groups, function(g) {
108 _.each(g.children, function (g) {
109 if (g.attrs.context) {
110 var field_id = py.eval(g.attrs.context).group_by;
111 self.important_fields.push(field_id);
117 // get the fields descriptions from the model
118 var deferred2 = this.model.call('fields_get', []).then(function (fs) {
120 var measure_selection = self.$('.graph_measure_selection');
121 _.each(self.measure_list, function (measure) {
122 var choice = $('<a></a>').attr('data-choice', measure)
124 .append(self.fields[measure].string);
125 measure_selection.append($('<li></li>').append(choice));
130 return $.when(deferred1, deferred2);
133 do_search: function (domain, context, group_by) {
135 col_groupby = context.col_group_by || []; // get_col_groupby('ColGroupBy');
137 if (group_by.length || col_groupby.length) {
138 this.groupby_mode = 'manual';
141 this.pivot_table.set_domain(domain);
142 if (this.groupby_mode === 'manual') {
143 this.pivot_table.set_row_groupby(group_by);
144 this.pivot_table.set_col_groupby(col_groupby);
146 this.pivot_table.set_row_groupby(_.toArray(this.default_row_groupby));
147 this.pivot_table.set_col_groupby(_.toArray(this.default_col_groupby));
152 do_show: function () {
153 this.do_push_state({});
154 return this._super();
157 display_data: function () {
158 var pivot = this.pivot_table;
159 if (pivot.stale_data) {
160 pivot.update_data().done(this.proxy('display_data'));
162 this.$('.graph_main_content svg').remove();
166 var msg = 'No data available. Try to remove any filter or add some data.';
167 this.table.append($('<tr><td>' + msg + '</td></tr>'));
169 var table_modes = ['pivot', 'heatmap', 'row_heatmap', 'col_heatmap'];
170 if (_.contains(table_modes, this.mode)) {
173 this.$('.graph_main_content').append($('<div><svg></svg></div>'));
174 var svg = this.$('.graph_main_content svg')[0];
175 openerp.web_graph.draw_chart(this.mode, this.pivot_table, svg);
181 /******************************************************************************
182 * Event handling methods...
183 ******************************************************************************/
184 handle_header_event: function (options) {
185 var pivot = this.pivot_table,
187 header = pivot.get_header(id),
188 dim = header.root.groupby.length;
190 if (header.is_expanded) {
192 this.register_groupby();
194 if (header.path.length < header.root.groupby.length) {
195 var field = header.root.groupby[header.path.length];
196 pivot.expand(id, field).then(this.proxy('register_groupby'));
198 this.display_dropdown({id:header.id,
199 target: $(options.event.target),
200 x: options.event.pageX,
201 y: options.event.pageY});
206 mode_selection: function (event) {
207 event.preventDefault();
208 var mode = event.target.attributes['data-mode'].nodeValue;
213 register_groupby: function() {
215 query = this.search_view.query;
216 this.groupby_mode = 'manual';
218 var rows = _.map(this.pivot_table.rows.groupby, function (group) {
219 return make_facet('GroupBy', group);
221 var cols = _.map(this.pivot_table.cols.groupby, function (group) {
222 return make_facet('ColGroupBy', group);
225 query.reset(rows.concat(cols));
227 function make_facet (category, fields) {
232 if (!(fields instanceof Array)) { fields = [fields]; }
233 if (category === 'GroupBy') {
234 cat_name = 'group_by';
236 backbone_field = self.search_view._s_groupby;
238 cat_name = 'col_group_by';
240 backbone_field = self.search_field;
242 values = _.map(fields, function (field) {
244 context[cat_name] = field;
245 return {label: self.fields[field].string, value: {attrs:{domain: [], context: context}}};
247 return {category:category, values: values, icon:icon, field: backbone_field};
251 measure_selection: function (event) {
252 event.preventDefault();
253 var measure = event.target.attributes['data-choice'].nodeValue;
254 this.pivot_table.set_measure((measure === '__count') ? null : measure);
258 expand_selection: function (event) {
259 event.preventDefault();
260 switch (event.target.attributes['data-choice'].nodeValue) {
262 this.pivot_table.fold_cols();
263 this.register_groupby();
266 this.pivot_table.fold_rows();
267 this.register_groupby();
270 this.pivot_table.fold_cols();
271 this.pivot_table.fold_rows();
272 this.register_groupby();
275 this.pivot_table.invalidate_data();
281 option_selection: function (event) {
282 event.preventDefault();
283 switch (event.target.attributes['data-choice'].nodeValue) {
285 this.pivot_table.swap_axis();
286 this.register_groupby();
288 case 'update_values':
289 this.pivot_table.stale_data = true;
293 // Export code... To do...
299 cell_click_callback: function (event) {
300 event.preventDefault();
301 event.stopPropagation();
302 var id = event.target.attributes['data-id'].nodeValue;
303 this.handle_header_event({id:id, event:event});
306 field_selection: function (event) {
308 id = event.target.attributes['data-id'].nodeValue,
309 field_id = event.target.attributes['data-field-id'].nodeValue;
310 event.preventDefault();
311 this.pivot_table.expand(id, field_id).then(function () {
312 self.register_groupby();
316 display_dropdown: function (options) {
318 pivot = this.pivot_table,
320 header_id: options.id,
321 fields: _.map(self.important_fields, function (field) {
322 return {id: field, value: self.fields[field].string};
324 this.dropdown = $(QWeb.render('field_selection', dropdown_options));
325 options.target.after(this.dropdown);
326 this.dropdown.css({position:'absolute',
329 this.$('.field-selection').next('.dropdown-menu').toggle();
332 /******************************************************************************
333 * Drawing pivot table methods...
334 ******************************************************************************/
335 draw_table: function () {
336 this.pivot_table.rows.main.title = 'Total';
337 this.pivot_table.cols.main.title = this.measure_label();
338 this.draw_top_headers();
339 _.each(this.pivot_table.rows.headers, this.proxy('draw_row'));
342 measure_label: function () {
343 var pivot = this.pivot_table;
344 return (pivot.measure) ? this.fields[pivot.measure].string : 'Quantity';
347 make_border_cell: function (colspan, rowspan) {
348 return $('<td></td>').addClass('graph_border')
349 .attr('colspan', colspan)
350 .attr('rowspan', rowspan);
353 make_header_title: function (header) {
354 return $('<span> </span>')
355 .addClass('web_graph_click')
357 .addClass((header.is_expanded) ? 'icon-minus-sign' : 'icon-plus-sign')
358 .append((header.title !== undefined) ? header.title : 'Undefined');
361 draw_top_headers: function () {
363 pivot = this.pivot_table,
364 height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})),
365 header_cells = [[this.make_border_cell(1, height)]];
367 function set_dim (cols) {
368 _.each(cols.children, set_dim);
369 if (cols.children.length === 0) {
370 cols.height = height - cols.path.length + 1;
374 cols.width = _.reduce(cols.children, function (sum,c) { return sum + c.width;}, 0);
378 function make_col_header (col) {
379 var cell = self.make_border_cell(col.width, col.height);
380 return cell.append(self.make_header_title(col).attr('data-id', col.id));
383 function make_cells (queue, level) {
385 queue = _.rest(queue).concat(col.children);
386 if (col.path.length == level) {
387 _.last(header_cells).push(make_col_header(col));
390 header_cells.push([make_col_header(col)]);
392 if (queue.length !== 0) {
393 make_cells(queue, level);
397 set_dim(pivot.cols.main); // add width and height info to columns headers
398 if (pivot.cols.main.children.length === 0) {
399 make_cells(pivot.cols.headers, 0);
401 make_cells(pivot.cols.main.children, 1);
402 header_cells[0].push(self.make_border_cell(1, height).append('Total').css('font-weight', 'bold'));
405 _.each(header_cells, function (cells) {
406 self.table.append($('<tr></tr>').append(cells));
410 draw_row: function (row) {
412 pivot = this.pivot_table,
413 html_row = $('<tr></tr>'),
414 row_header = this.make_border_cell(1,1)
415 .append(this.make_header_title(row).attr('data-id', row.id))
416 .addClass('graph_border');
418 for (var i in _.range(row.path.length)) {
419 row_header.prepend($('<span/>', {class:'web_graph_indent'}));
422 html_row.append(row_header);
424 _.each(pivot.cols.headers, function (col) {
425 if (col.children.length === 0) {
426 var value = pivot.get_value(row.id, col.id),
427 cell = make_cell(value, col);
428 html_row.append(cell);
432 if (pivot.cols.main.children.length > 0) {
433 var cell = make_cell(pivot.get_total(row), pivot.cols.main)
434 .css('font-weight', 'bold');
435 html_row.append(cell);
438 this.table.append(html_row);
440 function make_cell (value, col) {
443 cell = $('<td></td>');
444 if ((self.mode === 'pivot') && (row.is_expanded) && (row.path.length <=2)) {
445 color = row.path.length * 5 + 240;
446 cell.css('background-color', $.Color(color, color, color));
448 if (value === undefined) {
452 if (self.mode === 'heatmap') {
453 total = pivot.get_total();
454 color = Math.floor(50 + 205*(total - value)/total);
455 cell.css('background-color', $.Color(255, color, color));
457 if (self.mode === 'row_heatmap') {
458 total = pivot.get_total(row);
459 color = Math.floor(50 + 205*(total - value)/total);
460 cell.css('background-color', $.Color(255, color, color));
462 if (self.mode === 'col_heatmap') {
463 total = pivot.get_total(col);
464 color = Math.floor(50 + 205*(total - value)/total);
465 cell.css('background-color', $.Color(255, color, color));