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;
34 'click .graph_measure_selection li' : function (event) {
35 event.preventDefault();
36 var measure = event.target.attributes['data-choice'].nodeValue;
37 this.measure = (measure === '__count') ? null : measure;
38 this.pivot_table.set_measure(this.measure)
39 .then(this.proxy('display_data'));
42 'click .graph_expand_selection li' : function (event) {
43 event.preventDefault();
44 switch (event.target.attributes['data-choice'].nodeValue) {
46 this.pivot_table.fold_cols();
50 this.pivot_table.fold_rows();
54 this.pivot_table.fold_all();
58 this.pivot_table.expand_all().then(this.proxy('display_data'));
63 'click .graph_options_selection li' : function (event) {
64 event.preventDefault();
65 switch (event.target.attributes['data-choice'].nodeValue) {
67 this.pivot_table.swap_axis();
71 this.pivot_table.update_values().then(this.proxy('display_data'));
74 // Export code... To do...
79 'click .web_graph_click' : function (event) {
80 event.preventDefault();
81 event.stopPropagation();
82 var id = event.target.attributes['data-id'].nodeValue;
83 this.handle_header_event({id:id, event:event});
86 'click a.field-selection' : function (event) {
87 var id = event.target.attributes['data-id'].nodeValue,
88 field_id = event.target.attributes['data-field-id'].nodeValue;
89 event.preventDefault();
90 this.pivot_table.expand(id, field_id)
91 .then(this.proxy('display_data'));
95 init: function(parent, dataset, view_id, options) {
97 this.dataset = dataset;
98 this.set_default_options(options);
100 this.pivot_table = null;
101 this.heat_map_mode = false;
104 this.measure_list = [];
105 this.important_fields = [];
109 this.table = $('<table></table>');
110 this.$('.graph_main_content').append(this.table);
111 instance.web.bus.on('click', this, function (ev) {
113 this.dropdown.remove();
114 this.dropdown = null;
117 return this.load_view();
120 view_loading: function (fields_view_get) {
122 model = new instance.web.Model(fields_view_get.model, {group_by_no_leaf: true}),
125 // get the default groupbys and measure defined in the field view
126 _.each(fields_view_get.arch.children, function (field) {
127 if ('name' in field.attrs) {
128 if ('operator' in field.attrs) {
129 self.measure_list.push(field.attrs.name);
131 row_groupby.push(field.attrs.name);
135 if (this.measure_list.length > 0) {
136 this.measure = this.measure_list[0];
139 // get the most important fields (of the model) by looking at the
140 // groupby filters defined in the search view
141 var options = {model:model, view_type: 'search'},
142 deferred1 = instance.web.fields_view_get(options).then(function (search_view) {
143 var groups = _.select(search_view.arch.children, function (c) {
144 return (c.tag == 'group') && (c.attrs.string != 'Display');
146 _.each(groups, function(g) {
147 _.each(g.children, function (g) {
148 if (g.attrs.context) {
149 var field_id = py.eval(g.attrs.context).group_by;
150 self.important_fields.push(field_id);
156 // get the fields descriptions from the model
157 var deferred2 = model.call('fields_get', []).then(function (fs) {
159 var measure_selection = self.$('.graph_measure_selection');
160 _.each(self.measure_list, function (measure) {
161 var choice = $('<a></a>').attr('data-choice', measure)
163 .append(self.fields[measure].string);
164 measure_selection.append($('<li></li>').append(choice));
169 return $.when(deferred1, deferred2).then(function () {
173 measure: self.measure,
175 row_groupby: row_groupby,
177 self.pivot_table = new openerp.web_graph.PivotTable(data);
181 do_search: function (domain, context, group_by) {
182 this.pivot_table.domain = domain;
183 this.pivot_table.update_values().done(this.proxy('display_data'));
186 do_show: function () {
187 this.do_push_state({});
188 return this._super();
191 display_data: function () {
192 this.$('.graph_main_content svg').remove();
195 if (this.pivot_table.no_data) {
196 var msg = 'No data available. Try to remove any filter or add some data.';
197 this.table.append($('<tr><td>' + msg + '</td></tr>'));
199 var table_modes = ['pivot', 'heatmap', 'row_heatmap', 'col_heatmap'];
200 if (_.contains(table_modes, this.mode)) {
203 this.$('.graph_main_content').append($('<div><svg></svg></div>'));
204 var svg = this.$('.graph_main_content svg')[0];
205 openerp.web_graph.draw_chart(this.mode, this.pivot_table, svg);
210 handle_header_event: function (options) {
211 var pivot = this.pivot_table,
213 header = pivot.get_header(id);
215 if (header.is_expanded) {
219 if (header.path.length < header.root.groupby.length) {
220 var field = header.root.groupby[header.path.length];
221 pivot.expand(id, field).then(this.proxy('display_data'));
223 this.display_dropdown({id:header.id,
224 target: $(options.event.target),
225 x: options.event.pageX,
226 y: options.event.pageY});
231 display_dropdown: function (options) {
233 pivot = this.pivot_table,
234 already_grouped = pivot.rows.groupby.concat(pivot.cols.groupby),
235 possible_groups = _.difference(self.important_fields, already_grouped),
237 header_id: options.id,
238 fields: _.map(possible_groups, function (field) {
239 return {id: field, value: self.fields[field].string};
242 this.dropdown = $(QWeb.render('field_selection', dropdown_options));
243 options.target.after(this.dropdown);
244 this.dropdown.css({position:'absolute',
247 this.$('.field-selection').next('.dropdown-menu').toggle();
250 measure_label: function () {
251 return (this.measure) ? this.fields[this.measure].string : 'Quantity';
254 draw_table: function () {
255 this.pivot_table.rows.main.title = 'Total';
256 this.pivot_table.cols.main.title = this.measure_label();
257 this.draw_top_headers();
258 _.each(this.pivot_table.rows.headers, this.proxy('draw_row'));
261 make_border_cell: function (colspan, rowspan) {
262 return $('<td></td>').addClass('graph_border')
263 .attr('colspan', colspan)
264 .attr('rowspan', rowspan);
267 make_header_title: function (header) {
268 return $('<span> </span>')
269 .addClass('web_graph_click')
271 .addClass((header.is_expanded) ? 'icon-minus-sign' : 'icon-plus-sign')
272 .append((header.title !== undefined) ? header.title : 'Undefined');
275 draw_top_headers: function () {
277 pivot = this.pivot_table,
278 height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})),
279 header_cells = [[this.make_border_cell(1, height)]];
281 function set_dim (cols) {
282 _.each(cols.children, set_dim);
283 if (cols.children.length === 0) {
284 cols.height = height - cols.path.length + 1;
288 cols.width = _.reduce(cols.children, function (sum,c) { return sum + c.width;}, 0);
292 function make_col_header (col) {
293 var cell = self.make_border_cell(col.width, col.height);
294 return cell.append(self.make_header_title(col).attr('data-id', col.id));
297 function make_cells (queue, level) {
299 queue = _.rest(queue).concat(col.children);
300 if (col.path.length == level) {
301 _.last(header_cells).push(make_col_header(col));
304 header_cells.push([make_col_header(col)]);
306 if (queue.length !== 0) {
307 make_cells(queue, level);
311 set_dim(pivot.cols.main); // add width and height info to columns headers
312 if (pivot.cols.main.children.length === 0) {
313 make_cells(pivot.cols.headers, 0);
315 make_cells(pivot.cols.main.children, 1);
316 header_cells[0].push(self.make_border_cell(1, height).append('Total').css('font-weight', 'bold'));
319 _.each(header_cells, function (cells) {
320 self.table.append($('<tr></tr>').append(cells));
324 draw_row: function (row) {
326 pivot = this.pivot_table,
327 html_row = $('<tr></tr>'),
328 row_header = this.make_border_cell(1,1)
329 .append(this.make_header_title(row).attr('data-id', row.id))
330 .addClass('graph_border');
332 for (var i in _.range(row.path.length)) {
333 row_header.prepend($('<span/>', {class:'web_graph_indent'}));
336 html_row.append(row_header);
338 _.each(pivot.cols.headers, function (col) {
339 if (col.children.length === 0) {
340 var value = pivot.get_value(row.id, col.id),
341 cell = make_cell(value, col);
342 html_row.append(cell);
346 if (pivot.cols.main.children.length > 0) {
347 var cell = make_cell(pivot.get_total(row), pivot.cols.main)
348 .css('font-weight', 'bold');
349 html_row.append(cell);
352 this.table.append(html_row);
354 function make_cell (value, col) {
357 cell = $('<td></td>');
358 if ((self.mode === 'pivot') && (row.is_expanded) && (row.path.length <=2)) {
359 color = row.path.length * 5 + 240;
360 cell.css('background-color', $.Color(color, color, color));
362 if (value === undefined) {
366 if (self.mode === 'heatmap') {
367 total = pivot.get_total();
368 color = Math.floor(50 + 205*(total - value)/total);
369 cell.css('background-color', $.Color(255, color, color));
371 if (self.mode === 'row_heatmap') {
372 total = pivot.get_total(row);
373 color = Math.floor(50 + 205*(total - value)/total);
374 cell.css('background-color', $.Color(255, color, color));
376 if (self.mode === 'col_heatmap') {
377 total = pivot.get_total(col);
378 color = Math.floor(50 + 205*(total - value)/total);
379 cell.css('background-color', $.Color(255, color, color));