2011-04-15 06:42:41 +00:00
|
|
|
/*---------------------------------------------------------
|
2011-09-05 12:28:15 +00:00
|
|
|
* OpenERP web_graph
|
2011-04-15 06:42:41 +00:00
|
|
|
*---------------------------------------------------------*/
|
2013-11-08 15:41:14 +00:00
|
|
|
|
2013-11-12 08:38:27 +00:00
|
|
|
/* jshint undef: false */
|
|
|
|
|
2012-04-17 12:48:30 +00:00
|
|
|
openerp.web_graph = function (instance) {
|
2013-11-13 10:50:59 +00:00
|
|
|
'use strict';
|
2011-06-17 13:25:50 +00:00
|
|
|
|
2012-06-07 14:17:12 +00:00
|
|
|
var _lt = instance.web._lt;
|
2013-01-25 07:02:40 +00:00
|
|
|
var _t = instance.web._t;
|
2013-11-12 13:53:44 +00:00
|
|
|
var QWeb = instance.web.qweb;
|
2012-05-07 08:19:08 +00:00
|
|
|
|
2012-04-17 12:48:30 +00:00
|
|
|
instance.web.views.add('graph', 'instance.web_graph.GraphView');
|
2013-11-08 13:08:06 +00:00
|
|
|
|
2012-04-17 12:48:30 +00:00
|
|
|
instance.web_graph.GraphView = instance.web.View.extend({
|
2011-12-16 13:00:00 +00:00
|
|
|
display_name: _lt('Graph'),
|
2013-11-08 13:08:06 +00:00
|
|
|
view_type: 'graph',
|
2013-11-12 11:31:34 +00:00
|
|
|
|
2013-11-27 13:04:06 +00:00
|
|
|
init: function(parent, dataset, view_id, options) {
|
2013-12-26 16:14:57 +00:00
|
|
|
this._super(parent, dataset, view_id, options);
|
2013-11-27 13:04:06 +00:00
|
|
|
this.dataset = dataset;
|
2013-12-18 13:40:40 +00:00
|
|
|
this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});
|
2013-12-05 10:07:10 +00:00
|
|
|
this.search_view = parent.searchview;
|
2013-12-19 12:54:22 +00:00
|
|
|
this.search_view_groupby = [];
|
2013-12-05 15:19:45 +00:00
|
|
|
this.groupby_mode = 'default'; // 'default' or 'manual'
|
|
|
|
this.default_row_groupby = [];
|
2013-12-10 11:16:46 +00:00
|
|
|
this.default_col_groupby = [];
|
2013-12-06 15:43:19 +00:00
|
|
|
this.search_field = {
|
2013-12-09 13:08:58 +00:00
|
|
|
get_context: this.proxy('get_context'),
|
|
|
|
get_domain: function () {},
|
2013-12-06 15:43:19 +00:00
|
|
|
get_groupby: function () { },
|
|
|
|
};
|
2013-11-28 08:27:54 +00:00
|
|
|
},
|
|
|
|
|
2013-12-09 13:08:58 +00:00
|
|
|
get_context: function (facet) {
|
|
|
|
var col_group_by = _.map(facet.values.models, function (model) {
|
|
|
|
return model.attributes.value.attrs.context.col_group_by;
|
|
|
|
});
|
|
|
|
return {col_group_by : col_group_by};
|
|
|
|
},
|
|
|
|
|
2013-11-28 08:27:54 +00:00
|
|
|
start: function () {
|
2013-12-18 13:40:40 +00:00
|
|
|
var options = {enabled:false};
|
|
|
|
this.graph_widget = new openerp.web_graph.Graph(this, this.model, options);
|
|
|
|
this.graph_widget.appendTo(this.$el);
|
|
|
|
this.graph_widget.pivot.on('groupby_changed', this, this.proxy('register_groupby'));
|
2013-11-28 08:27:54 +00:00
|
|
|
return this.load_view();
|
2013-11-27 13:04:06 +00:00
|
|
|
},
|
|
|
|
|
2013-11-08 13:08:06 +00:00
|
|
|
view_loading: function (fields_view_get) {
|
2013-11-22 07:43:27 +00:00
|
|
|
var self = this,
|
2013-12-20 11:02:43 +00:00
|
|
|
arch = fields_view_get.arch,
|
|
|
|
measure = null,
|
|
|
|
stacked = false;
|
2013-11-22 07:43:27 +00:00
|
|
|
|
2013-12-20 11:02:43 +00:00
|
|
|
if (arch.attrs.type === 'bar' || !_.has(arch.attrs, 'type')) {
|
2013-12-19 13:10:58 +00:00
|
|
|
this.graph_widget.mode = 'bar_chart';
|
2013-11-28 14:51:55 +00:00
|
|
|
}
|
2013-12-24 11:22:24 +00:00
|
|
|
if (arch.attrs.stacked === 'True') {
|
2013-12-20 11:02:43 +00:00
|
|
|
stacked = true;
|
|
|
|
}
|
2013-11-28 14:51:55 +00:00
|
|
|
|
2013-12-20 11:02:43 +00:00
|
|
|
_.each(arch.children, function (field) {
|
|
|
|
if (_.has(field.attrs, 'type')) {
|
|
|
|
switch (field.attrs.type) {
|
2013-12-24 11:22:24 +00:00
|
|
|
case 'row':
|
2013-12-20 11:02:43 +00:00
|
|
|
self.default_row_groupby.push(field.attrs.name);
|
|
|
|
break;
|
2013-12-24 11:22:24 +00:00
|
|
|
case 'col':
|
2013-12-20 11:02:43 +00:00
|
|
|
self.default_col_groupby.push(field.attrs.name);
|
|
|
|
break;
|
2013-12-24 11:22:24 +00:00
|
|
|
case 'measure':
|
2013-12-20 11:02:43 +00:00
|
|
|
measure = field.attrs.name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else { // old style, kept for backward compatibility
|
2013-11-08 15:20:48 +00:00
|
|
|
if ('operator' in field.attrs) {
|
2013-12-24 10:09:42 +00:00
|
|
|
measure = (measure) ? measure : field.attrs.name;
|
2013-11-08 15:20:48 +00:00
|
|
|
} else {
|
2013-12-20 11:02:43 +00:00
|
|
|
self.default_row_groupby.push(field.attrs.name);
|
2013-11-08 15:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-12-20 11:02:43 +00:00
|
|
|
this.graph_widget.config({
|
2013-12-24 11:22:24 +00:00
|
|
|
measure:measure,
|
2013-12-20 11:02:43 +00:00
|
|
|
update:false,
|
|
|
|
bar_ui: (stacked) ? 'stack' : 'group'
|
|
|
|
});
|
2013-12-18 13:40:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
do_search: function (domain, context, group_by) {
|
2013-12-24 11:22:24 +00:00
|
|
|
var col_groupby = context.col_group_by || [],
|
2013-12-18 13:40:40 +00:00
|
|
|
options = {domain:domain};
|
|
|
|
|
2013-12-19 12:54:22 +00:00
|
|
|
this.search_view_groupby = group_by;
|
2013-12-24 10:09:42 +00:00
|
|
|
|
|
|
|
if (group_by.length && this.groupby_mode !== 'manual') {
|
|
|
|
if (_.isEqual(col_groupby, [])) {
|
|
|
|
col_groupby = this.default_col_groupby;
|
2013-12-24 11:22:24 +00:00
|
|
|
}
|
2013-12-24 10:09:42 +00:00
|
|
|
}
|
2013-12-18 13:40:40 +00:00
|
|
|
if (group_by.length || col_groupby.length) {
|
|
|
|
this.groupby_mode = 'manual';
|
|
|
|
}
|
2013-12-24 11:22:24 +00:00
|
|
|
if (!this.graph_widget.enabled) {
|
2013-12-18 13:40:40 +00:00
|
|
|
options.update = false;
|
|
|
|
options.silent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.groupby_mode === 'manual') {
|
|
|
|
options.row_groupby = group_by;
|
|
|
|
options.col_groupby = col_groupby;
|
|
|
|
} else {
|
|
|
|
options.row_groupby = _.toArray(this.default_row_groupby);
|
|
|
|
options.col_groupby = _.toArray(this.default_col_groupby);
|
|
|
|
}
|
|
|
|
this.graph_widget.pivot.config(options);
|
|
|
|
|
|
|
|
if (!this.graph_widget.enabled) {
|
|
|
|
this.graph_widget.activate_display();
|
2013-12-26 12:34:49 +00:00
|
|
|
this.ViewManager.on('switch_mode', this, function () {this.graph_widget.pivot.update_data(); });
|
2013-12-18 13:40:40 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
do_show: function () {
|
|
|
|
this.do_push_state({});
|
|
|
|
return this._super();
|
|
|
|
},
|
|
|
|
|
|
|
|
register_groupby: function() {
|
|
|
|
var self = this,
|
|
|
|
query = this.search_view.query;
|
|
|
|
|
2013-12-19 12:54:22 +00:00
|
|
|
this.groupby_mode = 'manual';
|
2013-12-20 12:39:25 +00:00
|
|
|
if (_.isEqual(this.search_view_groupby, this.graph_widget.pivot.rows.groupby) ||
|
|
|
|
(!_.has(this.search_view, '_s_groupby'))) {
|
2013-12-19 12:54:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-12-18 13:40:40 +00:00
|
|
|
var rows = _.map(this.graph_widget.pivot.rows.groupby, function (group) {
|
|
|
|
return make_facet('GroupBy', group);
|
|
|
|
});
|
|
|
|
var cols = _.map(this.graph_widget.pivot.cols.groupby, function (group) {
|
|
|
|
return make_facet('ColGroupBy', group);
|
|
|
|
});
|
|
|
|
|
|
|
|
query.reset(rows.concat(cols));
|
|
|
|
|
|
|
|
function make_facet (category, fields) {
|
|
|
|
var values,
|
|
|
|
icon,
|
|
|
|
backbone_field,
|
|
|
|
cat_name;
|
|
|
|
if (!(fields instanceof Array)) { fields = [fields]; }
|
|
|
|
if (category === 'GroupBy') {
|
|
|
|
cat_name = 'group_by';
|
|
|
|
icon = 'w';
|
|
|
|
backbone_field = self.search_view._s_groupby;
|
|
|
|
} else {
|
|
|
|
cat_name = 'col_group_by';
|
|
|
|
icon = 'f';
|
|
|
|
backbone_field = self.search_field;
|
|
|
|
}
|
|
|
|
values = _.map(fields, function (field) {
|
|
|
|
var context = {};
|
|
|
|
context[cat_name] = field;
|
|
|
|
return {label: self.graph_widget.fields[field].string, value: {attrs:{domain: [], context: context}}};
|
|
|
|
});
|
|
|
|
return {category:category, values: values, icon:icon, field: backbone_field};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
instance.web_graph.Graph = instance.web.Widget.extend({
|
2013-12-24 11:22:24 +00:00
|
|
|
template: 'GraphWidget',
|
2013-12-18 13:40:40 +00:00
|
|
|
|
|
|
|
events: {
|
|
|
|
'click .graph_mode_selection li' : 'mode_selection',
|
|
|
|
'click .graph_measure_selection li' : 'measure_selection',
|
|
|
|
'click .graph_options_selection li' : 'option_selection',
|
|
|
|
'click .web_graph_click' : 'header_cell_clicked',
|
|
|
|
'click a.field-selection' : 'field_selection',
|
|
|
|
},
|
|
|
|
|
|
|
|
init: function(parent, model, options) {
|
|
|
|
this._super(parent);
|
|
|
|
this.model = model;
|
|
|
|
this.important_fields = [];
|
|
|
|
this.measure_list = [];
|
|
|
|
this.fields = [];
|
|
|
|
this.pivot = new openerp.web_graph.PivotTable(model, []);
|
2013-12-18 15:07:56 +00:00
|
|
|
this.mode = 'pivot';
|
2013-12-18 13:40:40 +00:00
|
|
|
if (_.has(options, 'mode')) { this.mode = mode; }
|
2013-12-18 15:07:56 +00:00
|
|
|
this.enabled = true;
|
2013-12-18 13:40:40 +00:00
|
|
|
if (_.has(options, 'enabled')) { this.enabled = options.enabled; }
|
2013-12-18 15:07:56 +00:00
|
|
|
this.visible_ui = true;
|
2013-12-18 15:20:48 +00:00
|
|
|
this.bar_ui = 'group'; // group or stack
|
2013-12-18 15:07:56 +00:00
|
|
|
this.config(options || {});
|
2013-12-18 13:40:40 +00:00
|
|
|
},
|
|
|
|
|
2013-12-18 15:07:56 +00:00
|
|
|
// hide ui/show, stacked/grouped
|
|
|
|
config: function (options) {
|
|
|
|
if (_.has(options, 'visible_ui')) {
|
|
|
|
this.visible_ui = options.visible_ui;
|
|
|
|
}
|
2013-12-18 15:20:48 +00:00
|
|
|
if (_.has(options, 'bar_ui')) {
|
|
|
|
this.bar_ui = options.bar_ui;
|
|
|
|
}
|
2013-12-18 15:07:56 +00:00
|
|
|
this.pivot.config(options);
|
|
|
|
},
|
2013-11-08 15:20:48 +00:00
|
|
|
|
2013-12-18 13:40:40 +00:00
|
|
|
start: function() {
|
|
|
|
var self = this;
|
|
|
|
this.table = $('<table></table>');
|
|
|
|
this.$('.graph_main_content').append(this.table);
|
2013-11-13 15:47:31 +00:00
|
|
|
// get the most important fields (of the model) by looking at the
|
|
|
|
// groupby filters defined in the search view
|
2013-12-05 15:19:45 +00:00
|
|
|
var options = {model:this.model, view_type: 'search'},
|
2013-11-28 10:49:29 +00:00
|
|
|
deferred1 = instance.web.fields_view_get(options).then(function (search_view) {
|
|
|
|
var groups = _.select(search_view.arch.children, function (c) {
|
|
|
|
return (c.tag == 'group') && (c.attrs.string != 'Display');
|
|
|
|
});
|
|
|
|
_.each(groups, function(g) {
|
|
|
|
_.each(g.children, function (g) {
|
|
|
|
if (g.attrs.context) {
|
|
|
|
var field_id = py.eval(g.attrs.context).group_by;
|
2013-12-27 14:35:08 +00:00
|
|
|
if (field_id) {
|
|
|
|
self.important_fields.push(field_id);
|
|
|
|
}
|
2013-11-28 10:49:29 +00:00
|
|
|
}
|
|
|
|
});
|
2013-11-13 15:47:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-18 13:40:40 +00:00
|
|
|
// get the fields descriptions and measure list from the model
|
2013-12-05 15:19:45 +00:00
|
|
|
var deferred2 = this.model.call('fields_get', []).then(function (fs) {
|
|
|
|
self.fields = fs;
|
2013-12-27 10:22:40 +00:00
|
|
|
self.pivot.config({fields:fs});
|
2013-12-11 13:11:48 +00:00
|
|
|
var temp = _.map(fs, function (field, name) {
|
|
|
|
return {name:name, type: field.type};
|
|
|
|
});
|
|
|
|
temp = _.filter(temp, function (field) {
|
|
|
|
return (((field.type === 'integer') || (field.type === 'float')) && (field.name !== 'id'));
|
|
|
|
});
|
|
|
|
self.measure_list = _.map(temp, function (field) {
|
|
|
|
return field.name;
|
|
|
|
});
|
|
|
|
|
2013-11-28 10:54:02 +00:00
|
|
|
var measure_selection = self.$('.graph_measure_selection');
|
|
|
|
_.each(self.measure_list, function (measure) {
|
|
|
|
var choice = $('<a></a>').attr('data-choice', measure)
|
|
|
|
.attr('href', '#')
|
|
|
|
.append(self.fields[measure].string);
|
|
|
|
measure_selection.append($('<li></li>').append(choice));
|
2013-11-28 08:27:54 +00:00
|
|
|
});
|
2013-11-28 10:54:02 +00:00
|
|
|
});
|
|
|
|
|
2013-12-18 13:40:40 +00:00
|
|
|
return $.when(deferred1, deferred2).then(function () {
|
|
|
|
if (this.enabled) {
|
|
|
|
this.activate_display();
|
|
|
|
}
|
|
|
|
});
|
2012-05-07 08:19:08 +00:00
|
|
|
},
|
2013-11-08 15:20:48 +00:00
|
|
|
|
2013-12-18 13:40:40 +00:00
|
|
|
activate_display: function () {
|
|
|
|
this.pivot.on('redraw_required', this, this.proxy('display_data'));
|
|
|
|
this.pivot.update_data();
|
|
|
|
this.enabled = true;
|
2013-12-24 11:22:24 +00:00
|
|
|
instance.web.bus.on('click', this, function () {
|
2013-12-18 13:40:40 +00:00
|
|
|
if (this.dropdown) {
|
|
|
|
this.dropdown.remove();
|
|
|
|
this.dropdown = null;
|
2013-11-27 14:07:01 +00:00
|
|
|
}
|
2013-12-18 13:40:40 +00:00
|
|
|
});
|
2013-11-22 16:04:49 +00:00
|
|
|
},
|
2013-11-22 13:59:27 +00:00
|
|
|
|
2013-12-18 13:40:40 +00:00
|
|
|
display_data: function () {
|
|
|
|
this.$('.graph_main_content svg').remove();
|
2013-12-24 15:37:35 +00:00
|
|
|
this.$('.graph_main_content div').remove();
|
2013-12-18 13:40:40 +00:00
|
|
|
this.table.empty();
|
2013-11-22 13:59:27 +00:00
|
|
|
|
2013-12-18 15:07:56 +00:00
|
|
|
if (this.visible_ui) {
|
|
|
|
this.$('.graph_header').css('display', 'block');
|
|
|
|
} else {
|
|
|
|
this.$('.graph_header').css('display', 'none');
|
|
|
|
}
|
2013-12-20 11:02:43 +00:00
|
|
|
if (this.pivot.no_data) {
|
2013-12-24 15:37:35 +00:00
|
|
|
this.$('.graph_main_content').append($(QWeb.render('graph_no_data')));
|
2013-11-22 13:59:27 +00:00
|
|
|
} else {
|
2013-12-18 13:40:40 +00:00
|
|
|
var table_modes = ['pivot', 'heatmap', 'row_heatmap', 'col_heatmap'];
|
|
|
|
if (_.contains(table_modes, this.mode)) {
|
|
|
|
this.draw_table();
|
2013-11-22 13:59:27 +00:00
|
|
|
} else {
|
2013-12-18 13:40:40 +00:00
|
|
|
this.$('.graph_main_content').append($('<div><svg></svg></div>'));
|
2013-12-18 15:07:56 +00:00
|
|
|
this.svg = this.$('.graph_main_content svg')[0];
|
|
|
|
this.width = this.$el.width();
|
|
|
|
this.height = Math.min(Math.max(document.documentElement.clientHeight - 116 - 60, 250), Math.round(0.8*this.$el.width()));
|
|
|
|
this[this.mode]();
|
2013-11-22 13:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-12-05 10:07:10 +00:00
|
|
|
mode_selection: function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var mode = event.target.attributes['data-mode'].nodeValue;
|
|
|
|
this.mode = mode;
|
|
|
|
this.display_data();
|
|
|
|
},
|
|
|
|
|
|
|
|
measure_selection: function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var measure = event.target.attributes['data-choice'].nodeValue;
|
2013-12-18 15:07:56 +00:00
|
|
|
var actual_measure = (measure === '__count') ? null : measure;
|
2013-12-18 13:40:40 +00:00
|
|
|
this.pivot.config({measure:actual_measure});
|
2013-12-05 10:07:10 +00:00
|
|
|
},
|
|
|
|
|
2013-12-12 10:55:33 +00:00
|
|
|
option_selection: function (event) {
|
2013-12-05 10:07:10 +00:00
|
|
|
event.preventDefault();
|
|
|
|
switch (event.target.attributes['data-choice'].nodeValue) {
|
2013-12-27 14:55:01 +00:00
|
|
|
case 'bar_grouped':
|
|
|
|
this.bar_ui = 'group';
|
|
|
|
if (this.mode === 'bar_chart') {
|
|
|
|
this.display_data();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'bar_stacked':
|
|
|
|
this.bar_ui = 'stack';
|
|
|
|
if (this.mode === 'bar_chart') {
|
|
|
|
this.display_data();
|
|
|
|
}
|
|
|
|
break;
|
2013-12-12 10:55:33 +00:00
|
|
|
case 'swap_axis':
|
2013-12-18 13:40:40 +00:00
|
|
|
this.pivot.swap_axis();
|
2013-12-05 10:07:10 +00:00
|
|
|
break;
|
|
|
|
case 'expand_all':
|
2013-12-18 13:40:40 +00:00
|
|
|
this.pivot.rows.headers = null;
|
|
|
|
this.pivot.cols.headers = null;
|
|
|
|
this.pivot.update_data();
|
2013-12-05 10:07:10 +00:00
|
|
|
break;
|
|
|
|
case 'update_values':
|
2013-12-18 13:40:40 +00:00
|
|
|
this.pivot.update_data();
|
2013-12-05 10:07:10 +00:00
|
|
|
break;
|
|
|
|
case 'export_data':
|
|
|
|
// Export code... To do...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-12-18 13:40:40 +00:00
|
|
|
header_cell_clicked: function (event) {
|
2013-12-05 10:07:10 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2013-12-18 13:40:40 +00:00
|
|
|
var id = event.target.attributes['data-id'].nodeValue,
|
|
|
|
header = this.pivot.get_header(id),
|
2013-12-24 11:22:24 +00:00
|
|
|
self = this;
|
2013-12-18 13:40:40 +00:00
|
|
|
|
|
|
|
if (header.is_expanded) {
|
|
|
|
this.pivot.fold(header);
|
|
|
|
} else {
|
|
|
|
if (header.path.length < header.root.groupby.length) {
|
|
|
|
var field = header.root.groupby[header.path.length];
|
|
|
|
this.pivot.expand(id, field);
|
|
|
|
} else {
|
2013-12-20 12:39:25 +00:00
|
|
|
if (!this.important_fields.length) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-18 13:40:40 +00:00
|
|
|
var fields = _.map(this.important_fields, function (field) {
|
|
|
|
return {id: field, value: self.fields[field].string};
|
|
|
|
});
|
|
|
|
this.dropdown = $(QWeb.render('field_selection', {fields:fields, header_id:id}));
|
|
|
|
$(event.target).after(this.dropdown);
|
|
|
|
this.dropdown.css({position:'absolute',
|
|
|
|
left:event.pageX,
|
|
|
|
top:event.pageY});
|
|
|
|
this.$('.field-selection').next('.dropdown-menu').toggle();
|
|
|
|
}
|
|
|
|
}
|
2013-12-05 10:07:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
field_selection: function (event) {
|
2013-12-24 11:22:24 +00:00
|
|
|
var id = event.target.attributes['data-id'].nodeValue,
|
2013-12-05 10:07:10 +00:00
|
|
|
field_id = event.target.attributes['data-field-id'].nodeValue;
|
|
|
|
event.preventDefault();
|
2013-12-18 13:40:40 +00:00
|
|
|
this.pivot.expand(id, field_id);
|
2013-11-22 13:59:27 +00:00
|
|
|
},
|
|
|
|
|
2013-12-05 10:07:10 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Drawing pivot table methods...
|
|
|
|
******************************************************************************/
|
2013-11-22 07:43:27 +00:00
|
|
|
draw_table: function () {
|
2013-12-18 13:40:40 +00:00
|
|
|
this.pivot.rows.main.title = 'Total';
|
|
|
|
this.pivot.cols.main.title = this.measure_label();
|
2013-11-22 07:43:27 +00:00
|
|
|
this.draw_top_headers();
|
2013-12-18 13:40:40 +00:00
|
|
|
_.each(this.pivot.rows.headers, this.proxy('draw_row'));
|
2013-11-14 13:17:47 +00:00
|
|
|
},
|
|
|
|
|
2013-12-05 10:07:10 +00:00
|
|
|
measure_label: function () {
|
2013-12-18 13:40:40 +00:00
|
|
|
return (this.pivot.measure) ? this.fields[this.pivot.measure].string : 'Quantity';
|
2013-12-05 10:07:10 +00:00
|
|
|
},
|
|
|
|
|
2013-11-22 09:43:22 +00:00
|
|
|
make_border_cell: function (colspan, rowspan) {
|
|
|
|
return $('<td></td>').addClass('graph_border')
|
|
|
|
.attr('colspan', colspan)
|
|
|
|
.attr('rowspan', rowspan);
|
|
|
|
},
|
2013-11-19 14:52:33 +00:00
|
|
|
|
2013-11-22 09:43:22 +00:00
|
|
|
make_header_title: function (header) {
|
|
|
|
return $('<span> </span>')
|
|
|
|
.addClass('web_graph_click')
|
|
|
|
.attr('href', '#')
|
2013-12-12 10:55:33 +00:00
|
|
|
.addClass((header.is_expanded) ? 'fa fa-minus-square' : 'fa fa-plus-square')
|
2013-11-26 15:19:06 +00:00
|
|
|
.append((header.title !== undefined) ? header.title : 'Undefined');
|
2013-11-22 09:43:22 +00:00
|
|
|
},
|
2013-11-20 11:08:21 +00:00
|
|
|
|
2013-11-22 09:43:22 +00:00
|
|
|
draw_top_headers: function () {
|
|
|
|
var self = this,
|
2013-12-18 13:40:40 +00:00
|
|
|
pivot = this.pivot,
|
2013-11-23 11:42:01 +00:00
|
|
|
height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})),
|
2013-11-22 09:43:22 +00:00
|
|
|
header_cells = [[this.make_border_cell(1, height)]];
|
|
|
|
|
2013-11-22 13:59:27 +00:00
|
|
|
function set_dim (cols) {
|
|
|
|
_.each(cols.children, set_dim);
|
|
|
|
if (cols.children.length === 0) {
|
|
|
|
cols.height = height - cols.path.length + 1;
|
|
|
|
cols.width = 1;
|
|
|
|
} else {
|
|
|
|
cols.height = 1;
|
|
|
|
cols.width = _.reduce(cols.children, function (sum,c) { return sum + c.width;}, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-22 09:43:22 +00:00
|
|
|
function make_col_header (col) {
|
|
|
|
var cell = self.make_border_cell(col.width, col.height);
|
2013-11-24 21:48:09 +00:00
|
|
|
return cell.append(self.make_header_title(col).attr('data-id', col.id));
|
2013-11-14 15:33:48 +00:00
|
|
|
}
|
2013-11-14 13:17:47 +00:00
|
|
|
|
2013-11-22 07:43:27 +00:00
|
|
|
function make_cells (queue, level) {
|
2013-11-22 15:10:25 +00:00
|
|
|
var col = queue[0];
|
|
|
|
queue = _.rest(queue).concat(col.children);
|
2013-11-22 09:43:22 +00:00
|
|
|
if (col.path.length == level) {
|
|
|
|
_.last(header_cells).push(make_col_header(col));
|
2013-11-21 09:07:19 +00:00
|
|
|
} else {
|
2013-11-22 09:43:22 +00:00
|
|
|
level +=1;
|
|
|
|
header_cells.push([make_col_header(col)]);
|
2013-11-21 09:07:19 +00:00
|
|
|
}
|
2013-11-22 07:43:27 +00:00
|
|
|
if (queue.length !== 0) {
|
|
|
|
make_cells(queue, level);
|
2013-11-21 09:07:19 +00:00
|
|
|
}
|
2013-11-20 13:07:48 +00:00
|
|
|
}
|
|
|
|
|
2013-11-23 11:42:01 +00:00
|
|
|
set_dim(pivot.cols.main); // add width and height info to columns headers
|
|
|
|
if (pivot.cols.main.children.length === 0) {
|
|
|
|
make_cells(pivot.cols.headers, 0);
|
2013-11-22 09:43:22 +00:00
|
|
|
} else {
|
2013-11-23 11:42:01 +00:00
|
|
|
make_cells(pivot.cols.main.children, 1);
|
2013-11-25 09:15:05 +00:00
|
|
|
header_cells[0].push(self.make_border_cell(1, height).append('Total').css('font-weight', 'bold'));
|
2013-11-22 09:43:22 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 07:43:27 +00:00
|
|
|
_.each(header_cells, function (cells) {
|
2013-11-27 09:20:52 +00:00
|
|
|
self.table.append($('<tr></tr>').append(cells));
|
2013-11-18 15:26:20 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-12-11 12:43:07 +00:00
|
|
|
get_measure_type: function () {
|
2013-12-18 13:40:40 +00:00
|
|
|
var measure = this.pivot.measure;
|
2013-12-11 12:43:07 +00:00
|
|
|
return (measure) ? this.fields[measure].type : 'integer';
|
|
|
|
},
|
|
|
|
|
2013-11-22 07:43:27 +00:00
|
|
|
draw_row: function (row) {
|
2013-11-24 14:52:40 +00:00
|
|
|
var self = this,
|
2013-12-18 13:40:40 +00:00
|
|
|
pivot = this.pivot,
|
2013-12-11 12:43:07 +00:00
|
|
|
measure_type = this.get_measure_type(),
|
2013-11-22 07:43:27 +00:00
|
|
|
html_row = $('<tr></tr>'),
|
2013-11-22 13:59:27 +00:00
|
|
|
row_header = this.make_border_cell(1,1)
|
2013-11-24 21:48:09 +00:00
|
|
|
.append(this.make_header_title(row).attr('data-id', row.id))
|
2013-11-22 07:43:27 +00:00
|
|
|
.addClass('graph_border');
|
|
|
|
|
2013-12-27 10:25:10 +00:00
|
|
|
for (var i = 0; i < row.path.length; i++) {
|
2013-11-22 07:43:27 +00:00
|
|
|
row_header.prepend($('<span/>', {class:'web_graph_indent'}));
|
2013-11-14 13:17:47 +00:00
|
|
|
}
|
2013-11-14 10:34:46 +00:00
|
|
|
|
2013-11-22 07:43:27 +00:00
|
|
|
html_row.append(row_header);
|
2013-11-15 09:24:11 +00:00
|
|
|
|
2013-11-23 11:42:01 +00:00
|
|
|
_.each(pivot.cols.headers, function (col) {
|
2013-11-22 15:10:25 +00:00
|
|
|
if (col.children.length === 0) {
|
2013-11-24 14:52:40 +00:00
|
|
|
var value = pivot.get_value(row.id, col.id),
|
2013-11-27 15:06:28 +00:00
|
|
|
cell = make_cell(value, col);
|
2013-11-22 15:10:25 +00:00
|
|
|
html_row.append(cell);
|
|
|
|
}
|
2013-11-14 10:34:46 +00:00
|
|
|
});
|
2013-11-25 09:04:43 +00:00
|
|
|
|
|
|
|
if (pivot.cols.main.children.length > 0) {
|
2013-11-28 09:47:46 +00:00
|
|
|
var cell = make_cell(pivot.get_total(row), pivot.cols.main)
|
2013-11-27 15:06:28 +00:00
|
|
|
.css('font-weight', 'bold');
|
2013-11-25 09:04:43 +00:00
|
|
|
html_row.append(cell);
|
|
|
|
}
|
|
|
|
|
2013-11-22 07:43:27 +00:00
|
|
|
this.table.append(html_row);
|
2013-11-27 14:45:05 +00:00
|
|
|
|
2013-11-27 15:06:28 +00:00
|
|
|
function make_cell (value, col) {
|
2013-11-27 14:45:05 +00:00
|
|
|
var color,
|
2013-11-27 15:06:28 +00:00
|
|
|
total,
|
2013-11-27 14:45:05 +00:00
|
|
|
cell = $('<td></td>');
|
|
|
|
if ((self.mode === 'pivot') && (row.is_expanded) && (row.path.length <=2)) {
|
|
|
|
color = row.path.length * 5 + 240;
|
|
|
|
cell.css('background-color', $.Color(color, color, color));
|
|
|
|
}
|
2013-11-27 15:06:28 +00:00
|
|
|
if (value === undefined) {
|
|
|
|
return cell;
|
|
|
|
}
|
2013-12-11 12:43:07 +00:00
|
|
|
cell.append(instance.web.format_value(value, {type: measure_type}));
|
2013-11-27 14:45:05 +00:00
|
|
|
if (self.mode === 'heatmap') {
|
2013-11-28 09:47:46 +00:00
|
|
|
total = pivot.get_total();
|
|
|
|
color = Math.floor(50 + 205*(total - value)/total);
|
2013-11-27 14:45:05 +00:00
|
|
|
cell.css('background-color', $.Color(255, color, color));
|
|
|
|
}
|
2013-11-27 15:06:28 +00:00
|
|
|
if (self.mode === 'row_heatmap') {
|
2013-11-28 09:47:46 +00:00
|
|
|
total = pivot.get_total(row);
|
2013-11-27 15:06:28 +00:00
|
|
|
color = Math.floor(50 + 205*(total - value)/total);
|
|
|
|
cell.css('background-color', $.Color(255, color, color));
|
|
|
|
}
|
|
|
|
if (self.mode === 'col_heatmap') {
|
2013-11-28 09:47:46 +00:00
|
|
|
total = pivot.get_total(col);
|
2013-11-27 15:06:28 +00:00
|
|
|
color = Math.floor(50 + 205*(total - value)/total);
|
|
|
|
cell.css('background-color', $.Color(255, color, color));
|
|
|
|
}
|
2013-11-27 14:45:05 +00:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
},
|
2013-12-18 13:40:40 +00:00
|
|
|
|
2013-12-18 15:07:56 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Drawing charts methods...
|
|
|
|
******************************************************************************/
|
|
|
|
bar_chart: function () {
|
|
|
|
var self = this,
|
|
|
|
dim_x = this.pivot.rows.groupby.length,
|
|
|
|
dim_y = this.pivot.cols.groupby.length,
|
2013-12-18 15:49:49 +00:00
|
|
|
data;
|
2013-12-18 15:07:56 +00:00
|
|
|
|
|
|
|
// No groupby **************************************************************
|
|
|
|
if ((dim_x === 0) && (dim_y === 0)) {
|
|
|
|
data = [{key: 'Total', values:[{
|
2013-12-18 15:49:49 +00:00
|
|
|
x: 'Total',
|
|
|
|
y: this.pivot.get_value(this.pivot.rows.main.id, this.pivot.cols.main.id),
|
2013-12-18 15:07:56 +00:00
|
|
|
}]}];
|
|
|
|
// Only column groupbys ****************************************************
|
|
|
|
} else if ((dim_x === 0) && (dim_y >= 1)){
|
|
|
|
data = _.map(this.pivot.get_columns_depth(1), function (header) {
|
|
|
|
return {
|
|
|
|
key: header.title,
|
|
|
|
values: [{x:header.root.main.title, y: self.pivot.get_total(header)}]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
// Just 1 row groupby ******************************************************
|
|
|
|
} else if ((dim_x === 1) && (dim_y === 0)) {
|
|
|
|
data = _.map(this.pivot.rows.main.children, function (pt) {
|
|
|
|
var value = self.pivot.get_value(pt.id, self.pivot.cols.main.id),
|
|
|
|
title = (pt.title !== undefined) ? pt.title : 'Undefined';
|
2013-12-18 15:49:49 +00:00
|
|
|
return {x: title, y: value};
|
2013-12-18 15:07:56 +00:00
|
|
|
});
|
|
|
|
data = [{key: this.measure_label(), values:data}];
|
|
|
|
// 1 row groupby and some col groupbys**************************************
|
|
|
|
} else if ((dim_x === 1) && (dim_y >= 1)) {
|
|
|
|
data = _.map(this.pivot.get_columns_depth(1), function (colhdr) {
|
|
|
|
var values = _.map(self.pivot.get_rows_depth(1), function (header) {
|
|
|
|
return {
|
|
|
|
x: header.title || 'Undefined',
|
|
|
|
y: self.pivot.get_value(header.id, colhdr.id, 0)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return {key: colhdr.title || 'Undefined', values: values};
|
|
|
|
});
|
|
|
|
// At least two row groupby*************************************************
|
|
|
|
} else {
|
|
|
|
var keys = _.uniq(_.map(this.pivot.get_rows_depth(2), function (hdr) {
|
|
|
|
return hdr.title || 'Undefined';
|
|
|
|
}));
|
|
|
|
data = _.map(keys, function (key) {
|
|
|
|
var values = _.map(self.pivot.get_rows_depth(1), function (hdr) {
|
|
|
|
var subhdr = _.find(hdr.children, function (child) {
|
|
|
|
return ((child.title === key) || ((child.title === undefined) && (key === 'Undefined')));
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
x: hdr.title || 'Undefined',
|
|
|
|
y: (subhdr) ? self.pivot.get_total(subhdr) : 0
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return {key:key, values: values};
|
|
|
|
});
|
|
|
|
}
|
2013-12-18 15:49:49 +00:00
|
|
|
|
|
|
|
nv.addGraph(function () {
|
|
|
|
var chart = nv.models.multiBarChart()
|
|
|
|
.width(self.width)
|
|
|
|
.height(self.height)
|
2013-12-27 14:28:14 +00:00
|
|
|
.reduceXTicks(false)
|
2013-12-20 11:02:43 +00:00
|
|
|
.stacked(self.bar_ui === 'stack')
|
2013-12-27 14:55:01 +00:00
|
|
|
.showControls(false)
|
2013-12-18 15:49:49 +00:00
|
|
|
.staggerLabels(true);
|
|
|
|
|
2013-12-27 14:28:14 +00:00
|
|
|
if (self.width / data[0].values.length < 100) {
|
|
|
|
chart.rotateLabels(-15);
|
|
|
|
chart.reduceXTicks(true);
|
|
|
|
chart.margin({bottom:40});
|
|
|
|
}
|
2013-12-24 14:09:42 +00:00
|
|
|
|
2013-12-18 15:49:49 +00:00
|
|
|
d3.select(self.svg)
|
|
|
|
.datum(data)
|
|
|
|
.attr('width', self.width)
|
|
|
|
.attr('height', self.height)
|
|
|
|
.call(chart);
|
|
|
|
|
|
|
|
nv.utils.windowResize(chart.update);
|
|
|
|
return chart;
|
|
|
|
});
|
|
|
|
|
2013-12-18 15:07:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
line_chart: function () {
|
|
|
|
var self = this,
|
|
|
|
dim_x = this.pivot.rows.groupby.length,
|
|
|
|
dim_y = this.pivot.cols.groupby.length;
|
|
|
|
|
|
|
|
var data = _.map(this.pivot.get_cols_leaves(), function (col) {
|
|
|
|
var values = _.map(self.pivot.get_rows_depth(dim_x), function (row) {
|
|
|
|
return {x: row.title, y: self.pivot.get_value(row.id,col.id, 0)};
|
|
|
|
});
|
|
|
|
var title = _.map(col.path, function (p) {
|
|
|
|
return p || 'Undefined';
|
|
|
|
}).join('/');
|
|
|
|
if (dim_y === 0) {
|
|
|
|
title = self.measure_label();
|
|
|
|
}
|
|
|
|
return {values: values, key: title};
|
|
|
|
});
|
|
|
|
|
|
|
|
nv.addGraph(function () {
|
|
|
|
var chart = nv.models.lineChart()
|
|
|
|
.x(function (d,u) { return u; })
|
|
|
|
.width(self.width)
|
|
|
|
.height(self.height)
|
|
|
|
.margin({top: 30, right: 20, bottom: 20, left: 60});
|
|
|
|
|
|
|
|
d3.select(self.svg)
|
|
|
|
.attr('width', self.width)
|
|
|
|
.attr('height', self.height)
|
|
|
|
.datum(data)
|
|
|
|
.call(chart);
|
|
|
|
|
|
|
|
return chart;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
pie_chart: function () {
|
|
|
|
var self = this,
|
2013-12-24 11:22:24 +00:00
|
|
|
dim_x = this.pivot.rows.groupby.length;
|
2013-12-18 15:07:56 +00:00
|
|
|
|
|
|
|
var data = _.map(this.pivot.get_rows_leaves(), function (row) {
|
|
|
|
var title = _.map(row.path, function (p) {
|
|
|
|
return p || 'Undefined';
|
|
|
|
}).join('/');
|
|
|
|
if (dim_x === 0) {
|
|
|
|
title = self.measure_label;
|
|
|
|
}
|
|
|
|
return {x: title, y: self.pivot.get_total(row)};
|
|
|
|
});
|
|
|
|
|
|
|
|
nv.addGraph(function () {
|
|
|
|
var chart = nv.models.pieChart()
|
|
|
|
.color(d3.scale.category10().range())
|
|
|
|
.width(self.width)
|
|
|
|
.height(self.height);
|
|
|
|
|
|
|
|
d3.select(self.svg)
|
|
|
|
.datum(data)
|
|
|
|
.transition().duration(1200)
|
|
|
|
.attr('width', self.width)
|
|
|
|
.attr('height', self.height)
|
|
|
|
.call(chart);
|
|
|
|
|
|
|
|
nv.utils.windowResize(chart.update);
|
|
|
|
return chart;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-11-22 07:43:27 +00:00
|
|
|
});
|
2013-11-14 10:34:46 +00:00
|
|
|
|
2013-12-18 15:07:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|