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-11-08 13:08:06 +00:00
|
|
|
view_loading: function (fields_view_get) {
|
2013-11-22 07:43:27 +00:00
|
|
|
var self = this,
|
2014-01-10 14:08:37 +00:00
|
|
|
arch = fields_view_get.arch;
|
2013-11-22 07:43:27 +00:00
|
|
|
|
2014-01-10 14:08:37 +00:00
|
|
|
this.widget_config = {
|
|
|
|
title: arch.attrs.string,
|
|
|
|
stacked : (arch.attrs.stacked === 'True'),
|
|
|
|
mode: (arch.attrs.type) ? arch.attrs.type : 'bar',
|
|
|
|
measures: [],
|
|
|
|
};
|
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':
|
2014-01-10 14:08:37 +00:00
|
|
|
self.widget_config.measures.push(field.attrs.name);
|
2013-12-20 11:02:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else { // old style, kept for backward compatibility
|
2013-11-08 15:20:48 +00:00
|
|
|
if ('operator' in field.attrs) {
|
2014-01-10 14:08:37 +00:00
|
|
|
self.widget_config.measures.push(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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-01-10 14:08:37 +00:00
|
|
|
if (self.widget_config.measures.length === 0) {
|
|
|
|
self.widget_config.measures.push('__count');
|
2014-01-03 14:22:15 +00:00
|
|
|
}
|
2014-01-10 11:35:57 +00:00
|
|
|
this.widget_config.row_groupby = self.default_row_groupby;
|
|
|
|
this.widget_config.col_groupby = self.default_col_groupby;
|
|
|
|
},
|
|
|
|
|
|
|
|
get_context: function (facet) {
|
|
|
|
var col_group_by = _.map(facet.values.models, function (model) {
|
|
|
|
return model.attributes.value.attrs.context.col_group_by;
|
2013-12-20 11:02:43 +00:00
|
|
|
});
|
2014-01-10 11:35:57 +00:00
|
|
|
return {col_group_by : col_group_by};
|
2013-12-18 13:40:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
do_search: function (domain, context, group_by) {
|
2014-01-10 14:08:37 +00:00
|
|
|
// var col_groupby = context.col_group_by || [],
|
|
|
|
// options = {domain:domain};
|
2013-12-18 13:40:40 +00:00
|
|
|
|
2014-01-10 11:35:57 +00:00
|
|
|
if (!this.graph_widget) {
|
|
|
|
this.graph_widget = new openerp.web_graph.Graph(this, this.model, domain, this.widget_config);
|
|
|
|
this.graph_widget.appendTo(this.$el);
|
|
|
|
this.graph_widget.on('groupby_changed', this, this.proxy('register_groupby'));
|
2014-01-10 14:08:37 +00:00
|
|
|
this.ViewManager.on('switch_mode', this, function (e) { if (e === 'graph') this.graph_widget.reload(); });
|
2014-01-10 11:35:57 +00:00
|
|
|
}
|
2014-01-10 14:08:37 +00:00
|
|
|
// this.search_view_groupby = group_by;
|
2013-12-24 10:09:42 +00:00
|
|
|
|
2014-01-10 14:08:37 +00:00
|
|
|
// if (group_by.length && this.groupby_mode !== 'manual') {
|
|
|
|
// if (_.isEqual(col_groupby, [])) {
|
|
|
|
// col_groupby = this.default_col_groupby;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (group_by.length || col_groupby.length) {
|
|
|
|
// this.groupby_mode = 'manual';
|
|
|
|
// }
|
|
|
|
// if (!this.graph_widget.enabled) {
|
|
|
|
// options.update = false;
|
|
|
|
// options.silent = true;
|
|
|
|
// }
|
2014-01-10 11:35:57 +00:00
|
|
|
|
2014-01-10 14:08:37 +00:00
|
|
|
// 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.set(domain, options.row_groupby, options.col_groupby);
|
|
|
|
// this.graph_widget.set_domain(domain);
|
|
|
|
// this.graph_widget.set_col_groupby(options.col_groupby);
|
|
|
|
// this.graph_widget.set_row_groupby(options.row_groupby);
|
2013-12-18 13:40:40 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
do_show: function () {
|
|
|
|
this.do_push_state({});
|
|
|
|
return this._super();
|
|
|
|
},
|
|
|
|
|
|
|
|
register_groupby: function() {
|
2014-01-10 11:35:57 +00:00
|
|
|
// var self = this,
|
|
|
|
// query = this.search_view.query;
|
|
|
|
|
|
|
|
// this.groupby_mode = 'manual';
|
|
|
|
// if (_.isEqual(this.search_view_groupby, this.graph_widget.pivot.rows.groupby) ||
|
|
|
|
// (!_.has(this.search_view, '_s_groupby'))) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// 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};
|
|
|
|
// }
|
2013-12-18 13:40:40 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2014-01-10 11:35:57 +00:00
|
|
|
instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMixin, {
|
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',
|
|
|
|
},
|
|
|
|
|
2014-01-10 11:35:57 +00:00
|
|
|
init: function(parent, model, domain, options) {
|
2013-12-18 13:40:40 +00:00
|
|
|
this._super(parent);
|
|
|
|
this.model = model;
|
2014-01-10 11:35:57 +00:00
|
|
|
this.domain = domain;
|
|
|
|
this.mode = options.mode || 'pivot';
|
|
|
|
this.title = options.title || 'Graph';
|
|
|
|
this.visible_ui = options.visible_ui || true;
|
2014-01-13 15:23:16 +00:00
|
|
|
this.bar_ui = options.bar_ui || 'group';
|
2014-01-10 11:35:57 +00:00
|
|
|
this.pivot_options = options;
|
2013-12-18 15:07:56 +00:00
|
|
|
},
|
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);
|
2014-01-10 11:35:57 +00:00
|
|
|
var def1 = this.get_search_fields().then(function (f) {
|
|
|
|
self.important_fields = f;
|
|
|
|
});
|
2013-11-13 15:47:31 +00:00
|
|
|
|
2014-01-10 14:08:37 +00:00
|
|
|
var def2 = this.model.call('fields_get', []).then(function (f) {
|
2014-01-10 11:35:57 +00:00
|
|
|
self.fields = f;
|
|
|
|
self.measure_list = self.get_measures();
|
|
|
|
self.add_measures_to_options();
|
|
|
|
});
|
|
|
|
|
2014-01-10 12:01:49 +00:00
|
|
|
return $.when(def1, def2).then(function () {
|
2014-01-10 11:35:57 +00:00
|
|
|
self.pivot = new openerp.web_graph.PivotTable(self.model, self.domain, self.fields, self.pivot_options);
|
|
|
|
self.pivot.on('redraw_required', self, self.proxy('display_data'));
|
|
|
|
self.pivot.on('groupby_changed', self, function () { self.trigger('groupby_changed'); });
|
|
|
|
instance.web.bus.on('click', self, function () {
|
|
|
|
if (self.dropdown) {
|
|
|
|
self.dropdown.remove();
|
|
|
|
self.dropdown = null;
|
|
|
|
}
|
2013-12-11 13:11:48 +00:00
|
|
|
});
|
2014-01-10 11:35:57 +00:00
|
|
|
self.pivot.activate();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
get_search_fields: function () {
|
|
|
|
var self = this,
|
|
|
|
options = {model:this.model, view_type: 'search'},
|
|
|
|
result = [];
|
|
|
|
|
|
|
|
return 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');
|
2013-12-11 13:11:48 +00:00
|
|
|
});
|
2014-01-10 11:35:57 +00:00
|
|
|
_.each(groups, function(g) {
|
|
|
|
_.each(g.children, function (g) {
|
|
|
|
if (g.attrs.context) {
|
|
|
|
var field_id = py.eval(g.attrs.context).group_by;
|
|
|
|
if (field_id) {
|
|
|
|
if (field_id instanceof Array) {
|
|
|
|
result.concat(field_id);
|
|
|
|
} else {
|
|
|
|
result.push(field_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-12-11 13:11:48 +00:00
|
|
|
});
|
2014-01-10 11:35:57 +00:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
},
|
2013-12-11 13:11:48 +00:00
|
|
|
|
2014-01-10 11:35:57 +00:00
|
|
|
get_measures: function(fields) {
|
|
|
|
var measures = [];
|
|
|
|
_.each(this.fields, function (f, id) {
|
|
|
|
if (((f.type === 'integer') || (f.type === 'float')) && (id !== 'id')) {
|
|
|
|
measures.push({field:id, type: f.type, string: f.string});
|
2013-12-18 13:40:40 +00:00
|
|
|
}
|
|
|
|
});
|
2014-01-10 11:35:57 +00:00
|
|
|
return measures;
|
2012-05-07 08:19:08 +00:00
|
|
|
},
|
2013-11-08 15:20:48 +00:00
|
|
|
|
2014-01-10 11:35:57 +00:00
|
|
|
|
2014-01-10 14:08:37 +00:00
|
|
|
set: function (domain, row_groupby, col_groupby) {
|
2014-01-10 11:35:57 +00:00
|
|
|
if (this.pivot) {
|
2014-01-10 14:08:37 +00:00
|
|
|
this.pivot.set(domain, row_groupby, col_groupby);
|
2014-01-10 11:35:57 +00:00
|
|
|
} else {
|
2014-01-10 14:08:37 +00:00
|
|
|
this.pivot_options.domain = domain;
|
|
|
|
this.pivot_options.row_groupby = row_groupby;
|
|
|
|
this.pivot_options.col_groupby = col_groupby;
|
2014-01-10 11:35:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add_measures_to_options: function() {
|
|
|
|
var measure_selection = this.$('.graph_measure_selection');
|
|
|
|
_.each(this.measure_list, function (measure) {
|
|
|
|
var choice = $('<a></a>').attr('data-choice', measure.field)
|
|
|
|
.attr('href', '#')
|
|
|
|
.append(measure.string);
|
|
|
|
measure_selection.append($('<li></li>').append(choice));
|
2013-12-18 13:40:40 +00:00
|
|
|
});
|
2013-11-22 16:04:49 +00:00
|
|
|
},
|
2013-11-22 13:59:27 +00:00
|
|
|
|
2014-01-10 11:35:57 +00:00
|
|
|
set_mode: function (mode) {
|
2014-01-10 14:08:37 +00:00
|
|
|
this.mode = mode;
|
|
|
|
this.display_data();
|
2014-01-10 11:35:57 +00:00
|
|
|
},
|
|
|
|
|
2014-01-10 14:08:37 +00:00
|
|
|
reload: function () {
|
|
|
|
this.pivot.update_data();
|
2014-01-10 11:35:57 +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;
|
2014-01-10 14:08:37 +00:00
|
|
|
this.set_mode(mode);
|
2013-12-05 10:07:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
measure_selection: function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var measure = event.target.attributes['data-choice'].nodeValue;
|
2014-01-10 11:35:57 +00:00
|
|
|
this.pivot.toggle_measure(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';
|
2014-01-13 15:23:16 +00:00
|
|
|
if (this.mode === 'bar') {
|
2013-12-27 14:55:01 +00:00
|
|
|
this.display_data();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'bar_stacked':
|
|
|
|
this.bar_ui = 'stack';
|
2014-01-13 15:23:16 +00:00
|
|
|
if (this.mode === 'bar') {
|
2013-12-27 14:55:01 +00:00
|
|
|
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':
|
2014-01-14 09:27:24 +00:00
|
|
|
this.pivot.expand_all();
|
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
|
|
|
|
2014-01-10 11:35:57 +00:00
|
|
|
if (header.expanded) {
|
2013-12-18 13:40:40 +00:00
|
|
|
this.pivot.fold(header);
|
|
|
|
} else {
|
|
|
|
if (header.path.length < header.root.groupby.length) {
|
2014-01-10 11:35:57 +00:00
|
|
|
this.pivot.expand(id);
|
2013-12-18 13:40:40 +00:00
|
|
|
} 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) {
|
2014-01-13 13:38:00 +00:00
|
|
|
return {id: field, value: self.fields[field].string, type:self.fields[field].type};
|
2013-12-18 13:40:40 +00:00
|
|
|
});
|
|
|
|
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,
|
2014-01-13 13:38:00 +00:00
|
|
|
field_id = event.target.attributes['data-field-id'].nodeValue,
|
|
|
|
interval;
|
2013-12-05 10:07:10 +00:00
|
|
|
event.preventDefault();
|
2014-01-13 13:38:00 +00:00
|
|
|
if (this.fields[field_id].type === 'date' || this.fields[field_id].type === 'datetime') {
|
|
|
|
interval = event.target.attributes['data-interval'].nodeValue;
|
|
|
|
this.pivot.expand(id, {field: field_id, interval: interval});
|
|
|
|
} else {
|
|
|
|
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 () {
|
2014-01-10 11:35:57 +00:00
|
|
|
this.pivot.main_row().title = 'Total';
|
2014-01-03 14:44:18 +00:00
|
|
|
if (this.pivot.measures.length == 1) {
|
2014-01-10 11:35:57 +00:00
|
|
|
this.pivot.main_col().title = this.pivot.measures[0].string;
|
2014-01-03 14:44:18 +00:00
|
|
|
} else {
|
2014-01-10 11:35:57 +00:00
|
|
|
this.pivot.main_col().title = this.title;
|
2014-01-03 14:44:18 +00:00
|
|
|
}
|
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-31 09:02:57 +00:00
|
|
|
make_border_cell: function (colspan, rowspan, headercell) {
|
|
|
|
var tag = (headercell) ? $('<th></th>') : $('<td></td>');
|
|
|
|
return tag.addClass('graph_border')
|
2013-12-27 15:20:55 +00:00
|
|
|
.attr('colspan', (colspan) ? colspan : 1)
|
|
|
|
.attr('rowspan', (rowspan) ? rowspan : 1);
|
2013-11-22 09:43:22 +00:00
|
|
|
},
|
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', '#')
|
2014-01-10 11:35:57 +00:00
|
|
|
.addClass((header.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-31 09:02:57 +00:00
|
|
|
thead = $('<thead></thead>'),
|
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-12-31 09:02:57 +00:00
|
|
|
header_cells = [[this.make_border_cell(1, height, true)]];
|
2013-11-22 09:43:22 +00:00
|
|
|
|
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) {
|
2014-01-03 14:22:15 +00:00
|
|
|
var cell = self.make_border_cell(col.width*pivot.measures.length, col.height, true);
|
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
|
|
|
}
|
|
|
|
|
2014-01-10 11:35:57 +00:00
|
|
|
set_dim(pivot.main_col()); // add width and height info to columns headers
|
|
|
|
if (pivot.main_col().children.length === 0) {
|
2013-11-23 11:42:01 +00:00
|
|
|
make_cells(pivot.cols.headers, 0);
|
2013-11-22 09:43:22 +00:00
|
|
|
} else {
|
2014-01-10 11:35:57 +00:00
|
|
|
make_cells(pivot.main_col().children, 1);
|
2014-01-03 14:22:15 +00:00
|
|
|
if (pivot.get_cols_leaves().length > 1) {
|
|
|
|
header_cells[0].push(self.make_border_cell(pivot.measures.length, height, true).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-12-31 09:02:57 +00:00
|
|
|
thead.append($('<tr></tr>').append(cells));
|
2013-11-18 15:26:20 +00:00
|
|
|
});
|
2014-01-03 14:22:15 +00:00
|
|
|
|
|
|
|
if (pivot.measures.length >= 2) {
|
|
|
|
thead.append(self.make_measure_row());
|
|
|
|
}
|
|
|
|
|
2013-12-31 09:02:57 +00:00
|
|
|
self.table.append(thead);
|
2013-11-18 15:26:20 +00:00
|
|
|
},
|
|
|
|
|
2014-01-03 14:22:15 +00:00
|
|
|
make_measure_row: function() {
|
|
|
|
var self = this,
|
|
|
|
measures = this.pivot.measures,
|
|
|
|
cols = this.pivot.cols.headers,
|
|
|
|
measure_cells,
|
|
|
|
measure_row = $('<tr></tr>');
|
|
|
|
|
|
|
|
measure_row.append($('<th></th>'));
|
|
|
|
|
|
|
|
_.each(cols, function (col) {
|
|
|
|
if (!col.children.length) {
|
|
|
|
for (var i = 0; i < measures.length; i++) {
|
|
|
|
measure_cells = $('<th></th>').addClass('measure_row');
|
2014-01-10 11:35:57 +00:00
|
|
|
measure_cells.append(measures[i].string);
|
2014-01-03 14:22:15 +00:00
|
|
|
measure_row.append(measure_cells);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.pivot.get_cols_leaves().length > 1) {
|
|
|
|
for (var i = 0; i < measures.length; i++) {
|
|
|
|
measure_cells = $('<th></th>').addClass('measure_row');
|
2014-01-10 11:35:57 +00:00
|
|
|
measure_cells.append(measures[i].string);
|
2014-01-03 14:22:15 +00:00
|
|
|
measure_row.append(measure_cells);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return measure_row;
|
|
|
|
},
|
|
|
|
|
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,
|
2014-01-10 11:35:57 +00:00
|
|
|
measure_types = _.pluck(this.pivot.measures, '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) {
|
2014-01-10 11:35:57 +00:00
|
|
|
var values = pivot.get_values(row.id, col.id);
|
2014-01-03 14:22:15 +00:00
|
|
|
for (var i = 0; i < values.length; i++) {
|
2014-01-03 14:32:36 +00:00
|
|
|
html_row.append(make_cell(values[i], measure_types[i], i, col));
|
2014-01-03 14:22:15 +00:00
|
|
|
}
|
2013-11-22 15:10:25 +00:00
|
|
|
}
|
2013-11-14 10:34:46 +00:00
|
|
|
});
|
2013-11-25 09:04:43 +00:00
|
|
|
|
2014-01-03 14:22:15 +00:00
|
|
|
if (pivot.get_cols_leaves().length > 1) {
|
|
|
|
var total_vals = pivot.get_total(row);
|
|
|
|
for (var j = 0; j < total_vals.length; j++) {
|
2014-01-10 11:35:57 +00:00
|
|
|
var cell = make_cell(total_vals[j], measure_types[j], j, pivot.cols[0]).css('font-weight', 'bold');
|
2014-01-03 14:22:15 +00:00
|
|
|
html_row.append(cell);
|
|
|
|
}
|
2013-11-25 09:04:43 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 07:43:27 +00:00
|
|
|
this.table.append(html_row);
|
2013-11-27 14:45:05 +00:00
|
|
|
|
2014-01-03 14:32:36 +00:00
|
|
|
function make_cell (value, measure_type, index, 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>');
|
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') {
|
2014-01-06 08:37:34 +00:00
|
|
|
total = pivot.get_total()[index];
|
2014-01-03 14:32:36 +00:00
|
|
|
color = Math.floor(90 + 165*(total - Math.abs(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') {
|
2014-01-06 08:37:34 +00:00
|
|
|
total = pivot.get_total(row)[index];
|
2014-01-03 14:32:36 +00:00
|
|
|
color = Math.floor(90 + 165*(total - Math.abs(value))/total);
|
2013-11-27 15:06:28 +00:00
|
|
|
cell.css('background-color', $.Color(255, color, color));
|
|
|
|
}
|
|
|
|
if (self.mode === 'col_heatmap') {
|
2014-01-10 11:35:57 +00:00
|
|
|
debugger;
|
2014-01-06 08:37:34 +00:00
|
|
|
total = pivot.get_total(col)[index];
|
2014-01-03 14:32:36 +00:00
|
|
|
color = Math.floor(90 + 165*(total - Math.abs(value))/total);
|
2013-11-27 15:06:28 +00:00
|
|
|
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...
|
|
|
|
******************************************************************************/
|
2014-01-10 14:08:37 +00:00
|
|
|
bar: function () {
|
2013-12-18 15:07:56 +00:00
|
|
|
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',
|
2014-01-10 11:35:57 +00:00
|
|
|
y: this.pivot.get_total(),
|
2013-12-18 15:07:56 +00:00
|
|
|
}]}];
|
|
|
|
// Only column groupbys ****************************************************
|
|
|
|
} else if ((dim_x === 0) && (dim_y >= 1)){
|
2014-01-10 11:35:57 +00:00
|
|
|
data = _.map(this.pivot.get_columns_with_depth(1), function (header) {
|
2013-12-18 15:07:56 +00:00
|
|
|
return {
|
|
|
|
key: header.title,
|
2014-01-10 11:35:57 +00:00
|
|
|
values: [{x:header.root[0].title, y: self.pivot.get_total(header)[0]}]
|
2013-12-18 15:07:56 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
// Just 1 row groupby ******************************************************
|
|
|
|
} else if ((dim_x === 1) && (dim_y === 0)) {
|
2014-01-10 11:35:57 +00:00
|
|
|
data = _.map(this.pivot.main_row().children, function (pt) {
|
|
|
|
var value = self.pivot.get_total(pt),
|
2013-12-18 15:07:56 +00:00
|
|
|
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
|
|
|
});
|
2014-01-10 11:35:57 +00:00
|
|
|
data = [{key: self.pivot.measures[0].string, values:data}];
|
2013-12-18 15:07:56 +00:00
|
|
|
// 1 row groupby and some col groupbys**************************************
|
|
|
|
} else if ((dim_x === 1) && (dim_y >= 1)) {
|
2014-01-10 11:35:57 +00:00
|
|
|
data = _.map(this.pivot.get_cols_with_depth(1), function (colhdr) {
|
|
|
|
var values = _.map(self.pivot.get_rows_with_depth(1), function (header) {
|
2013-12-18 15:07:56 +00:00
|
|
|
return {
|
|
|
|
x: header.title || 'Undefined',
|
2014-01-13 15:23:16 +00:00
|
|
|
y: self.pivot.get_values(header.id, colhdr.id)[0] || 0
|
2013-12-18 15:07:56 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
return {key: colhdr.title || 'Undefined', values: values};
|
|
|
|
});
|
|
|
|
// At least two row groupby*************************************************
|
|
|
|
} else {
|
2014-01-10 11:35:57 +00:00
|
|
|
var keys = _.uniq(_.map(this.pivot.get_rows_with_depth(2), function (hdr) {
|
2013-12-18 15:07:56 +00:00
|
|
|
return hdr.title || 'Undefined';
|
|
|
|
}));
|
|
|
|
data = _.map(keys, function (key) {
|
2014-01-10 11:35:57 +00:00
|
|
|
var values = _.map(self.pivot.get_rows_with_depth(1), function (hdr) {
|
2013-12-18 15:07:56 +00:00
|
|
|
var subhdr = _.find(hdr.children, function (child) {
|
|
|
|
return ((child.title === key) || ((child.title === undefined) && (key === 'Undefined')));
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
x: hdr.title || 'Undefined',
|
2014-01-03 14:22:15 +00:00
|
|
|
y: (subhdr) ? self.pivot.get_total(subhdr)[0] : 0
|
2013-12-18 15:07:56 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
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 15:07:19 +00:00
|
|
|
.showControls(false);
|
2013-12-18 15:49:49 +00:00
|
|
|
|
2013-12-27 15:07:19 +00:00
|
|
|
if (self.width / data[0].values.length < 80) {
|
2013-12-27 14:28:14 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2014-01-10 14:08:37 +00:00
|
|
|
line: function () {
|
2013-12-18 15:07:56 +00:00
|
|
|
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) {
|
2014-01-10 11:35:57 +00:00
|
|
|
var values = _.map(self.pivot.get_rows_with_depth(dim_x), function (row) {
|
|
|
|
return {x: row.title, y: self.pivot.get_values(row.id,col.id, 0)};
|
2013-12-18 15:07:56 +00:00
|
|
|
});
|
|
|
|
var title = _.map(col.path, function (p) {
|
|
|
|
return p || 'Undefined';
|
|
|
|
}).join('/');
|
|
|
|
if (dim_y === 0) {
|
2014-01-10 11:35:57 +00:00
|
|
|
title = self.pivot.measures[0].string;
|
2013-12-18 15:07:56 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-01-10 14:08:37 +00:00
|
|
|
pie: function () {
|
2013-12-18 15:07:56 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|