1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 openerp.web_graph = function (openerp) {
7 '#cc99ff', '#ccccff', '#48D1CC', '#CFD784', '#8B7B8B', '#75507b',
8 '#b0008c', '#ff0000', '#ff8e00', '#9000ff', '#0078ff', '#00ff00',
9 '#e6ff00', '#ffff00', '#905000', '#9b0000', '#840067', '#9abe00',
10 '#ffc900', '#510090', '#0000c9', '#009b00', '#75507b', '#3465a4',
11 '#73d216', '#c17d11', '#edd400', '#fcaf3e', '#ef2929', '#ff00c9',
12 '#ad7fa8', '#729fcf', '#8ae234', '#e9b96e', '#fce94f', '#f57900',
13 '#cc0000', '#d400a8'];
15 var QWeb = openerp.web.qweb,
16 _lt = openerp.web._lt;
17 openerp.web.views.add('graph', 'openerp.web_graph.GraphView');
18 openerp.web_graph.GraphView = openerp.web.View.extend({
19 display_name: _lt('Graph'),
21 init: function(parent, dataset, view_id, options) {
23 this.set_default_options(options);
24 this.dataset = dataset;
25 this.view_id = view_id;
27 this.first_field = null;
31 this.group_field = null;
32 this.is_loaded = $.Deferred();
38 clearTimeout(this.renderer);
46 if (this.embedded_view) {
47 loaded = $.when([self.embedded_view]);
49 loaded = this.rpc('/web/view/load', {
50 model: this.dataset.model,
51 view_id: this.view_id,
56 this.dataset.call_and_eval('fields_get', [false, {}], null, 1),
58 .then(function (fields_result, view_result) {
59 self.fields = fields_result[0];
60 self.fields_view = view_result[0];
61 self.on_loaded(self.fields_view);
65 * Returns all object fields involved in the graph view
67 list_fields: function () {
68 var fs = [this.abscissa];
69 fs.push.apply(fs, _(this.columns).pluck('name'));
70 if (this.group_field) {
71 fs.push(this.group_field);
75 on_loaded: function() {
76 this.chart = this.fields_view.arch.attrs.type || 'pie';
77 this.orientation = this.fields_view.arch.attrs.orientation || 'vertical';
79 _.each(this.fields_view.arch.children, function (field) {
80 var attrs = field.attrs;
82 this.group_field = attrs.name;
83 } else if(!this.abscissa) {
84 this.first_field = this.abscissa = attrs.name;
88 operator: attrs.operator || '+'
92 this.ordinate = this.columns[0].name;
93 this.is_loaded.resolve();
95 schedule_chart: function(results) {
97 this.$element.html(QWeb.render("GraphView", {
98 "fields_view": this.fields_view,
100 'element_id': this.element_id
103 var fields = _(this.columns).pluck('name').concat([this.abscissa]);
104 if (this.group_field) { fields.push(this.group_field); }
105 // transform search result into usable records (convert from OpenERP
106 // value shapes to usable atomic types
107 var records = _(results).map(function (result) {
109 _(result).each(function (value, field) {
110 if (!_(fields).contains(field)) { return; }
111 if (value === false) { point[field] = false; return; }
112 switch (self.fields[field].type) {
114 point[field] = _(self.fields[field].selection).detect(function (choice) {
115 return choice[0] === value;
119 point[field] = value[1];
121 case 'integer': case 'float': case 'char':
122 case 'date': case 'datetime':
123 point[field] = value;
127 "Unknown field type " + self.fields[field].type
128 + "for field " + field + " (" + value + ")");
133 // aggregate data, because dhtmlx is crap. Aggregate on abscissa field,
134 // leave split on group field => max m*n records where m is the # of
135 // values for the abscissa and n is the # of values for the group field
137 _(records).each(function (record) {
138 var abscissa = record[self.abscissa],
139 group = record[self.group_field];
140 var r = _(graph_data).detect(function (potential) {
141 return potential[self.abscissa] === abscissa
142 && (!self.group_field
143 || potential[self.group_field] === group);
145 var datapoint = r || {};
147 datapoint[self.abscissa] = abscissa;
148 if (self.group_field) { datapoint[self.group_field] = group; }
149 _(self.columns).each(function (column) {
150 var val = record[column.name],
151 aggregate = datapoint[column.name];
152 switch(column.operator) {
154 datapoint[column.name] = (aggregate || 0) + val;
157 datapoint[column.name] = (aggregate || 1) * val;
160 datapoint[column.name] = (aggregate || Infinity) > val
165 datapoint[column.name] = (aggregate || -Infinity) < val
171 if (!r) { graph_data.push(datapoint); }
173 graph_data = _(graph_data).sortBy(function (point) {
174 return point[self.abscissa] + '[[--]]' + point[self.group_field];
176 if (_.include(['bar','line','area'],this.chart)) {
177 return this.schedule_bar_line_area(graph_data);
178 } else if (this.chart == "pie") {
179 return this.schedule_pie(graph_data);
182 schedule_bar_line_area: function(results) {
185 view_chart = (self.chart == 'line')?'line':(self.chart == 'area')?'area':'';
186 if (!this.group_field) {
187 if (self.chart == 'bar'){
188 view_chart = (this.orientation === 'horizontal') ? 'barH' : 'bar';
190 group_list = _(this.columns).map(function (column, index) {
193 text: self.fields[column.name].string,
194 color: COLOR_PALETTE[index % (COLOR_PALETTE.length)]
198 // dhtmlx handles clustered bar charts (> 1 column per abscissa
199 // value) and stacked bar charts (basically the same but with the
200 // columns on top of one another instead of side by side), but it
201 // does not handle clustered stacked bar charts
202 if (self.chart == 'bar' && (this.columns.length > 1)) {
204 'OpenERP Web does not support combining grouping and '
205 + 'multiple columns in graph at this time.');
207 'dhtmlx can not handle columns counts of that magnitude');
209 // transform series for clustered charts into series for stacked
211 if (self.chart == 'bar'){
212 view_chart = (this.orientation === 'horizontal')
213 ? 'stackedBarH' : 'stackedBar';
215 group_list = _(results).chain()
216 .pluck(this.group_field)
218 .map(function (value, index) {
220 group: self.ordinate + '_' +
221 value.toLowerCase().replace(/[\s\/]+/g,'_'),
223 color: COLOR_PALETTE[index % COLOR_PALETTE.length]
227 results = _(results).chain()
228 .groupBy(function (record) { return record[self.abscissa]; })
229 .map(function (records) {
231 // second argument is coerced to a str, no good for boolean
232 r[self.abscissa] = records[0][self.abscissa];
233 _(records).each(function (record) {
234 var key = _.str.sprintf('%s_%s',
236 record[self.group_field].toLowerCase().replace(/[\s\/]+/g,'_'));
237 r[key] = record[self.ordinate];
243 var abscissa_description = {
244 title: "<b>" + this.fields[this.abscissa].string + "</b>",
245 template: function (obj) {
246 return obj[self.abscissa] || 'Undefined';
249 var ordinate_description = {
251 title: "<b>" + this.fields[this.ordinate].string + "</b>"
255 if (self.chart == 'bar' && self.orientation == 'horizontal') {
256 x_axis = ordinate_description;
257 y_axis = abscissa_description;
259 x_axis = abscissa_description;
260 y_axis = ordinate_description;
262 var renderer = function () {
263 if (self.$element.is(':hidden')) {
264 self.renderer = setTimeout(renderer, 100);
267 self.renderer = null;
268 var charts = new dhtmlXChart({
270 container: self.element_id+"-"+self.chart+"chart",
271 value:"#"+group_list[0].group+"#",
272 gradient: (self.chart == "bar") ? "3d" : "light",
273 alpha: (self.chart == "area") ? 0.6 : 1,
277 template: _.str.sprintf("#%s#, %s=#%s#",
278 self.abscissa, group_list[0].text, group_list[0].group)
281 color: (self.chart != "line") ? group_list[0].color : "",
282 item: (self.chart == "line") ? {
283 borderColor: group_list[0].color,
286 line: (self.chart == "line") ? {
287 color: group_list[0].color,
308 for (var m = 1; m<group_list.length;m++){
309 var column = group_list[m];
310 if (column.group === self.group_field) { continue; }
312 value: "#"+column.group+"#",
314 template: _.str.sprintf("#%s#, %s=#%s#",
315 self.abscissa, column.text, column.group)
317 color: (self.chart != "line") ? column.color : "",
318 item: (self.chart == "line") ? {
319 borderColor: column.color,
322 line: (self.chart == "line") ? {
328 charts.parse(results, "json");
329 self.$element.find("#"+self.element_id+"-"+self.chart+"chart").height(
330 self.$element.find("#"+self.element_id+"-"+self.chart+"chart").height()+50);
331 charts.attachEvent("onItemClick", function(id) {
332 self.open_list_view(charts.get(id));
336 clearTimeout(this.renderer);
338 this.renderer = setTimeout(renderer, 0);
340 schedule_pie: function(result) {
342 var renderer = function () {
343 if (self.$element.is(':hidden')) {
344 self.renderer = setTimeout(renderer, 100);
347 self.renderer = null;
348 var chart = new dhtmlXChart({
350 container:self.element_id+"-piechart",
351 value:"#"+self.ordinate+"#",
352 pieInnerText:function(obj) {
353 var sum = chart.sum("#"+self.ordinate+"#");
354 var val = obj[self.ordinate] / sum * 100 ;
355 return val.toFixed(1) + "%";
358 template:"#"+self.abscissa+"#"+"="+"#"+self.ordinate+"#"
372 template:function(obj){
373 return obj[self.abscissa] || 'Undefined';
377 chart.parse(result,"json");
378 chart.attachEvent("onItemClick", function(id) {
379 self.open_list_view(chart.get(id));
383 clearTimeout(this.renderer);
385 this.renderer = setTimeout(renderer, 0);
387 open_list_view : function (id){
389 // unconditionally nuke tooltips before switching view
390 $(".dhx_tooltip").remove('div');
391 id = id[this.abscissa];
392 if(this.fields[this.abscissa].type == "selection"){
393 id = _.detect(this.fields[this.abscissa].selection,function(select_value){
394 return _.include(select_value, id);
397 if (typeof id == 'object'){
401 var record_id = this.abscissa;
402 var modes = ["list", "form", "graph"];
404 _.each(modes, function(mode) {
405 var view = [false, mode];
406 if (self.fields.views && self.fields.views[mode]) {
407 view.push(self.fields.views[mode]);
412 "res_model" : this.dataset.model,
413 "domain" : [[record_id, '=', id], ['id','in',this.dataset.ids]],
415 "type" : "ir.actions.act_window",
416 "view_type" : "list",
421 do_search: function(domain, context, group_by) {
423 return $.when(this.is_loaded).pipe(function() {
424 // TODO: handle non-empty group_by with read_group?
425 if (!_(group_by).isEmpty()) {
426 self.abscissa = group_by[0];
428 self.abscissa = self.first_field;
430 return self.dataset.read_slice(self.list_fields(), {}, $.proxy(self, 'schedule_chart'));
434 do_show: function() {
435 this.do_push_state({});
436 return this._super();
440 // vim:et fdc=0 fdl=0: