[FIX] correctly calls the parent of the graph view in the constructor (now, the view...
[odoo/odoo.git] / addons / web_graph / static / src / js / graph.js
index bd4842e..9806e70 100644 (file)
@@ -4,7 +4,6 @@
 
 /* jshint undef: false  */
 
-
 openerp.web_graph = function (instance) {
 'use strict';
 
@@ -14,81 +13,206 @@ var QWeb = instance.web.qweb;
 
 instance.web.views.add('graph', 'instance.web_graph.GraphView');
 
- /**
-  * GraphView 
-  */
 instance.web_graph.GraphView = instance.web.View.extend({
-    template: 'GraphView',
     display_name: _lt('Graph'),
     view_type: 'graph',
 
-    events: {
-        'click .graph_mode_selection li' : 'mode_selection',
-        'click .graph_measure_selection li' : 'measure_selection',
-        'click .graph_expand_selection li' : 'expand_selection',
-        'click .graph_options_selection li' : 'option_selection',
-        'click .web_graph_click' : 'cell_click_callback',
-        'click a.field-selection' : 'field_selection',
-    },
-
     init: function(parent, dataset, view_id, options) {
-        this._super(parent);
-
-        this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});
+        this._super(parent, dataset, view_id, options);
         this.dataset = dataset;
-
-        this.pivot_table = new openerp.web_graph.PivotTable(this.model, dataset.domain);
-        this.set_default_options(options);
-        this.dropdown = null;
-        this.mode = 'pivot'; // pivot, bar_chart, line_chart, pie_chart, heatmap, row_heatmap, col_heatmap
-        this.measure = null;
-        this.measure_list = [];
-        this.important_fields = [];
+        this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});
         this.search_view = parent.searchview;
-        this.search_field = {
-                        get_context: function () { },
-                        get_domain: function () { },
-                        get_groupby: function () { },
-                    };
+        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 () { },
+        };
     },
 
-    start: function () {
-        this.table = $('<table></table>');
-        this.$('.graph_main_content').append(this.table);
-        instance.web.bus.on('click', this, function (ev) {
-            if (this.dropdown) {
-                this.dropdown.remove();
-                this.dropdown = null;
-            }
+    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};
+    },
+
+    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,
-            measure = null;
+            arch = fields_view_get.arch,
+            measure = null,
+            stacked = false;
 
-        if (fields_view_get.arch.attrs.type === 'bar') {
-            this.mode = 'bar_chart';
+        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) {
-                    self.measure_list.push(field.attrs.name);
+                    measure = (measure) ? measure : field.attrs.name;
                 } else {
                     self.default_row_groupby.push(field.attrs.name);
                 }
             }
         });
-        if (this.measure_list.length > 0) {
-            measure = this.measure_list[0];
-            this.pivot_table.set_measure(measure);
+        this.graph_widget.config({
+            measure:measure,
+            update:false,
+            bar_ui: (stacked) ? 'stack' : 'group'
+        });
+    },
+
+    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);
+        });
 
+        query.reset(rows.concat(cols));
+
+        function make_facet (category, fields) {
+            var values,
+                icon,
+                backbone_field,
+                cat_name;
+            if (!(fields instanceof Array)) { fields = [fields]; }
+            if (category === 'GroupBy') {
+                cat_name = 'group_by';
+                icon = 'w';
+                backbone_field = self.search_view._s_groupby;
+            } else {
+                cat_name = 'col_group_by';
+                icon = 'f';
+                backbone_field = self.search_field;
+            }
+            values =  _.map(fields, function (field) {
+                var context = {};
+                context[cat_name] = field;
+                return {label: self.graph_widget.fields[field].string, value: {attrs:{domain: [], context: context}}};
+            });
+            return {category:category, values: values, icon:icon, field: backbone_field};
+        }
+    },
+});
+
+instance.web_graph.Graph = instance.web.Widget.extend({
+    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 = $('<table></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'},
@@ -106,136 +230,69 @@ instance.web_graph.GraphView = instance.web.View.extend({
                 });
             });
 
-        // get the fields descriptions from the model
+        // 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 = $('<a></a>').attr('data-choice', measure)
                                          .attr('href', '#')
                                          .append(self.fields[measure].string);
                 measure_selection.append($('<li></li>').append(choice));
