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 two widgets (PivotTable and ChartView)
21 instance.web_graph.GraphView = instance.web.View.extend({
22 template: 'GraphView',
23 display_name: _lt('Graph'),
25 mode: 'pivot', // pivot => display pivot table, chart => display chart
28 'click .graph_mode_selection li' : function (event) {
29 event.preventDefault();
30 var view_mode = event.target.attributes['data-mode'].nodeValue;
31 if (view_mode === 'data') {
35 this.chart_view.set_mode(view_mode);
39 'click .graph_clear_groups' : function (event) {
40 this.pivot_table.clear_groups();
44 view_loading: function (fields_view_get) {
46 var model = new instance.web.Model(fields_view_get.model, {group_by_no_leaf: true});
49 options.col_groupby = [];
51 // get the default groupbys and measure defined in the field view
52 options.measure = null;
53 options.row_groupby = [];
54 _.each(fields_view_get.arch.children, function (field) {
55 if ('name' in field.attrs) {
56 if ('operator' in field.attrs) {
57 options.measure = field.attrs.name;
59 options.row_groupby.push(field.attrs.name);
64 // get the most important fields (of the model) by looking at the
65 // groupby filters defined in the search view
66 options.important_fields = [];
67 var load_view = instance.web.fields_view_get({
72 var important_fields_def = $.when(load_view).then(function (search_view) {
73 var groups = _.select(search_view.arch.children, function (c) {
74 return (c.tag == 'group') && (c.attrs.string != 'Display');
77 _.each(groups, function(g) {
78 _.each(g.children, function (g) {
79 if (g.attrs.context) {
80 var field_id = py.eval(g.attrs.context).group_by;
81 options.important_fields.push(field_id);
87 // get the fields descriptions from the model
88 var field_descr_def = model.call('fields_get', [])
89 .then(function (fields) { options.fields = fields; });
92 return $.when(important_fields_def, field_descr_def)
94 self.pivot_table = new PivotTable(model, options);
95 self.chart_view = new ChartView(model, options);
98 return self.pivot_table.appendTo('.graph_main_content');
101 return self.chart_view.appendTo('.graph_main_content');
105 display_data : function () {
106 if (this.mode === 'pivot') {
107 this.chart_view.hide();
108 this.pivot_table.show();
110 this.pivot_table.hide();
111 this.chart_view.show();
115 do_search: function (domain, context, group_by) {
116 this.domain = new instance.web.CompoundDomain(domain);
117 this.pivot_table.set_domain(domain);
118 this.chart_view.set_domain(domain);
122 do_show: function () {
123 this.do_push_state({});
124 return this._super();
130 * BasicDataView widget. Basic widget to manage show/hide functionality
131 * and to initialize some attributes. It is inherited by PivotTable
132 * and ChartView widget.
134 var BasicDataView = instance.web.Widget.extend({
139 // model: model to display
140 // fields: dictionary returned by field_get on model (desc of model)
141 // domain: constraints on model records
142 // row_groupby: groubys on rows (so, row headers in the pivot table)
143 // col_groupby: idem, but on col
144 // measure: quantity to display. either a field from the model, or
145 // null, in which case we use the "count" measure
146 init: function (model, options) {
148 this.fields = options.fields;
149 this.domain = options.domain;
150 this.row_groupby = options.row_groupby;
151 this.col_groupby = options.col_groupby;
152 this.measure = options.measure;
153 this.measure_label = options.measure ? options.fields[options.measure].string : 'Quantity';
155 this.need_redraw = true;
156 this.important_fields = options.important_fields;
159 get_descr: function (field_id) {
160 return this.fields[field_id].string;
163 set_domain: function (domain) {
164 this.domain = domain;
165 this.need_redraw = true;
168 set_row_groupby: function (row_groupby) {
169 this.row_groupby = row_groupby;
170 this.need_redraw = true;
173 set_col_groupby: function (col_groupby) {
174 this.col_groupby = col_groupby;
175 this.need_redraw = true;
178 set_measure: function (measure) {
179 this.measure = measure;
180 this.need_redraw = true;
184 if (this.need_redraw) {
186 this.need_redraw = false;
188 this.$el.css('display', 'block');
192 this.$el.css('display', 'none');
198 get_data: function (groupby) {
199 var view_fields = this.row_groupby.concat(this.measure, this.col_groupby);
200 return query_groups(this.model, view_fields, this.domain, groupby);
206 * PivotTable widget. It displays the data in tabular data and allows the
207 * user to drill down and up in the table
209 var PivotTable = BasicDataView.extend({
210 template: 'pivot_table',
216 'click .graph_border > a' : function (event) {
218 event.preventDefault();
219 var row_id = event.target.attributes['data-row-id'].nodeValue;
221 var row = this.get_row(row_id);
223 this.fold_row(row_id)
225 var dropdown_options = {
226 fields: _.map(this.important_fields, function (field) {
227 return {id: field, value: self.get_descr(field)};
231 this.dropdown = $(QWeb.render('field_selection', dropdown_options));
232 $(event.target).after(this.dropdown);
233 $('.field-selection').next('.dropdown-menu').toggle();
238 'click a.field-selection' : function (event) {
239 event.preventDefault();
240 this.dropdown.remove();
241 var row_id = event.target.attributes['data-row-id'].nodeValue;
242 var field_id = event.target.attributes['data-field-id'].nodeValue;
243 this.expand_row(row_id, field_id);
247 clear_groups: function () {
248 this.row_groupby = [];
249 this.col_groupby = [];
255 init: function (model, options) {
256 this._super(model, options);
259 generate_id: function () {
260 this.current_row_id += 1;
261 return this.current_row_id - 1;
264 get_row: function (id) {
265 return _.find(this.rows, function(row) {
266 return (row.id == id);
270 make_cell: function (content, options) {
272 if (options && options.is_border) {
273 attrs.push('class="graph_border"');
277 if (options && options.indent) {
278 _.each(_.range(options.indent), function () {
279 attrs.push('<span class="web_graph_indent"></span>');
282 if (options && options.foldable) {
283 attrs.push('<a data-row-id="'+ options.row_id + '" href="#" class="icon-plus-sign"> </a>');
285 return attrs.join(' ') + content + '</td>';
288 make_row: function (data, parent_id) {
289 var has_parent = (parent_id !== undefined);
290 var parent = has_parent ? this.get_row(parent_id) : null;
293 path = parent.path.concat(data.attributes.grouped_on);
294 } else if (data.attributes.grouped_on !== undefined) {
295 path = [data.attributes.grouped_on];
300 var indent_level = has_parent ? parent.path.length : 0;
301 var value = (this.row_groupby.length > 0) ? data.attributes.value[1] : 'Total';
304 var jquery_row = $('<tr></tr>');
305 var row_id = this.generate_id();
307 var header = $(this.make_cell(value, {is_border:true, indent: indent_level, foldable:true, row_id: row_id}));
308 jquery_row.html(header);
309 jquery_row.append(this.make_cell(data.attributes.aggregates[this.measure]));
319 domain: data.model._domain,
321 // rows.splice(index of parent if any,0,row);
322 this.rows.push(row); // to do, insert it properly
324 if (this.row_groupby.length === 0) {
325 row.remove_when_expanded = true;
326 row.domain = this.domain;
329 parent.children.push(row);
334 expand_row: function (row_id, field_id) {
336 var row = this.get_row(row_id);
337 this.row_groupby.push(field_id);
339 var visible_fields = this.row_groupby.concat(this.col_groupby, this.measure);
341 if (row.remove_when_expanded) {
345 row.html_tr.find('.icon-plus-sign')
346 .removeClass('icon-plus-sign')
347 .addClass('icon-minus-sign');
350 query_groups(this.model, visible_fields, row.domain, [field_id])
351 .then(function (data) {
352 _.each(data.reverse(), function (datapt) {
354 if (row.remove_when_expanded) {
355 new_row = self.make_row(datapt);
356 self.$('tr.graph_table_header').after(new_row.html_tr);
358 new_row = self.make_row(datapt, row_id);
359 row.html_tr.after(new_row.html_tr);
362 if (row.remove_when_expanded) {
363 row.html_tr.remove();
369 fold_row: function (row_id) {
374 this.get_data(this.row_groupby)
375 .then(this.proxy('build_table'))
376 .done(this.proxy('_draw'));
379 build_table: function (data) {
384 value: this.measure_label,
390 header: $(this.make_cell(this.measure_label, {is_border:true})),
393 _.each(data, function (datapt) {
394 self.make_row(datapt);
404 if (this.row_groupby.length > 0) {
405 header = '<tr><td class="graph_border">' +
406 this.fields[this.row_groupby[0]].string +
407 '</td><td class="graph_border">' +
411 header = '<tr class="graph_table_header"><td class="graph_border">' +
412 '</td><td class="graph_border">' +
416 this.$el.append(header);
418 _.each(this.rows, function (row) {
419 self.$el.append(row.html_tr);
426 * ChartView widget. It displays the data in chart form, using the nvd3
427 * library. Various modes include bar charts, pie charts or line charts.
429 var ChartView = BasicDataView.extend({
430 template: 'chart_view',
432 set_mode: function (mode) {
433 this.render = this['render_' + mode];
434 this.need_redraw = true;
440 this.$el.append('<svg></svg>');
441 this.get_data(this.row_groupby).done(function (data) {
446 format_data: function (datapt) {
447 var val = datapt.attributes;
449 x: datapt.attributes.value[1],
450 y: this.measure ? val.aggregates[this.measure] : val.length,
454 render_bar_chart: function (data) {
455 var formatted_data = [{
457 values: _.map(data, this.proxy('format_data')),
460 nv.addGraph(function () {
461 var chart = nv.models.discreteBarChart()
468 d3.select('.graph_chart svg')
469 .datum(formatted_data)
474 nv.utils.windowResize(chart.update);
479 render_line_chart: function (data) {
480 var formatted_data = [{
481 key: this.measure_label,
482 values: _.map(data, this.proxy('format_data'))
485 nv.addGraph(function () {
486 var chart = nv.models.lineChart()
487 .x(function (d,u) { return u; })
490 .margin({top: 30, right: 20, bottom: 20, left: 60});
492 d3.select('.graph_chart svg')
495 .datum(formatted_data)
502 render_pie_chart: function (data) {
503 var formatted_data = _.map(data, this.proxy('format_data'));
505 nv.addGraph(function () {
506 var chart = nv.models.pieChart()
507 .color(d3.scale.category10().range())
511 d3.select('.graph_chart svg')
512 .datum(formatted_data)
513 .transition().duration(1200)
518 nv.utils.windowResize(chart.update);
526 * Query the server and return a deferred which will return the data
527 * with all the groupbys applied (this is done for now, but the goal
528 * is to modify read_group in order to allow eager and lazy groupbys
530 function query_groups (model, fields, domain, groupbys) {
531 return model.query(fields)
534 .then(function (results) {
535 var non_empty_results = _.filter(results, function (group) {
536 return group.attributes.length > 0;
538 if (groupbys.length <= 1) {
539 return non_empty_results;
541 var get_subgroups = $.when.apply(null, _.map(non_empty_results, function (result) {
542 var new_domain = result.model._domain;
543 var new_groupings = groupbys.slice(1);
544 return query_groups(model, fields,new_domain, new_groupings).then(function (subgroups) {
545 result.subgroups_data = subgroups;
548 return get_subgroups.then(function () {
549 return non_empty_results;