diff options
| author | niv-openerp <nicolas.vanhoren@openerp.com> | 2012-10-23 16:58:58 +0200 |
|---|---|---|
| committer | niv-openerp <nicolas.vanhoren@openerp.com> | 2012-10-23 16:58:58 +0200 |
| commit | f6a1c4986dc7bcbe2d6d2b0557de00b51ece44c6 (patch) | |
| tree | 1cb27fbc362cd45bfda2c709d698fd89bf967d2f /addons/web_graph/static/src/js/graph.js | |
| parent | 587e72300e56db38038e658a0cf17df0eaa327ea (diff) | |
lot of modifs, still not functionning
bzr revid: nicolas.vanhoren@openerp.com-20121023145858-dg34k7kqi3jkar6z
Diffstat (limited to 'addons/web_graph/static/src/js/graph.js')
| -rw-r--r-- | addons/web_graph/static/src/js/graph.js | 154 |
1 files changed, 145 insertions, 9 deletions
diff --git a/addons/web_graph/static/src/js/graph.js b/addons/web_graph/static/src/js/graph.js index 788ec3bcb3f..0295020dd92 100644 --- a/addons/web_graph/static/src/js/graph.js +++ b/addons/web_graph/static/src/js/graph.js @@ -228,15 +228,151 @@ instance.web_graph.GraphView = instance.web.View.extend({ }, graph_get_data: function () { - return this.rpc('/web_graph/graph/data_get', { - model: this.dataset.model, - domain: this.domain, - context: this.context, - group_by: this.group_by, - view_id: this.view_id, - mode: this.mode, - orientation: this.orientation, - stacked: this.stacked + var model = this.dataset.model, + domain = new instance.web.CompoundDomain(this.domain), + context = new instance.web.CompoundContext(this.context), + group_by = this.group_by, + view_id = this.view_id, + mode = this.mode, + orientation = this.orientation, + stacked = this.stacked; + + var obj = new instance.web.Model(model); + var view_get; + var fields; + var result = []; + var ticks = {}; + + return obj.call("fields_view_get", [view_id, 'graph']).pipe(function(tmp) { + view_get = tmp; + fields = view_get['fields']; + var toload = _.select(group_by, function(x) { return fields[x] === undefined }); + if (toload.length >= 1) + return obj.call("fields_get", [toload, context]); + else + return $.when([]); + }).pipe(function (fields_to_add) { + _.extend(fields, fields_to_add); + + var tree = $($.parseXML(view_get['arch'])); + + var pos = 0; + var xaxis = group_by || []; + var yaxis = []; + debugger; + tree.find("field").each(function() { + var field = $(this); + if (! field.attr("name")) + return; + if ((! group_by) && ((! pos) || field.attr('group'))) { + xaxis.push(field.attr('name')); + } + if (pos && ! field.attr('group')) { + yaxis.push(field.attr('name')); + } + pos += 1; + }); + + if (xaxis.length === 0) + throw new Error("No field for the X axis!"); + if (yaxis.length === 0) + throw new Error("No field for the Y axis!"); + + // Convert a field's data into a displayable string + + function _convert_key(field, data) { + if (fields[field]['type'] === 'many2one') + data = data && data[0]; + return data; + } + + function _convert(field, data, tick) { + tick = tick === undefined ? true : false; + if (fields[field]['type'] === 'many2one') { + data = data && data[1]; + } else if ((fields[field]['type'] === 'selection') && (fields[field]['selection'] instanceof Array)) { + var d = {}; + _.each(fields[field]['selection'], function(el) { + d[el[0]] = el[1]; + }); + data = d[data]; + } + if (tick) { + if (ticks[data] === undefined) + ticks[data] = _.size(ticks); + return ticks[data]; + } + return data || 0; + } + + function _orientation(x, y) { + if (! orientation) + return [x, y] + return [y, x] + } + + if (mode === "pie") { + return obj.call("read_group", [domain, yaxis+[xaxis[0]], [xaxis[0]]], {context: context}).pipe(function(res) { + _.each(res, function(record) { + result.push({ + 'data': [[_convert(xaxis[0], record[xaxis[0]]), record[yaxis[0]]]], + 'label': _convert(xaxis[0], record[xaxis[0]], false) + }); + }); + }); + } else if ((! stacked) || (xaxis.length < 2)) { + var defs = []; + _.each(xaxis, function(x) { + defs.push(obj.call("read_group", [domain, yaxis+[x], [x]], {context: context}).pipe(function(res) { + return [x, res]; + })); + }); + return $.when.apply($, defs).pipe(function() { + _.each(_.toArray(arguments), function(res) { + // TODO: must convert res + debugger; + var x = res[0]; + res = res[1]; + result.push({ + 'data': _.map(res, function(record) { + return _orientation(_convert(x, record[x]), record[yaxis[0]] || 0); + }), + 'label': fields[x]['string'] + }); + }); + }); + } else { + xaxis.reverse(); + return obj.call("read_group", [domain, yaxis + xaxis.slice(0, 1), xaxis.slice(0, 1)], {context: context}).pipe(function(axis) { + var defs = []; + _.each(axis, function(x) { + var key = x[xaxis[0]] + defs.push(obj.call("read_group", [domain+[(xaxis[0],'=',_convert_key(xaxis[0], key))], yaxis + xaxis.slice(1, 2), xaxis.slice(1, 2)], + {context: context}).pipe(function(res) { + return [x, key, res]; + })); + }); + return $.when.apply($, defs).pipe(function(res) { + // TODO: must convert res + debugger; + var x = res[0]; + var key = res[1]; + res = res[2]; + result.push({ + 'data': _.map(res, function(record) { + return _orientation(_convert(xaxis[1], record[xaxis[1]]), record[yaxis[0]] or 0); + }), + 'label': _convert(xaxis[0], key, false) + }) + }); + }); + } + }).pipe(function() { + var res = { + 'data': result, + 'ticks': _.map(ticks, function(el, key) { return [el, key] }) + }; + return res; }); }, |