-
             });
         });
 
-        return $.when(deferred1, deferred2);
-    },
-
-    do_search: function (domain, context, group_by) {
-        console.log('dgb', this.default_row_groupby);
-        if (this.groupby_mode === 'getting_ready') {
-            this.groupby_mode = 'manual';
-            return;
-        }
-
-        var self = this,
-            col_groupby = get_col_groupby('ColGroupBy');
-
-        if (group_by.length || col_groupby.length) {
-            this.groupby_mode = 'manual';
-            // if (!group_by.length) {
-            //     group_by = this.default_row_groupby;
-            // }
-        }
-
-        this.pivot_table.set_domain(domain);
-        if (this.groupby_mode === 'manual') {
-            this.pivot_table.set_row_groupby(group_by);
-            this.pivot_table.set_col_groupby(col_groupby);
-        } else {
-            this.pivot_table.set_row_groupby(_.toArray(this.default_row_groupby));
-            this.pivot_table.set_col_groupby([]);
-        }
-        this.display_data();
-
-        function get_col_groupby() {
-            var groupby = [],
-                search = _.find(self.search_view.query.models, function (model) {
-                return model.attributes.category == 'ColGroupBy';
-            });
-            if (search) {
-                groupby = _.map(search.values.models, function (val) {
-                    return val.attributes.value;
-                });
+        return $.when(deferred1, deferred2).then(function () {
+            if (this.enabled) {
+                this.activate_display();
             }
-            return groupby;
-        }
+        });
     },
 
-    do_show: function () {
-        this.do_push_state({});
-        return this._super();
+    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;
+            }
+        });
     },
 
     display_data: function () {
-        var pivot = this.pivot_table;
-        if (pivot.stale_data) {
-            pivot.update_data().done(this.proxy('display_data'));
-        } else {
-            this.$('.graph_main_content svg').remove();
-            this.table.empty();
+        this.$('.graph_main_content svg').remove();
+        this.$('.graph_main_content div').remove();
+        this.table.empty();
 
-            if (pivot.no_data) {
-                var msg = 'No data available. Try to remove any filter or add some data.';
-                this.table.append($('<tr><td>' + msg + '</td></tr>'));
-            } 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($('<div><svg></svg></div>'));
-                    var svg = this.$('.graph_main_content svg')[0];
-                    openerp.web_graph.draw_chart(this.mode, this.pivot_table, svg);
-                }
-            }
+        if (this.visible_ui) {
+            this.$('.graph_header').css('display', 'block');
+        } else {
+            this.$('.graph_header').css('display', 'none');
         }
