3731d301b2b25e65c8fae2682721a6e07c5923f4
[odoo/odoo.git] / addons / web_graph / static / src / js / graph_widget.js
1
2 /* jshint undef: false  */
3
4 (function () {
5 'use strict';
6 var QWeb = openerp.web.qweb;
7 var _lt = openerp.web._lt;
8 var _t = openerp.web._t;
9
10 nv.dev = false;  // sets nvd3 library in production mode
11
12 openerp.web_graph.Graph = openerp.web.Widget.extend({
13     template: 'GraphWidget',
14
15     // ----------------------------------------------------------------------
16     // Init stuff
17     // ----------------------------------------------------------------------
18     init: function(parent, model,  domain, options) {
19         this._super(parent);
20         this.model = model;
21         this.domain = domain;
22         this.mode = options.mode || 'pivot';  // pivot, bar, pie, line
23         this.visible_ui = options.visible_ui || true;
24         this.bar_ui = options.bar_ui || 'group';
25         this.graph_view = options.graph_view || null;
26         this.pivot_options = options;
27         this.title = options.title || 'Data';
28         this.$buttons = options.$buttons;
29     },
30
31     start: function() {
32         var self = this;
33         this.table = $('<table>');
34         this.$('.graph_main_content').append(this.table);
35
36         this.$buttons.find('.oe-pivot-mode').click(function () {
37             self.set_mode.bind(self)('pivot');
38         });
39         this.$measure_list = this.$buttons.find('.oe-measure-list');
40
41         this.$buttons.find('.oe-bar-mode').click(function () {
42             self.set_mode.bind(self)('bar');
43         });
44         this.$buttons.find('.oe-line-mode').click(function () {
45             self.set_mode.bind(self)('line');
46         });
47         this.$buttons.find('.oe-pie-mode').click(function () {
48             self.set_mode.bind(self)('pie');
49         });
50         this.$buttons.find('.fa-expand').click(this.swap_axis.bind(this));
51         this.$buttons.find('.fa-arrows-alt').click(function () {
52             self.pivot.expand_all().then(self.proxy('display_data'));
53         });
54         this.$buttons.find('.fa-download').click(this.export_xls.bind(this));
55
56         var indexes = {'pivot': 0, 'bar': 1, 'line': 2, 'chart': 3};
57         this.$('.graph_mode_selection label').eq(indexes[this.mode]).addClass('selected');
58
59         if (this.mode !== 'pivot') {
60             this.$('.graph_heatmap label').addClass('disabled');
61             this.$('.graph_main_content').addClass('graph_chart_mode');
62         } else {
63             this.$('.graph_main_content').addClass('graph_pivot_mode');
64         }
65         // get search view
66         var parent = this.getParent();
67         while (!(parent instanceof openerp.web.ViewManager)) {
68             parent = parent.getParent();
69         }
70         this.search_view = parent.searchview;
71
72         openerp.session.rpc('/web_graph/check_xlwt').then(function (result) {
73             self.$('.graph_options_selection label').last().toggle(result);
74         });
75
76         return this.model.call('fields_get', []).then(function (f) {
77             self.fields = f;
78             self.fields.__count = {field:'__count', type: 'integer', string:_t('Quantity')};
79             self.groupby_fields = self.get_groupby_fields();
80             self.measure_list = self.get_measures();
81             self.add_measures_to_options();
82             self.pivot_options.row_groupby = self.create_field_values(self.pivot_options.row_groupby || []);
83             self.pivot_options.col_groupby = self.create_field_values(self.pivot_options.col_groupby || []);
84             self.pivot_options.measures = self.create_field_values(self.pivot_options.measures || [{field:'__count', type: 'integer', string:'Quantity'}]);
85             self.pivot = new openerp.web_graph.PivotTable(self.model, self.domain, self.fields, self.pivot_options);
86             self.pivot.update_data().then(function () {
87                 self.display_data();
88                 if (self.graph_view) {
89                     self.graph_view.register_groupby(self.pivot.rows.groupby, self.pivot.cols.groupby);
90                 }
91             });
92             openerp.web.bus.on('click', self, function (event) {
93                 if (self.dropdown) {
94                     self.$row_clicked = $(event.target).closest('tr');
95                     self.dropdown.remove();
96                     self.dropdown = null;
97                 }
98             });
99             self.put_measure_checkmarks();
100         });
101     },
102
103     get_groupby_fields: function () {
104         var search_fields = this.get_search_fields(),
105             search_field_names = _.pluck(search_fields, 'field'),
106             other_fields = [],
107             groupable_types = ['many2one', 'char', 'boolean', 'selection', 'date', 'datetime'];
108
109         _.each(this.fields, function (val, key) {
110             if (!_.contains(search_field_names, key) && 
111                 _.contains(groupable_types, val.type) && 
112                 val.store === true) {
113                 other_fields.push({
114                     field: key,
115                     string: val.string,
116                 });
117             }
118         });
119         return search_fields.concat(other_fields);
120     },
121
122     // this method gets the fields that appear in the search view, under the 
123     // 'Groupby' heading
124     get_search_fields: function () {
125         return [];
126         var self = this;
127
128         var groupbygroups = _(this.search_view.drawer.inputs).select(function (g) {
129             return g instanceof openerp.web.search.GroupbyGroup;
130         });
131
132         var filters = _.flatten(_.pluck(groupbygroups, 'filters'), true),
133             groupbys = _.flatten(_.map(filters, function (filter) {
134                 var groupby = py.eval(filter.attrs.context).group_by;
135                 if (!(groupby instanceof Array)) { groupby = [groupby]; }
136                 return _.map(groupby, function(g) {
137                     return {field: g, filter: filter};
138                 });
139             }));
140
141         return _.uniq(_.map(groupbys, function (groupby) {
142             var field = groupby.field,
143                 filter = groupby.filter,
144                 raw_field = field.split(':')[0],
145                 string = (field === raw_field) ? filter.attrs.string : self.fields[raw_field].string;
146             
147             filter = (field === raw_field) ? filter : undefined;
148
149             return { field: raw_field, string: string, filter: filter };
150         }), false, function (filter) {return filter.field;});
151     },
152
153     // Extracts the integer/float fields which are not 'id'
154     get_measures: function() {
155         return _.compact(_.map(this.fields, function (f, id) {
156             if (((f.type === 'integer') || (f.type === 'float')) && 
157                 (id !== 'id') &&
158                 (f.store !== false)) {
159                 return {field:id, type: f.type, string: f.string};
160             }
161         }));
162     },
163
164     add_measures_to_options: function() {
165         this.$measure_list.append(
166         _.map(this.measure_list, function (measure) {
167             return $('<li>').append($('<a>').attr('data-choice', measure.field)
168                                      .attr('href', '#')
169                                      .text(measure.string));
170         }));
171         this.$measure_list.find('li').click(this.measure_selection.bind(this));
172     },
173
174     // ----------------------------------------------------------------------
175     // Configuration methods
176     // ----------------------------------------------------------------------
177     set: function (domain, row_groupby, col_groupby, measures_groupby) {
178         if (!this.pivot) {
179             this.pivot_options.domain = domain;
180             this.pivot_options.row_groupby = row_groupby;
181             this.pivot_options.col_groupby = col_groupby;
182             this.pivot_options.measures_groupby = measures_groupby;
183             return;
184         }
185         var row_gbs = this.create_field_values(row_groupby),
186             col_gbs = this.create_field_values(col_groupby),
187             measures_gbs = this.create_field_values(measures_groupby),
188             dom_changed = !_.isEqual(this.pivot.domain, domain),
189             row_gb_changed = !_.isEqual(row_gbs, this.pivot.rows.groupby),
190             col_gb_changed = !_.isEqual(col_gbs, this.pivot.cols.groupby),
191             measures_gb_changed = !_.isEqual(measures_gbs, this.pivot.measures),
192             row_reduced = is_strict_beginning_of(row_gbs, this.pivot.rows.groupby),
193             col_reduced = is_strict_beginning_of(col_gbs, this.pivot.cols.groupby),
194             measures_reduced = is_strict_beginning_of(measures_gbs, this.pivot.measures);
195
196         if (!dom_changed && row_reduced && !col_gb_changed && !measures_gb_changed) {
197             this.pivot.fold_with_depth(this.pivot.rows, row_gbs.length);
198             this.display_data();
199             return;
200         }
201         if (!dom_changed && col_reduced && !row_gb_changed && !measures_gb_changed) {
202             this.pivot.fold_with_depth(this.pivot.cols, col_gbs.length);
203             this.display_data();
204             return;
205         }
206
207         if (!dom_changed && col_reduced && row_reduced && !measures_gb_changed) {
208             this.pivot.fold_with_depth(this.pivot.rows, row_gbs.length);
209             this.pivot.fold_with_depth(this.pivot.cols, col_gbs.length);
210             this.display_data();
211             return;
212         }
213
214         if (dom_changed || row_gb_changed || col_gb_changed || measures_gb_changed) {
215             this.pivot.set(domain, row_gbs, col_gbs, measures_gbs).then(this.proxy('display_data'));
216         }
217
218         if (measures_gb_changed) {
219             this.put_measure_checkmarks();
220         }
221     },
222
223     set_mode: function (mode) {
224         this.mode = mode;
225
226         if (mode === 'pivot') {
227             this.$('.graph_heatmap label').removeClass('disabled');
228             this.$('.graph_main_content').removeClass('graph_chart_mode').addClass('graph_pivot_mode');
229         } else {
230             this.$('.graph_heatmap label').addClass('disabled');
231             this.$('.graph_main_content').removeClass('graph_pivot_mode').addClass('graph_chart_mode');
232         }
233         this.display_data();
234     },
235
236     create_field_value: function (f) {
237         var field = (_.contains(f, ':')) ? f.split(':')[0] : f,
238             groupby_field = _.findWhere(this.groupby_fields, {field:field}),
239             string = groupby_field ? groupby_field.string : this.fields[field].string,
240             result =  {field: f, string: string, type: this.fields[field].type };
241
242         if (groupby_field) {
243             result.filter = groupby_field.filter;
244         }
245
246         return result;
247     },
248
249     create_field_values: function (field_ids) {
250         return _.map(field_ids, this.proxy('create_field_value'));
251     },
252
253
254     get_col_groupbys: function () {
255         return _.pluck(this.pivot.cols.groupby, 'field');
256     },
257
258     get_current_measures: function () {
259         return _.pluck(this.pivot.measures, 'field');
260     },
261
262     // ----------------------------------------------------------------------
263     // UI code
264     // ----------------------------------------------------------------------
265     events: {
266         'click .graph_mode_selection label' : 'mode_selection',
267         'click .graph_measure_selection li' : 'measure_selection',
268         'click .graph_options_selection label' : 'option_selection',
269         'click .graph_heatmap label' : 'heatmap_mode_selection',
270         'click .web_graph_click' : 'header_cell_clicked',
271         'click a.field-selection' : 'field_selection',
272     },
273
274     mode_selection: function (event) {
275         event.preventDefault();
276         var mode = event.currentTarget.getAttribute('data-mode');
277         this.set_mode(mode);
278     },
279
280     measure_selection: function (event) {
281         event.preventDefault();
282         event.stopPropagation();
283         var measure_field = event.target.getAttribute('data-choice');
284         var measure = {
285             field: measure_field,
286             type: this.fields[measure_field].type,
287             string: this.fields[measure_field].string
288         };
289
290         this.pivot.toggle_measure(measure).then(this.proxy('display_data'));
291         this.put_measure_checkmarks();
292     },
293
294     put_measure_checkmarks: function () {
295         var self = this,
296             measures_li = this.$measure_list.find('li');
297         measures_li.removeClass('selected');
298         _.each(this.measure_list, function (measure, index) {
299             if (_.findWhere(self.pivot.measures, measure)) {
300                 measures_li.eq(index).addClass('selected');
301             }
302         });
303
304     },
305
306     option_selection: function (event) {
307         event.preventDefault();
308         switch (event.currentTarget.getAttribute('data-choice')) {
309             case 'swap_axis':
310                 this.swap_axis();
311                 break;
312             case 'expand_all':
313                 this.pivot.expand_all().then(this.proxy('display_data'));
314                 break;
315             case 'update_values':
316                 this.pivot.update_data().then(this.proxy('display_data'));
317                 break;
318             case 'export_data':
319                 this.export_xls();
320                 break;
321         }
322     },
323
324     header_cell_clicked: function (event) {
325         event.preventDefault();
326         event.stopPropagation();
327         var id = event.target.getAttribute('data-id'),
328             header = this.pivot.get_header(id),
329             self = this;
330
331         if (header.expanded) {
332             if (header.root === this.pivot.rows) {
333                 this.fold_row(header, event);
334             } else {
335                 this.fold_col(header);
336             }
337             return;
338         }
339         if (header.path.length < header.root.groupby.length) {
340             this.$row_clicked = $(event.target).closest('tr');
341             this.expand(id);
342             return;
343         }
344         if (!this.groupby_fields.length) {
345             return;
346         }
347
348         var fields = _.map(this.groupby_fields, function (field) {
349                 return {id: field.field, value: field.string, type:self.fields[field.field.split(':')[0]].type};
350         });
351         if (this.dropdown) {
352             this.dropdown.remove();
353         }
354         this.dropdown = $(QWeb.render('field_selection', {fields:fields, header_id:id}));
355         $(event.target).after(this.dropdown);
356         this.dropdown.css({
357             position:'absolute',
358             left:event.originalEvent.layerX,
359         });
360         this.$('.field-selection').next('.dropdown-menu').first().toggle();        
361     },
362
363     field_selection: function (event) {
364         var id = event.target.getAttribute('data-id'),
365             field_id = event.target.getAttribute('data-field-id'),
366             interval,
367             groupby = this.create_field_value(field_id);
368         event.preventDefault();
369         if (this.fields[field_id].type === 'date' || this.fields[field_id].type === 'datetime') {
370             interval = event.target.getAttribute('data-interval');
371             groupby.field =  groupby.field + ':' + interval;
372         }
373         this.expand(id, groupby);
374     },
375
376     // ----------------------------------------------------------------------
377     // Pivot Table integration
378     // ----------------------------------------------------------------------
379     expand: function (header_id, groupby) {
380         var self = this,
381             header = this.pivot.get_header(header_id),
382             update_groupby = !!groupby;
383         
384         groupby = groupby || header.root.groupby[header.path.length];
385
386         this.pivot.expand(header_id, groupby).then(function () {
387             if (header.root === self.pivot.rows) {
388                 // expanding rows can be done by only inserting in the dom
389                 // console.log(event.target);
390                 var rows = self.build_rows(header.children);
391                 var doc_fragment = $(document.createDocumentFragment());
392                 rows.map(function (row) {
393                     doc_fragment.append(self.draw_row(row, 0));
394                 });
395                 self.$row_clicked.after(doc_fragment);
396             } else {
397                 // expanding cols will redraw the full table
398                 self.display_data();                
399             }
400             if (update_groupby && self.graph_view) {
401                 self.graph_view.register_groupby(self.pivot.rows.groupby, self.pivot.cols.groupby);
402             }
403         });
404
405     },
406
407     fold_row: function (header, event) {
408         var rows_before = this.pivot.rows.headers.length,
409             update_groupby = this.pivot.fold(header),
410             rows_after = this.pivot.rows.headers.length,
411             rows_removed = rows_before - rows_after;
412
413         if (rows_after === 1) {
414             // probably faster to redraw the unique row instead of removing everything
415             this.display_data();
416         } else {
417             var $row = $(event.target).parent().parent();
418             $row.nextAll().slice(0,rows_removed).remove();
419         }
420         if (update_groupby && this.graph_view) {
421             this.graph_view.register_groupby(this.pivot.rows.groupby, this.pivot.cols.groupby);
422         }
423     },
424
425     fold_col: function (header) {
426         var update_groupby = this.pivot.fold(header);
427         
428         this.display_data();
429         if (update_groupby && this.graph_view) {
430             this.graph_view.register_groupby(this.pivot.rows.groupby, this.pivot.cols.groupby);
431         }
432     },
433
434     swap_axis: function () {
435         this.pivot.swap_axis();
436         this.display_data();
437         this.graph_view.register_groupby(this.pivot.rows.groupby, this.pivot.cols.groupby);
438     },
439
440     // ----------------------------------------------------------------------
441     // Convert Pivot data structure into table structure :
442     //      compute rows, cols, colors, cell width, cell height, ...
443     // ----------------------------------------------------------------------
444     build_table: function(raw) {
445         return {
446             headers: this.build_headers(),
447             measure_row: this.build_measure_row(),
448             rows: this.build_rows(this.pivot.rows.headers,raw),
449             nbr_measures: this.pivot.measures.length,
450             title: this.title,
451         };
452     },
453
454     build_headers: function () {
455         var pivot = this.pivot,
456             nbr_measures = pivot.measures.length,
457             height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})) + 1,
458             rows = [];
459
460         _.each(pivot.cols.headers, function (col) {
461             var cell_width = nbr_measures * (col.expanded ? pivot.get_ancestor_leaves(col).length : 1),
462                 cell_height = col.expanded ? 1 : height - col.path.length,
463                 cell = {width: cell_width, height: cell_height, title: col.title, id: col.id, expanded: col.expanded};
464             if (rows[col.path.length]) {
465                 rows[col.path.length].push(cell);
466             } else {
467                 rows[col.path.length] = [cell];
468             }
469         });
470
471         if (pivot.get_cols_leaves().length > 1) {
472             rows[0].push({width: nbr_measures, height: height, title: ' ', id: pivot.main_col().id });
473         }
474         if (pivot.cols.headers.length === 1) {
475             rows = [[{width: nbr_measures, height: 1, title: _t('Total'), id: pivot.main_col().id, expanded: false}]];
476         }
477         return rows;
478     },
479
480     build_measure_row: function () {
481         var nbr_leaves = this.pivot.get_cols_leaves().length,
482             nbr_cols = nbr_leaves + ((nbr_leaves > 1) ? 1 : 0),
483             result = [],
484             add_total = this.pivot.get_cols_leaves().length > 1,
485             i, m;
486         for (i = 0; i < nbr_cols; i++) {
487             for (m = 0; m < this.pivot.measures.length; m++) {
488                 result.push({
489                     text:this.pivot.measures[m].string,
490                     is_bold: add_total && (i === nbr_cols - 1)
491                 });
492             }
493         }
494         return result;
495     },
496
497     make_cell: function (row, col, value, index, raw) {
498         var formatted_value = raw && !_.isUndefined(value) ? value : openerp.web.format_value(value, {type:this.pivot.measures[index].type}),
499             cell = {value:formatted_value};
500
501         return cell;
502     },
503
504     build_rows: function (headers, raw) {
505         var self = this,
506             pivot = this.pivot,
507             m, i, j, k, cell, row;
508
509         var rows = [];
510         var cells, pivot_cells, values;
511
512         var nbr_of_rows = headers.length;
513         var col_headers = pivot.get_cols_leaves();
514
515         for (i = 0; i < nbr_of_rows; i++) {
516             row = headers[i];
517             cells = [];
518             pivot_cells = [];
519             for (j = 0; j < pivot.cells.length; j++) {
520                 if (pivot.cells[j].x == row.id || pivot.cells[j].y == row.id) {
521                     pivot_cells.push(pivot.cells[j]);
522                 }              
523             }
524
525             for (j = 0; j < col_headers.length; j++) {
526                 values = undefined;
527                 for (k = 0; k < pivot_cells.length; k++) {
528                     if (pivot_cells[k].x == col_headers[j].id || pivot_cells[k].y == col_headers[j].id) {
529                         values = pivot_cells[k].values;
530                         break;
531                     }               
532                 }
533                 if (!values) { values = new Array(pivot.measures.length);}
534                 for (m = 0; m < pivot.measures.length; m++) {
535                     cells.push(self.make_cell(row,col_headers[j],values[m], m, raw));
536                 }
537             }
538             if (col_headers.length > 1) {
539                 var totals = pivot.get_total(row);
540                 for (m = 0; m < pivot.measures.length; m++) {
541                     cell = self.make_cell(row, pivot.cols.headers[0], totals[m], m, raw);
542                     cell.is_bold = 'true';
543                     cells.push(cell);
544                 }
545             }
546             rows.push({
547                 id: row.id,
548                 indent: row.path.length,
549                 title: row.title,
550                 expanded: row.expanded,
551                 cells: cells,
552             });
553         }
554
555         return rows;
556     },
557
558     // ----------------------------------------------------------------------
559     // Main display method
560     // ----------------------------------------------------------------------
561     display_data: function () {
562         var scroll = $(window).scrollTop();
563         this.$('.graph_main_content svg').remove();
564         this.$('.graph_main_content div').remove();
565         this.table.empty();
566         this.$('.graph_options_selection label').last().toggleClass('disabled', this.pivot.no_data);
567         this.width = this.$el.width();
568         this.height = Math.min(Math.max(document.documentElement.clientHeight - 116 - 60, 250), Math.round(0.8*this.$el.width()));
569
570         this.$('.graph_header').toggle(this.visible_ui);
571         if (this.pivot.no_data) {
572             this.$('.graph_main_content').append($(QWeb.render('graph_no_data')));
573         } else {
574             if (this.mode === 'pivot') {
575                 this.draw_table();
576                 $(window).scrollTop(scroll);
577             } else {
578                 this.$('.graph_main_content').append($('<div><svg>'));
579                 this.svg = this.$('.graph_main_content svg')[0];
580                 this[this.mode]();
581             }
582         }
583     },
584
585     // ----------------------------------------------------------------------
586     // Drawing the table
587     // ----------------------------------------------------------------------
588     draw_table: function () {
589         var custom_gbs = this.graph_view.get_custom_filter_groupbys(),
590             frozen_rows = custom_gbs.groupby.length,
591             frozen_cols = custom_gbs.col_groupby.length;
592
593         var table = this.build_table();
594         var doc_fragment = $(document.createDocumentFragment());
595         this.draw_headers(table.headers, doc_fragment, frozen_cols);
596         this.draw_measure_row(table.measure_row, doc_fragment);
597         this.draw_rows(table.rows, doc_fragment, frozen_rows);
598         this.table.append(doc_fragment);
599     },
600
601     make_header_cell: function (header, frozen) {
602         var cell = (_.has(header, 'cells') ? $('<td>') : $('<th>'))
603                         .addClass('graph_border')
604                         .attr('rowspan', header.height)
605                         .attr('colspan', header.width);
606         var $content = $('<span>').attr('href','#')
607                                  .text(' ' + (header.title || _t('Undefined')))
608                                  .css('margin-left', header.indent*30 + 'px')
609                                  .attr('data-id', header.id);
610         if (_.has(header, 'expanded')) {
611             if (('indent' in header) && header.indent >= frozen) {
612                 $content.addClass(header.expanded ? 'fa fa-minus-square' : 'fa fa-plus-square');
613                 $content.addClass('web_graph_click');
614             }
615             if (!('indent' in header) && header.lvl >= frozen) {
616                 $content.addClass(header.expanded ? 'fa fa-minus-square' : 'fa fa-plus-square');
617                 $content.addClass('web_graph_click');
618             }
619         } else {
620             $content.css('font-weight', 'bold');
621         }
622         return cell.append($content);
623     },
624
625     draw_headers: function (headers, doc_fragment, frozen_cols) {
626         var make_cell = this.make_header_cell,
627             $empty_cell = $('<th>').attr('rowspan', headers.length),
628             $thead = $('<thead>');
629
630         _.each(headers, function (row, lvl) {
631             var $row = $('<tr>');
632             _.each(row, function (header) {
633                 header.lvl = lvl;
634                 $row.append(make_cell(header, frozen_cols));
635             });
636             $thead.append($row);
637         });
638         $thead.children(':first').prepend($empty_cell);
639         doc_fragment.append($thead);
640         this.$thead = $thead;
641     },
642     
643     draw_measure_row: function (measure_row) {
644         var $row = $('<tr>').append('<th>');
645         _.each(measure_row, function (cell) {
646             var $cell = $('<th>').addClass('measure_row').text(cell.text);
647             if (cell.is_bold) {$cell.css('font-weight', 'bold');}
648             $row.append($cell);
649         });
650         this.$thead.append($row);
651     },
652     
653     draw_row: function (row, frozen_rows) {
654         var $row = $('<tr>')
655             .attr('data-indent', row.indent)
656             .append(this.make_header_cell(row, frozen_rows));
657         
658         var cells_length = row.cells.length;
659         var cells_list = [];
660         var cell, hcell;
661
662         for (var j = 0; j < cells_length; j++) {
663             cell = row.cells[j];
664             hcell = '<td';
665             if (cell.is_bold || cell.color) {
666                 hcell += ' style="';
667                 if (cell.is_bold) hcell += 'font-weight: bold;';
668                 if (cell.color) hcell += 'background-color:' + $.Color(255, cell.color, cell.color) + ';';
669                 hcell += '"';
670             }
671             hcell += '>' + cell.value + '</td>';
672             cells_list[j] = hcell;
673         }
674         return $row.append(cells_list.join(''));
675     },
676
677     draw_rows: function (rows, doc_fragment, frozen_rows) {
678         var rows_length = rows.length,
679             $tbody = $('<tbody>');
680
681         doc_fragment.append($tbody);
682         for (var i = 0; i < rows_length; i++) {
683             $tbody.append(this.draw_row(rows[i], frozen_rows));
684         }
685     },
686
687     // ----------------------------------------------------------------------
688     // Drawing charts code
689     // ----------------------------------------------------------------------
690     bar: function () {
691         var self = this,
692             dim_x = this.pivot.rows.groupby.length,
693             dim_y = this.pivot.cols.groupby.length,
694             show_controls = (this.width > 400 && this.height > 300 && dim_x + dim_y >=2),
695             data;
696
697         // No groupby 
698         if ((dim_x === 0) && (dim_y === 0)) {
699             data = [{key: _t('Total'), values:[{
700                 x: _t('Total'),
701                 y: this.pivot.get_total()[0],
702             }]}];
703         // Only column groupbys 
704         } else if ((dim_x === 0) && (dim_y >= 1)){
705             data =  _.map(this.pivot.get_cols_with_depth(1), function (header) {
706                 return {
707                     key: header.title,
708                     values: [{x:header.title, y: self.pivot.get_total(header)[0]}]
709                 };
710             });
711         // Just 1 row groupby 
712         } else if ((dim_x === 1) && (dim_y === 0))  {
713             data = _.map(this.pivot.main_row().children, function (pt) {
714                 var value = self.pivot.get_total(pt)[0],
715                     title = (pt.title !== undefined) ? pt.title : _t('Undefined');
716                 return {x: title, y: value};
717             });
718             data = [{key: self.pivot.measures[0].string, values:data}];
719         // 1 row groupby and some col groupbys
720         } else if ((dim_x === 1) && (dim_y >= 1))  {
721             data = _.map(this.pivot.get_cols_with_depth(1), function (colhdr) {
722                 var values = _.map(self.pivot.get_rows_with_depth(1), function (header) {
723                     return {
724                         x: header.title || _t('Undefined'),
725                         y: self.pivot.get_values(header.id, colhdr.id)[0] || 0
726                     };
727                 });
728                 return {key: colhdr.title || _t('Undefined'), values: values};
729             });
730         // At least two row groupby
731         } else {
732             var keys = _.uniq(_.map(this.pivot.get_rows_with_depth(2), function (hdr) {
733                 return hdr.title || _t('Undefined');
734             }));
735             data = _.map(keys, function (key) {
736                 var values = _.map(self.pivot.get_rows_with_depth(1), function (hdr) {
737                     var subhdr = _.find(hdr.children, function (child) {
738                         return ((child.title === key) || ((child.title === undefined) && (key === _t('Undefined'))));
739                     });
740                     return {
741                         x: hdr.title || _t('Undefined'),
742                         y: (subhdr) ? self.pivot.get_total(subhdr)[0] : 0
743                     };
744                 });
745                 return {key:key, values: values};
746             });
747         }
748
749         nv.addGraph(function () {
750           var chart = nv.models.multiBarChart()
751                 .reduceXTicks(false)
752                 .stacked(self.bar_ui === 'stack')
753                 .showControls(show_controls);
754
755             if (self.width / data[0].values.length < 80) {
756                 chart.rotateLabels(-15);
757                 chart.reduceXTicks(true);
758                 chart.margin({bottom:40});
759             }
760
761             d3.select(self.svg)
762                 .datum(data)
763                 .attr('width', self.width)
764                 .attr('height', self.height)
765                 .call(chart);
766
767             nv.utils.windowResize(chart.update);
768             return chart;
769         });
770
771     },
772
773     line: function () {
774         var self = this,
775             dim_x = this.pivot.rows.groupby.length,
776             dim_y = this.pivot.cols.groupby.length;
777
778         var rows = this.pivot.get_rows_with_depth(dim_x),
779             labels = _.pluck(rows, 'title');
780
781         var data = _.map(this.pivot.get_cols_leaves(), function (col) {
782             var values = _.map(rows, function (row, index) {
783                 return {x: index, y: self.pivot.get_values(row.id,col.id)[0] || 0};
784             });
785             var title = _.map(col.path, function (p) {
786                 return p || _t('Undefined');
787             }).join('/');
788             if (dim_y === 0) {
789                 title = self.pivot.measures[0].string;
790             }
791             return {values: values, key: title};
792         });
793
794         nv.addGraph(function () {
795             var chart = nv.models.lineChart()
796                 .x(function (d,u) { return u; });
797
798             chart.xAxis.tickFormat(function (d,u) {return labels[d];});
799
800             d3.select(self.svg)
801                 .attr('width', self.width)
802                 .attr('height', self.height)
803                 .datum(data)
804                 .call(chart);
805
806             return chart;
807           });
808     },
809
810     pie: function () {
811         var self = this,
812             dim_x = this.pivot.rows.groupby.length;
813         var data = _.map(this.pivot.get_rows_leaves(), function (row) {
814             var title = _.map(row.path, function (p) {
815                 return p || _t('Undefined');
816             }).join('/');
817             if (dim_x === 0) {
818                 title = self.measure_label;
819             }
820             return {x: title, y: self.pivot.get_total(row)[0]};
821         });
822
823         nv.addGraph(function () {
824             var chart = nv.models.pieChart()
825                 .width(self.width)
826                 .height(self.height)
827                 .color(d3.scale.category10().range());
828
829             d3.select(self.svg)
830                 .datum(data)
831                 .transition().duration(1200)
832                 .attr('width', self.width)
833                 .attr('height', self.height)
834                 .call(chart);
835
836             nv.utils.windowResize(chart.update);
837             return chart;
838         });
839     },
840
841     // ----------------------------------------------------------------------
842     // Controller stuff...
843     // ----------------------------------------------------------------------
844     export_xls: function() {
845         var c = openerp.webclient.crashmanager;
846         openerp.web.blockUI();
847         this.session.get_file({
848             url: '/web_graph/export_xls',
849             data: {data: JSON.stringify(this.build_table(true))},
850             complete: openerp.web.unblockUI,
851             error: c.rpc_error.bind(c)
852         });
853     },
854
855 });
856
857 // Utility function: returns true if the beginning of array2 is array1 and
858 // if array1 is not array2
859 function is_strict_beginning_of (array1, array2) {
860     if (array1.length >= array2.length) { return false; }
861     var result = true;
862     for (var i = 0; i < array1.length; i++) {
863         if (!_.isEqual(array1[i], array2[i])) { return false;}
864     }
865     return result;
866 }
867 })();