X-Git-Url: http://git.inspyration.org/?a=blobdiff_plain;f=addons%2Fweb_graph%2Fstatic%2Fsrc%2Fjs%2Fgraph.js;h=9806e7011a4ae081f71070ce07dc5e13dd6ea155;hb=e1e676d737a2f682ed6a106e99b63a4dd377c5d5;hp=7d7d575756c313f5015dd35c6e869c0a3e77589c;hpb=200762944abc00e5cb748af96583302237c9a669;p=odoo%2Fodoo.git diff --git a/addons/web_graph/static/src/js/graph.js b/addons/web_graph/static/src/js/graph.js index 7d7d575..9806e70 100644 --- a/addons/web_graph/static/src/js/graph.js +++ b/addons/web_graph/static/src/js/graph.js @@ -4,7 +4,6 @@ /* jshint undef: false */ - openerp.web_graph = function (instance) { 'use strict'; @@ -14,193 +13,373 @@ var QWeb = instance.web.qweb; instance.web.views.add('graph', 'instance.web_graph.GraphView'); - /** - * GraphView view. It mostly contains a widget (PivotTable), some data, and - * calls to charts function. - */ instance.web_graph.GraphView = instance.web.View.extend({ - template: 'GraphView', display_name: _lt('Graph'), view_type: 'graph', - events: { - 'click .graph_mode_selection li' : function (event) { - event.preventDefault(); - this.mode = event.target.attributes['data-mode'].nodeValue; - }, + init: function(parent, dataset, view_id, options) { + this._super(parent, dataset, view_id, options); + this.dataset = dataset; + this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true}); + this.search_view = parent.searchview; + this.search_view_groupby = []; + this.groupby_mode = 'default'; // 'default' or 'manual' + this.default_row_groupby = []; + this.default_col_groupby = []; + this.search_field = { + get_context: this.proxy('get_context'), + get_domain: function () {}, + get_groupby: function () { }, + }; + }, - 'click .web_graph_click' : function (event) { - event.preventDefault(); - if (event.target.attributes['data-row-id'] !== undefined) { - this.handle_header_event({type:'row', event:event}); + 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}; + }, - } - if (event.target.attributes['data-col-id'] !== undefined) { - this.handle_header_event({type:'col', event:event}); - } - }, - - 'click a.field-selection' : function (event) { - var id, - field_id = event.target.attributes['data-field-id'].nodeValue; - event.preventDefault(); - this.dropdown.remove(); - if (event.target.attributes['data-row-id'] !== undefined) { - id = event.target.attributes['data-row-id'].nodeValue; - this.pivot_table.expand_row(id, field_id) - .then(this.proxy('draw_table')); - } - if (event.target.attributes['data-col-id'] !== undefined) { - id = event.target.attributes['data-col-id'].nodeValue; - this.pivot_table.expand_col(id, field_id) - .then(this.proxy('draw_table')); - } - }, + start: function () { + 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')); + return this.load_view(); }, view_loading: function (fields_view_get) { var self = this, - model = new instance.web.Model(fields_view_get.model, {group_by_no_leaf: true}), - domain = [], + arch = fields_view_get.arch, measure = null, - fields, - important_fields = [], - col_groupby = [], - row_groupby = []; + stacked = false; - this.pivot_table = null; + if (arch.attrs.type === 'bar' || !_.has(arch.attrs, 'type')) { + this.graph_widget.mode = 'bar_chart'; + } + if (arch.attrs.stacked === 'True') { + stacked = true; + } - // get the default groupbys and measure defined in the field view - _.each(fields_view_get.arch.children, function (field) { - if ('name' in field.attrs) { + _.each(arch.children, function (field) { + if (_.has(field.attrs, 'type')) { + switch (field.attrs.type) { + case 'row': + self.default_row_groupby.push(field.attrs.name); + break; + case 'col': + self.default_col_groupby.push(field.attrs.name); + break; + case 'measure': + measure = field.attrs.name; + break; + } + } else { // old style, kept for backward compatibility if ('operator' in field.attrs) { - measure = field.attrs.name; + measure = (measure) ? measure : field.attrs.name; } else { - row_groupby.push(field.attrs.name); + self.default_row_groupby.push(field.attrs.name); } } }); + this.graph_widget.config({ + measure:measure, + update:false, + bar_ui: (stacked) ? 'stack' : 'group' + }); + }, - // get the most important fields (of the model) by looking at the - // groupby filters defined in the search view - var load_view = instance.web.fields_view_get({ - model: model, - view_type: 'search', + do_search: function (domain, context, group_by) { + var col_groupby = context.col_group_by || [], + options = {domain:domain}; + + this.search_view_groupby = group_by; + + 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; + } + + 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(); + this.ViewManager.on('switch_mode', this, function () {this.graph_widget.pivot.update_data(); }); + } + }, + + do_show: function () { + this.do_push_state({}); + return this._super(); + }, + + register_groupby: function() { + 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); }); - var important_fields_def = $.when(load_view).then(function (search_view) { - var groups = _.select(search_view.arch.children, function (c) { - return (c.tag == 'group') && (c.attrs.string != 'Display'); + 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}}}; }); - _.each(groups, function(g) { - _.each(g.children, function (g) { - if (g.attrs.context) { - var field_id = py.eval(g.attrs.context).group_by; - important_fields.push(field_id); - } + return {category:category, values: values, icon:icon, field: backbone_field}; + } + }, +}); + +instance.web_graph.Graph = instance.web.Widget.extend({ + template: 'GraphWidget', + + 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, []); + this.mode = 'pivot'; + if (_.has(options, 'mode')) { this.mode = mode; } + this.enabled = true; + if (_.has(options, 'enabled')) { this.enabled = options.enabled; } + this.visible_ui = true; + this.bar_ui = 'group'; // group or stack + this.config(options || {}); + }, + + // hide ui/show, stacked/grouped + config: function (options) { + if (_.has(options, 'visible_ui')) { + this.visible_ui = options.visible_ui; + } + if (_.has(options, 'bar_ui')) { + this.bar_ui = options.bar_ui; + } + this.pivot.config(options); + }, + + start: function() { + var self = this; + this.table = $('
'); + this.$('.graph_main_content').append(this.table); + // get the most important fields (of the model) by looking at the + // groupby filters defined in the search view + var options = {model:this.model, view_type: 'search'}, + 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; + self.important_fields.push(field_id); + } + }); }); }); - }); - // get the fields descriptions from the model - var field_descr_def = model.call('fields_get', []) - .then(function (fs) { fields = fs; }); - - return $.when(important_fields_def, field_descr_def) - .then(function () { - self.data = { - model: model, - domain: domain, - fields: fields, - important_fields: important_fields, - measure: measure, - measure_label: fields[measure].string, - col_groupby: [], - row_groupby: row_groupby, - groups: [], - total: null, - }; + // get the fields descriptions and measure list from the model + var deferred2 = this.model.call('fields_get', []).then(function (fs) { + self.fields = fs; + 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; + }); + + var measure_selection = self.$('.graph_measure_selection'); + _.each(self.measure_list, function (measure) { + var choice = $('').attr('data-choice', measure) + .attr('href', '#') + .append(self.fields[measure].string); + measure_selection.append($('
  • ').append(choice)); + }); + }); + + return $.when(deferred1, deferred2).then(function () { + if (this.enabled) { + this.activate_display(); + } + }); }, - start: function () { - this.table = $('
    '); - this.svg = $(''); - this.$el.filter('.graph_main_content').append(this.table); - this.$el.filter('.graph_main_content').append(this.svg); - return this.load_view(); + activate_display: function () { + this.pivot.on('redraw_required', this, this.proxy('display_data')); + this.pivot.update_data(); + this.enabled = true; + instance.web.bus.on('click', this, function () { + if (this.dropdown) { + this.dropdown.remove(); + this.dropdown = null; + } + }); }, - do_search: function (domain, context, group_by) { - var self = this; - this.data.domain = new instance.web.CompoundDomain(domain); + display_data: function () { + this.$('.graph_main_content svg').remove(); + this.$('.graph_main_content div').remove(); + this.table.empty(); - if (!this.pivot_table) { - self.pivot_table = new PivotTable(self.data); - self.pivot_table.start().then(self.proxy('draw_table')); + if (this.visible_ui) { + this.$('.graph_header').css('display', 'block'); } else { - this.pivot_table.update_values.done(function () { - self.draw_table(); - }); + this.$('.graph_header').css('display', 'none'); + } + if (this.pivot.no_data) { + this.$('.graph_main_content').append($(QWeb.render('graph_no_data'))); + } else { + var table_modes = ['pivot', 'heatmap', 'row_heatmap', 'col_heatmap']; + if (_.contains(table_modes, this.mode)) { + this.draw_table(); + } else { + this.$('.graph_main_content').append($('
    ')); + 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](); + } } }, - do_show: function () { - this.do_push_state({}); - return this._super(); + 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; + var actual_measure = (measure === '__count') ? null : measure; + this.pivot.config({measure:actual_measure}); + }, - handle_header_event: function (options) { - var pivot = this.pivot_table, - id = options.event.target.attributes['data-' + options.type + '-id'].nodeValue, - header = pivot['get_' + options.type](id); + option_selection: function (event) { + event.preventDefault(); + switch (event.target.attributes['data-choice'].nodeValue) { + case 'swap_axis': + this.pivot.swap_axis(); + break; + case 'expand_all': + this.pivot.rows.headers = null; + this.pivot.cols.headers = null; + this.pivot.update_data(); + break; + case 'update_values': + this.pivot.update_data(); + break; + case 'export_data': + // Export code... To do... + break; + } + }, + + header_cell_clicked: function (event) { + event.preventDefault(); + event.stopPropagation(); + var id = event.target.attributes['data-id'].nodeValue, + header = this.pivot.get_header(id), + self = this; if (header.is_expanded) { - pivot['fold_' + options.type](header); - this.draw_table(); + this.pivot.fold(header); } else { - if (header.path.length < pivot[options.type + '_groupby'].length) { - // expand the corresponding header - var field = pivot[options.type + '_groupby'][header.path.length]; - pivot['expand_' + options.type](id, field) - .then(this.proxy('draw_table')); + if (header.path.length < header.root.groupby.length) { + var field = header.root.groupby[header.path.length]; + this.pivot.expand(id, field); } else { - // display dropdown to query field to expand - this.display_dropdown({id:header.id, - type: options.type, - target: $(event.target), - x: event.pageX, - y: event.pageY}); + if (!this.important_fields.length) { + return; + } + 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(); } } - }, - display_dropdown: function (options) { - var self = this, - pivot = this.pivot_table, - already_grouped = pivot.row_groupby.concat(pivot.col_groupby), - possible_groups = _.difference(self.data.important_fields, already_grouped), - dropdown_options = { - fields: _.map(possible_groups, function (field) { - return {id: field, value: self.data.fields[field].string}; - })}; - dropdown_options[options.type + '_id'] = options.id; - - this.dropdown = $(QWeb.render('field_selection', dropdown_options)); - options.target.after(this.dropdown); - this.dropdown.css({position:'absolute', - left:options.x, - top:options.y}); - $('.field-selection').next('.dropdown-menu').toggle(); + field_selection: function (event) { + var id = event.target.attributes['data-id'].nodeValue, + field_id = event.target.attributes['data-field-id'].nodeValue; + event.preventDefault(); + this.pivot.expand(id, field_id); }, +/****************************************************************************** + * Drawing pivot table methods... + ******************************************************************************/ draw_table: function () { - console.log("cols",this.pivot_table.cols); - this.table.empty(); + this.pivot.rows.main.title = 'Total'; + this.pivot.cols.main.title = this.measure_label(); this.draw_top_headers(); - _.each(this.pivot_table.rows, this.proxy('draw_row')); + _.each(this.pivot.rows.headers, this.proxy('draw_row')); + }, + + measure_label: function () { + return (this.pivot.measure) ? this.fields[this.pivot.measure].string : 'Quantity'; }, make_border_cell: function (colspan, rowspan) { @@ -213,14 +392,14 @@ instance.web_graph.GraphView = instance.web.View.extend({ return $(' ') .addClass('web_graph_click') .attr('href', '#') - .addClass((header.is_expanded) ? 'icon-minus-sign' : 'icon-plus-sign') - .append(header.name); + .addClass((header.is_expanded) ? 'fa fa-minus-square' : 'fa fa-plus-square') + .append((header.title !== undefined) ? header.title : 'Undefined'); }, draw_top_headers: function () { var self = this, - pivot = this.pivot_table, - height = _.max(_.map(pivot.cols, function(g) {return g.path.length;})), + pivot = this.pivot, + height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})), header_cells = [[this.make_border_cell(1, height)]]; function set_dim (cols) { @@ -236,7 +415,7 @@ instance.web_graph.GraphView = instance.web.View.extend({ function make_col_header (col) { var cell = self.make_border_cell(col.width, col.height); - return cell.append(self.make_header_title(col).attr('data-col-id', col.id)); + return cell.append(self.make_header_title(col).attr('data-id', col.id)); } function make_cells (queue, level) { @@ -253,23 +432,31 @@ instance.web_graph.GraphView = instance.web.View.extend({ } } - set_dim(pivot.cols[0]); // add width and height info to columns headers - if (pivot.cols[0].children.length === 0) { - make_cells(pivot.cols, 0); + 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); } else { - make_cells(pivot.cols[0].children, 1); + make_cells(pivot.cols.main.children, 1); + header_cells[0].push(self.make_border_cell(1, height).append('Total').css('font-weight', 'bold')); } _.each(header_cells, function (cells) { - self.table.append($("").append(cells)); + self.table.append($('').append(cells)); }); }, + get_measure_type: function () { + var measure = this.pivot.measure; + return (measure) ? this.fields[measure].type : 'integer'; + }, + draw_row: function (row) { - var pivot = this.pivot_table, + var self = this, + pivot = this.pivot, + measure_type = this.get_measure_type(), html_row = $(''), row_header = this.make_border_cell(1,1) - .append(this.make_header_title(row).attr('data-row-id', row.id)) + .append(this.make_header_title(row).attr('data-id', row.id)) .addClass('graph_border'); for (var i in _.range(row.path.length)) { @@ -278,706 +465,210 @@ instance.web_graph.GraphView = instance.web.View.extend({ html_row.append(row_header); - _.each(pivot.cols, function (col) { + _.each(pivot.cols.headers, function (col) { if (col.children.length === 0) { - var cell = $('').append(pivot.get_value(row.id, col.id)); + var value = pivot.get_value(row.id, col.id), + cell = make_cell(value, col); html_row.append(cell); } }); + + if (pivot.cols.main.children.length > 0) { + var cell = make_cell(pivot.get_total(row), pivot.cols.main) + .css('font-weight', 'bold'); + html_row.append(cell); + } + this.table.append(html_row); - } -}); + function make_cell (value, col) { + var color, + total, + cell = $(''); + 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)); + } + if (value === undefined) { + return cell; + } + cell.append(instance.web.format_value(value, {type: measure_type})); + if (self.mode === 'heatmap') { + total = pivot.get_total(); + color = Math.floor(50 + 205*(total - value)/total); + cell.css('background-color', $.Color(255, color, color)); + } + if (self.mode === 'row_heatmap') { + total = pivot.get_total(row); + color = Math.floor(50 + 205*(total - value)/total); + cell.css('background-color', $.Color(255, color, color)); + } + if (self.mode === 'col_heatmap') { + total = pivot.get_total(col); + color = Math.floor(50 + 205*(total - value)/total); + cell.css('background-color', $.Color(255, color, color)); + } + return cell; + } + }, + +/****************************************************************************** + * Drawing charts methods... + ******************************************************************************/ + bar_chart: function () { + var self = this, + dim_x = this.pivot.rows.groupby.length, + dim_y = this.pivot.cols.groupby.length, + data; + + // No groupby ************************************************************** + if ((dim_x === 0) && (dim_y === 0)) { + data = [{key: 'Total', values:[{ + x: 'Total', + y: this.pivot.get_value(this.pivot.rows.main.id, this.pivot.cols.main.id), + }]}]; + // 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'; + return {x: title, y: value}; + }); + 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}; + }); + } + + nv.addGraph(function () { + var chart = nv.models.multiBarChart() + .width(self.width) + .height(self.height) + .stacked(self.bar_ui === 'stack') + .staggerLabels(true); + + if (dim_x === 1 && dim_y === 0) { chart.showControls(false); } + + d3.select(self.svg) + .datum(data) + .attr('width', self.width) + .attr('height', self.height) + .call(chart); + + nv.utils.windowResize(chart.update); + return chart; + }); + + }, + + 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, + dim_x = this.pivot.rows.groupby.length; + + 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)}; + }); -// make_top_headers : function () { -// var self = this, -// header; - -// function partition (columns) { -// return _.reduce(columns, function (partial, col) { -// if (partial.length === 0) return [[col]]; -// if (col.path.length > _.first(_.last(partial)).path.length) { -// _.last(partial).push(col); -// } else { -// partial.push([col]); -// } -// return partial; -// }, []); -// } - -// function side_by_side(blocks) { -// var result = _.zip.apply(_,blocks); -// result = _.map(result, function(line) {return _.compact(_.flatten(line))}); -// return result; -// } - -// function make_header_cell(col, span) { -// var options = { -// is_border:true, -// foldable:true, -// row_span: (span === undefined) ? 1 : span, -// col_id: col.id, -// }; -// var result = self.make_cell(col.value, options); -// if (col.expanded) { -// result.find('.icon-plus-sign') -// .removeClass('icon-plus-sign') -// .addClass('icon-minus-sign'); -// } -// return result; -// } - -// function calculate_width(cols) { -// if (cols.length === 1) { -// return 1; -// } -// var p = partition(_.rest(cols)); -// return _.reduce(p, function(x, y){ return x + calculate_width(y); }, 0); -// } - -// function make_header_cells(cols, height) { -// var p = partition(cols); -// if ((p.length === 1) && (p[0].length === 1)) { -// var result = [[make_header_cell(cols[0], height)]]; -// return result; -// } -// if ((p.length === 1) && (p[0].length > 1)) { -// var cell = make_header_cell(p[0][0]); -// cell.attr('colspan', calculate_width(cols)); -// return [[cell]].concat(make_header_cells(_.rest(cols), height - 1)); -// } -// if (p.length > 1) { -// return side_by_side(_.map(p, function (group) { -// return make_header_cells(group, height); -// })); -// } -// } - -// if (this.cols.length === 1) { -// header = $(''); -// header.append(this.make_cell('', {is_border:true})); -// header.append(this.make_cell(this.cols[0].value, -// {is_border:true, foldable:true, col_id:this.cols[0].id})); -// header.addClass('graph_top'); -// this.headers = [header]; -// } else { -// var height = _.max(_.map(self.cols, function(g) {return g.path.length;})); -// var header_rows = make_header_cells(_.rest(this.cols), height); - -// header_rows[0].splice(0,0,self.make_cell('', {is_border:true, }).attr('rowspan', height)) -// self.headers = []; -// _.each(header_rows, function (cells) { -// header = $(''); -// header.append(cells); -// header.addClass('graph_top'); -// self.headers.push(header); -// }); -// } -// }, - - - // mode: 'pivot', // pivot, bar_chart, line_chart or pie_chart - // pivot_table: null, - - // events: { - // 'click .graph_mode_selection li' : function (event) { - // event.preventDefault(); - // this.mode = event.target.attributes['data-mode'].nodeValue; - // this.display_data(); - // }, - // }, - - // display_data : function () { - // var content = this.$el.filter('.graph_main_content'); - // content.find('svg').remove(); - // var self = this; - // if (this.mode === 'pivot') { - // this.pivot_table.show(); - // } else { - // this.pivot_table.hide(); - // content.append(''); - // var view_fields = this.data.row_groupby.concat(this.data.measure, this.data.col_groupby); - // query_groups(this.data.model, view_fields, this.data.domain, this.data.row_groupby).then(function (groups) { - // Charts[self.mode](groups, self.data.measure, self.data.measure_label); - // }); - - // } - // }, - - - // if (this.pivot_table) { - // this.pivot_table.draw(true); - // } else { - - // this.pivot_table = new PivotTable(this.data); - // this.pivot_table.appendTo('.graph_main_content'); - // } - // this.display_data(); - - /** - * PivotTable widget. It displays the data in tabular data and allows the - * user to drill down and up in the table - */ -// var PivotTable = instance.web.Widget.extend({ -// template: 'pivot_table', - -// init: function (data) { -// var self = this; -// makePivotTable(this.data).done(function (table) { -// self.pivottable = table; -// }); -// }, - -// }); - -// var PivotTable = instance.web.Widget.extend({ -// template: 'pivot_table', -// data: null, -// headers: [], -// rows: [], -// cols: [], -// id_seed : 0, - -// events: { -// 'click .web_graph_click' : function (event) { -// event.preventDefault(); - -// if (event.target.attributes['data-row-id'] !== undefined) { -// this.handle_row_event(event); -// } -// if (event.target.attributes['data-col-id'] !== undefined) { -// this.handle_col_event(event); -// } -// }, - -// 'click a.field-selection' : function (event) { -// var id, -// field_id = event.target.attributes['data-field-id'].nodeValue; -// event.preventDefault(); -// this.dropdown.remove(); -// if (event.target.attributes['data-row-id'] !== undefined) { -// id = event.target.attributes['data-row-id'].nodeValue; -// this.expand_row(id, field_id); -// } -// if (event.target.attributes['data-col-id'] !== undefined) { -// id = event.target.attributes['data-col-id'].nodeValue; -// this.expand_col(id, field_id); -// } -// }, -// }, - -// handle_row_event: function (event) { -// var row_id = event.target.attributes['data-row-id'].nodeValue, -// row = this.get_row(row_id); - -// if (row.expanded) { -// this.fold_row(row_id); -// } else { -// if (row.path.length < this.data.row_groupby.length) { -// var field_to_expand = this.data.row_groupby[row.path.length]; -// this.expand_row(row_id, field_to_expand); -// } else { -// this.display_dropdown({row_id:row_id, -// target: $(event.target), -// x: event.pageX, -// y: event.pageY}); -// } -// } -// }, - -// handle_col_event: function (event) { -// var col_id = event.target.attributes['data-col-id'].nodeValue, -// col = this.get_col(col_id); - -// if (col.expanded) { -// this.fold_col(col_id); -// } else { -// if (col.path.length < this.data.col_groupby.length) { -// var field_to_expand = this.data.col_groupby[col.path.length]; -// this.expand_col(col_id, field_to_expand); -// } else { -// this.display_dropdown({col_id: col_id, -// target: $(event.target), -// x: event.pageX, -// y: event.pageY}); -// } -// } -// }, - -// init: function (data) { -// this.data = data; -// makePivotTable(this.data).done(function (table) { -// self.pivottable = table; -// }); - -// }, - -// start: function () { -// this.draw(true); -// }, - -// draw: function (load_data) { -// var self = this; - -// if (load_data) { -// var view_fields = this.data.row_groupby.concat(this.data.measure, this.data.col_groupby); -// query_groups_data(this.data.model, view_fields, this.data.domain, this.data.col_groupby, this.data.row_groupby[0]) -// .then(function (groups) { -// self.data.groups = groups; -// return self.get_groups([]); -// }).then(function (total) { -// total[0].path = []; -// self.data.total = [total]; -// self.build_table(); -// self.draw(false); -// }); -// } else { -// this.$el.empty(); - -// this.draw_top_headers(); - -// _.each(this.rows, function (row) { -// self.$el.append(row.html); -// }); -// } -// }, - -// show: function () { -// this.$el.css('display', 'block'); -// }, - -// hide: function () { -// this.$el.css('display', 'none'); -// }, - -// display_dropdown: function (options) { -// var self = this, -// already_grouped = self.data.row_groupby.concat(self.data.col_groupby), -// possible_groups = _.difference(self.data.important_fields, already_grouped), -// dropdown_options = { -// fields: _.map(possible_groups, function (field) { -// return {id: field, value: self.get_descr(field)}; -// })}; -// if (options.row_id) { -// dropdown_options.row_id= options.row_id; -// } else { -// dropdown_options.col_id = options.col_id; -// } - -// this.dropdown = $(QWeb.render('field_selection', dropdown_options)); -// options.target.after(this.dropdown); -// this.dropdown.css({position:'absolute', -// left:options.x, -// top:options.y}); -// $('.field-selection').next('.dropdown-menu').toggle(); -// }, - -// build_table: function () { -// var self = this; -// this.rows = []; - - -// var col_id = this.generate_id(); - -// this.cols= [{ -// id: col_id, -// path: [], -// value: this.data.measure_label, -// expanded: false, -// parent: null, -// children: [], -// cells: [], // a cell is {td:, row_id:} -// domain: this.data.domain, -// }]; - -// self.make_top_headers(); - -// var main_row = this.make_row(this.data.total[0]); - -// _.each(this.data.groups, function (group) { -// self.make_row(group, main_row.id); -// }); -// }, - -// get_descr: function (field_id) { -// return this.data.fields[field_id].string; -// }, - -// get_groups: function (groupby) { -// var view_fields = this.data.row_groupby.concat(this.data.measure, this.data.col_groupby); -// return query_groups(this.data.model, view_fields, this.data.domain, groupby); -// }, - -// make_top_headers : function () { -// var self = this, -// header; - -// function partition (columns) { -// return _.reduce(columns, function (partial, col) { -// if (partial.length === 0) return [[col]]; -// if (col.path.length > _.first(_.last(partial)).path.length) { -// _.last(partial).push(col); -// } else { -// partial.push([col]); -// } -// return partial; -// }, []); -// } - -// function side_by_side(blocks) { -// var result = _.zip.apply(_,blocks); -// result = _.map(result, function(line) {return _.compact(_.flatten(line))}); -// return result; -// } - -// function make_header_cell(col, span) { -// var options = { -// is_border:true, -// foldable:true, -// row_span: (span === undefined) ? 1 : span, -// col_id: col.id, -// }; -// var result = self.make_cell(col.value, options); -// if (col.expanded) { -// result.find('.icon-plus-sign') -// .removeClass('icon-plus-sign') -// .addClass('icon-minus-sign'); -// } -// return result; -// } - -// function calculate_width(cols) { -// if (cols.length === 1) { -// return 1; -// } -// var p = partition(_.rest(cols)); -// return _.reduce(p, function(x, y){ return x + calculate_width(y); }, 0); -// } - -// function make_header_cells(cols, height) { -// var p = partition(cols); -// if ((p.length === 1) && (p[0].length === 1)) { -// var result = [[make_header_cell(cols[0], height)]]; -// return result; -// } -// if ((p.length === 1) && (p[0].length > 1)) { -// var cell = make_header_cell(p[0][0]); -// cell.attr('colspan', calculate_width(cols)); -// return [[cell]].concat(make_header_cells(_.rest(cols), height - 1)); -// } -// if (p.length > 1) { -// return side_by_side(_.map(p, function (group) { -// return make_header_cells(group, height); -// })); -// } -// } - -// if (this.cols.length === 1) { -// header = $(''); -// header.append(this.make_cell('', {is_border:true})); -// header.append(this.make_cell(this.cols[0].value, -// {is_border:true, foldable:true, col_id:this.cols[0].id})); -// header.addClass('graph_top'); -// this.headers = [header]; -// } else { -// var height = _.max(_.map(self.cols, function(g) {return g.path.length;})); -// var header_rows = make_header_cells(_.rest(this.cols), height); - -// header_rows[0].splice(0,0,self.make_cell('', {is_border:true, }).attr('rowspan', height)) -// self.headers = []; -// _.each(header_rows, function (cells) { -// header = $(''); -// header.append(cells); -// header.addClass('graph_top'); -// self.headers.push(header); -// }); -// } -// }, - -// draw_top_headers: function () { -// var self = this; -// $("tr.graph_top").remove(); -// _.each(this.headers.reverse(), function (header) { -// self.$el.prepend(header); -// }); - -// }, - -// make_row: function (groups, parent_id) { -// var self = this, -// path, -// value, -// expanded, -// domain, -// parent, -// has_parent = (parent_id !== undefined), -// row_id = this.generate_id(); - -// if (has_parent) { -// parent = this.get_row(parent_id); -// path = parent.path.concat(groups[0].attributes.value[1]); -// value = groups[0].attributes.value[1]; -// expanded = false; -// parent.children.push(row_id); -// domain = groups[0].model._domain; -// } else { -// parent = null; -// path = []; -// value = 'Total'; -// expanded = true; -// domain = this.data.domain; -// } - -// var jquery_row = $(''); - -// var header = self.make_cell(value, {is_border:true, indent: path.length, foldable:true, row_id: row_id}); -// jquery_row.append(header); - -// var cell; - -// _.each(this.cols, function (col) { -// var element = _.find(groups, function (group) { -// return _.isEqual(_.rest(group.path), col.path); -// }); -// if (element === undefined) { -// cell = self.make_cell(''); -// } else { -// cell = self.make_cell(element.attributes.aggregates[self.data.measure]); -// } -// if (col.expanded) { -// cell.css('display', 'none'); -// } -// col.cells.push({td:cell, row_id:row_id}); -// jquery_row.append(cell); -// }); - -// if (!has_parent) { -// header.find('.icon-plus-sign') -// .removeClass('icon-plus-sign') -// .addClass('icon-minus-sign'); -// } - -// var row = { -// id: row_id, -// path: path, -// value: value, -// expanded: expanded, -// parent: parent_id, -// children: [], -// html: jquery_row, -// domain: domain, -// }; -// this.rows.push(row); // to do, insert it properly, after all childs of parent -// return row; -// }, - -// generate_id: function () { -// this.id_seed += 1; -// return this.id_seed - 1; -// }, - -// get_row: function (id) { -// return _.find(this.rows, function(row) { -// return (row.id == id); -// }); -// }, - -// get_col: function (id) { -// return _.find(this.cols, function(col) { -// return (col.id == id); -// }); -// }, - -// make_cell: function (content, options) { -// options = _.extend({is_border: false, indent:0, foldable:false}, options); -// content = (content !== undefined) ? content : 'Undefined'; - -// var cell = $(''); -// if (options.is_border) cell.addClass('graph_border'); -// if (options.row_span) cell.attr('rowspan', options.row_span); -// if (options.col_span) cell.attr('rowspan', options.col_span); -// _.each(_.range(options.indent), function () { -// cell.prepend($('', {class:'web_graph_indent'})); -// }); - -// if (options.foldable) { -// var attrs = {class:'icon-plus-sign web_graph_click', href:'#'}; -// if (options.row_id !== undefined) attrs['data-row-id'] = options.row_id; -// if (options.col_id !== undefined) attrs['data-col-id'] = options.col_id; -// var plus = $('', attrs); -// plus.append(' '); -// plus.append(content); -// cell.append(plus); -// } else { -// cell.append(content); -// } -// return cell; -// }, - -// expand_row: function (row_id, field_id) { -// var self = this; -// var row = this.get_row(row_id); - -// if (row.path.length == this.data.row_groupby.length) { -// this.data.row_groupby.push(field_id); -// } -// row.expanded = true; -// row.html.find('.icon-plus-sign') -// .removeClass('icon-plus-sign') -// .addClass('icon-minus-sign'); - -// var visible_fields = this.data.row_groupby.concat(this.data.col_groupby, this.data.measure); -// query_groups_data(this.data.model, visible_fields, row.domain, this.data.col_groupby, field_id) -// .then(function (groups) { -// _.each(groups.reverse(), function (group) { -// var new_row = self.make_row(group, row_id); -// row.html.after(new_row.html); -// }); -// }); - -// }, - -// expand_col: function (col_id, field_id) { -// var self = this; -// var col = this.get_col(col_id); - -// if (col.path.length == this.data.col_groupby.length) { -// this.data.col_groupby.push(field_id); -// } -// col.expanded = true; - -// var visible_fields = this.data.row_groupby.concat(this.data.col_groupby, this.data.measure); -// query_groups_data(this.data.model, visible_fields, col.domain, this.data.row_groupby, field_id) -// .then(function (groups) { -// _.each(groups, function (group) { -// var new_col = { -// id: self.generate_id(), -// path: col.path.concat(group[0].attributes.value[1]), -// value: group[0].attributes.value[1], -// expanded: false, -// parent: col_id, -// children: [], -// cells: [], // a cell is {td:, row_id:} -// domain: group[0].model._domain, -// }; -// col.children.push(new_col.id); -// insertAfter(self.cols, col, new_col) -// _.each(col.cells, function (cell) { -// var col_path = self.get_row(cell.row_id).path; - -// var datapt = _.find(group, function (g) { -// return _.isEqual(g.path.slice(1), col_path); -// }); - -// var value; -// if (datapt === undefined) { -// value = ''; -// } else { -// value = datapt.attributes.aggregates[self.data.measure]; -// } -// var new_cell = { -// row_id: cell.row_id, -// td: self.make_cell(value) -// }; -// new_col.cells.push(new_cell); -// cell.td.after(new_cell.td); -// cell.td.css('display','none'); -// }); - -// }); -// self.make_top_headers(); -// self.draw_top_headers(); -// }); -// }, - -// fold_row: function (row_id) { -// var self = this; -// var row = this.get_row(row_id); - -// _.each(row.children, function (child_row) { -// self.remove_row(child_row); -// }); -// row.children = []; - -// row.expanded = false; -// row.html.find('.icon-minus-sign') -// .removeClass('icon-minus-sign') -// .addClass('icon-plus-sign'); - -// var fold_levels = _.map(self.rows, function(g) {return g.path.length;}); -// var new_groupby_length = _.max(fold_levels); - -// this.data.row_groupby.splice(new_groupby_length); -// }, - -// remove_row: function (row_id) { -// var self = this; -// var row = this.get_row(row_id); - -// _.each(row.children, function (child_row) { -// self.remove_row(child_row); -// }); - -// row.html.remove(); -// removeFromArray(this.rows, row); - -// _.each(this.cols, function (col) { -// col.cells = _.filter(col.cells, function (cell) { -// return cell.row_id !== row_id; -// }); -// }); -// }, - -// fold_col: function (col_id) { -// var self = this; -// var col = this.get_col(col_id); - -// _.each(col.children, function (child_col) { -// self.remove_col(child_col); -// }); -// col.children = []; - -// _.each(col.cells, function (cell) { -// cell.td.css('display','table-cell'); -// }); -// col.expanded = false; -// // row.html.find('.icon-minus-sign') -// // .removeClass('icon-minus-sign') -// // .addClass('icon-plus-sign'); - -// var fold_levels = _.map(self.cols, function(g) {return g.path.length;}); -// var new_groupby_length = _.max(fold_levels); - -// this.data.col_groupby.splice(new_groupby_length); -// this.make_top_headers(); -// this.draw_top_headers(); - - -// }, - -// remove_col: function (col_id) { -// var self = this; -// var col = this.get_col(col_id); - -// _.each(col.children, function (child_col) { -// self.remove_col(child_col); -// }); - -// _.each(col.cells, function (cell) { -// cell.td.remove(); -// }); -// // row.html.remove(); -// removeFromArray(this.cols, col); - -// // _.each(this.cols, function (col) { -// // col.cells = _.filter(col.cells, function (cell) { -// // return cell.row_id !== row_id; -// // }); -// // }); -// }, - - -// }); + 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; + }); + }, + +}); }; + + + + + + + +