-    },
-
-
-/******************************************************************************
- * Event handling methods...
- ******************************************************************************/
-    handle_header_event: function (options) {
-        var pivot = this.pivot_table,
-            id = options.id,
-            header = pivot.get_header(id),
-            dim = header.root.groupby.length;
-
-        console.log('test');     
-        // debugger;
-        if (header.is_expanded) {
-            this.groupby_mode = 'manual';
-            pivot.fold(header);
-            if (header.root.groupby.length < dim) {
-                if (pivot.is_row(header.id)) {
-                    this.clear_groupby('GroupBy');
-                    if (header.root.groupby.length) {
-                        this.register_groupby('GroupBy', header.root.groupby);
-                    } else {
-
-                        this.display_data();
-                    }
-                } else {
-                    this.clear_groupby('ColGroupBy');
-                    if (header.root.groupby.length) {
-                        this.register_groupby('ColGroupBy', header.root.groupby);
-                    } else {
-                        this.display_data();
-                    }
-                }
-            } else {
-                this.display_data();                
-            }
+        if (this.pivot.no_data) {
+            this.$('.graph_main_content').append($(QWeb.render('graph_no_data')));
         } else {
-            if (header.path.length < header.root.groupby.length) {
-                var field = header.root.groupby[header.path.length];
-                pivot.expand(id, field).then(this.proxy('display_data'));
+            var table_modes = ['pivot', 'heatmap', 'row_heatmap', 'col_heatmap'];
+            if (_.contains(table_modes, this.mode)) {
+                this.draw_table();
             } else {
-                this.display_dropdown({id:header.id,
-                                       target: $(options.event.target),
-                                       x: options.event.pageX,
-                                       y: options.event.pageY});
+                this.$('.graph_main_content').append($('<div><svg></svg></div>'));
+                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]();
             }
         }
     },
@@ -245,111 +302,28 @@ instance.web_graph.GraphView = instance.web.View.extend({
         var mode = event.target.attributes['data-mode'].nodeValue;
         this.mode = mode;
         this.display_data();
-
-    },
-
-    register_groupby: function (category, field) {
-        var self = this,
-            values,
-            groupby;
-        if (category === 'ColGroupBy') {
-            if (!(field instanceof Array)) {
-                field = [field];
-            }
-            values = _.map(field, function (f) {
-                return {label: self.fields[f].string, value: f};
-            });
-            // values = [{label: this.fields[field].string, value: field}];
-            groupby = {
-                category: 'ColGroupBy',
-                values: values,
-                icon: 'f',
-                field: this.search_field,
-            };
-            this.search_view.query.add(groupby);
-        }
-        if (category === 'GroupBy') {
-            if (!(field instanceof Array)) {
-                field = [field];
-            }
-            values = _.map(field, function (f) {
-                return {label: self.fields[f].string, value: {attrs:{domain: [], context: {'group_by':f}}}};
-            });
-            // var value = {attrs: {domain: [], context: {'group_by':field}}};
-            // var value2 = {attrs: {domain: [], context: {'group_by':'user_id'}}}; [{label: this.fields[field].string, value: value}, {label: 'test', value:value2}]
-            groupby = {
-                category: 'GroupBy',
-                values: values,
-                icon: 'w',
-                field: this.search_view._s_groupby,
-            };
-            this.search_view.query.add(groupby);
-        }
-    },
-
-    clear_groupby: function (category) {
-        // debugger;
-        this.search_view.query.models =  _.filter(this.search_view.query.models, function (model) {
-            return model.attributes.category !== category;
-        });
     },
 
     measure_selection: function (event) {
         event.preventDefault();
         var measure = event.target.attributes['data-choice'].nodeValue;
-        this.measure = (measure === '__count') ? null : measure;
-        this.pivot_table.set_measure(this.measure);
-        this.display_data();
-    },
-
-    expand_selection: function (event) {
-        event.preventDefault();
-        switch (event.target.attributes['data-choice'].nodeValue) {
-            case 'fold_columns':
-                this.pivot_table.fold_cols();
-                this.display_data();
-                break;
-            case 'fold_rows':
-                this.pivot_table.fold_rows();
-                this.display_data();
-                break;
-            case 'fold_all':
-                this.pivot_table.fold_all();
-                this.display_data();
-                break;
-            case 'expand_all':
-                this.pivot_table.invalidate_data();
-                this.display_data();
-                break;
-        }
+        var actual_measure = (measure === '__count') ? null : measure;
+        this.pivot.config({measure:actual_measure});
     },
 
     option_selection: function (event) {
-        var pivot = this.pivot_table;
         event.preventDefault();
         switch (event.target.attributes['data-choice'].nodeValue) {
             case 'swap_axis':
-                this.pivot_table.swap_axis();
-                this.clear_groupby('GroupBy');
-                this.clear_groupby('ColGroupBy');
-                if (pivot.rows.groupby.length && pivot.cols.groupby.length) {
-                    this.groupby_mode = 'getting_ready';
-                    this.register_groupby('GroupBy', pivot.rows.groupby);
-                    this.register_groupby('ColGroupBy', pivot.cols.groupby);                    
-                } else {
-                    this.groupby_mode = 'manual';
-                    if (pivot.rows.groupby.length) {
-                        this.register_groupby('GroupBy', pivot.rows.groupby);                        
-                    } else {
-                        this.register_groupby('ColGroupBy', pivot.cols.groupby);                                                
-                    }
-                }
-
-                // this.display_data();
+                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_table.stale_data = true;
-                this.display_data();
+                this.pivot.update_data();
                 break;
             case 'export_data':
                 // Export code...  To do...
@@ -357,70 +331,55 @@ instance.web_graph.GraphView = instance.web.View.extend({
         }
     },
 
-
-    cell_click_callback: function (event) {
+    header_cell_clicked: function (event) {
         event.preventDefault();
         event.stopPropagation();
-        var id = event.target.attributes['data-id'].nodeValue;
-        this.handle_header_event({id:id, event:event});
-    },
+        var id = event.target.attributes['data-id'].nodeValue,
+            header = this.pivot.get_header(id),
+            self = this;
 
-    field_selection: function (event) {
-        var self = this,
-            id = event.target.attributes['data-id'].nodeValue,
-            field_id = event.target.attributes['data-field-id'].nodeValue;
-        event.preventDefault();
-        this.pivot_table.expand(id, field_id).then(function () {
-
-
-            if (self.pivot_table.is_row(id)) {
-                if (self.groupby_mode === 'default') {
-                    self.groupby_mode = 'manual';
-                    self.register_groupby('GroupBy', self.default_row_groupby.concat([field_id]));
-                } else {
-                    self.register_groupby('GroupBy', field_id);
-                }
+        if (header.is_expanded) {
+            this.pivot.fold(header);
+        } else {
+            if (header.path.length < header.root.groupby.length) {
+                var field = header.root.groupby[header.path.length];
+                this.pivot.expand(id, field);
             } else {
-                if (self.groupby_mode === 'default') {
-                    self.groupby_mode = 'getting_ready';
-                    self.register_groupby('GroupBy', self.default_row_groupby);
+                if (!this.important_fields.length) {
+                    return;
                 }
-                self.register_groupby('ColGroupBy', field_id);
+                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.rows.groupby.concat(pivot.cols.groupby),
-            possible_groups = _.difference(self.important_fields, already_grouped),
-            dropdown_options = {
-                header_id: options.id,
-                fields: _.map(possible_groups, function (field) {
-                    return {id: field, value: self.fields[field].string};
-            })};
-
-        this.dropdown = $(QWeb.render('field_selection', dropdown_options));
-        options.target.after(this.dropdown);
-        this.dropdown.css({position:'absolute',
-                           left:options.x,
-                           top:options.y});
-        this.$('.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 () {
-        this.pivot_table.rows.main.title = 'Total';
-        this.pivot_table.cols.main.title = this.measure_label();
+        this.pivot.rows.main.title = 'Total';
+        this.pivot.cols.main.title = this.measure_label();
         this.draw_top_headers();
-        _.each(this.pivot_table.rows.headers, this.proxy('draw_row'));
+        _.each(this.pivot.rows.headers, this.proxy('draw_row'));
     },
 
     measure_label: function () {
-        return (this.measure) ? this.fields[this.measure].string : 'Quantity';
+        return (this.pivot.measure) ? this.fields[this.pivot.measure].string : 'Quantity';
     },
 
     make_border_cell: function (colspan, rowspan) {
@@ -433,13 +392,13 @@ instance.web_graph.GraphView = instance.web.View.extend({
         return $('<span> </span>')
             .addClass('web_graph_click')
             .attr('href', '#')
-            .addClass((header.is_expanded) ? 'icon-minus-sign' : 'icon-plus-sign')
+            .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,
+            pivot = this.pivot,
             height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})),
             header_cells = [[this.make_border_cell(1, height)]];
 
@@ -486,9 +445,15 @@ instance.web_graph.GraphView = instance.web.View.extend({
         });
     },
 
+    get_measure_type: function () {
+        var measure = this.pivot.measure;
+        return (measure) ? this.fields[measure].type : 'integer';
+    },
+
     draw_row: function (row) {
         var self = this,
-            pivot = this.pivot_table,
+            pivot = this.pivot,
+            measure_type = this.get_measure_type(),
             html_row = $('<tr></tr>'),
             row_header = this.make_border_cell(1,1)
                 .append(this.make_header_title(row).attr('data-id', row.id))
@@ -527,7 +492,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
             if (value === undefined) {
                 return cell;
             }
-            cell.append(value);
+            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);
@@ -546,6 +511,164 @@ instance.web_graph.GraphView = instance.web.View.extend({
             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)};
+        });
+
+        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;
+        });
+    },
+
 });
 
 };
+
+
+
+
+
+
+
+