<commit_before>
openerp.web_graph.draw_chart = function (mode, pivot, svg, measure_label) {
    openerp.web_graph[mode](pivot, svg, measure_label);
};


openerp.web_graph.bar_chart = function (pivot, svg, measure_label) {
    var dim_x = pivot.rows.groupby.length,
        dim_y = pivot.cols.groupby.length,
        data = [];

    // No groupby **************************************************************
    if ((dim_x === 0) && (dim_y === 0)) {
        data = [{key: 'Total', values:[{
            title: 'Total',
            value: pivot.get_value(pivot.rows.main.id, pivot.cols.main.id),
        }]}];
        nv.addGraph(function () {
          var chart = nv.models.discreteBarChart()
                .x(function(d) { return d.title;})
                .y(function(d) { return d.value;})
                .tooltips(false)
                .showValues(true)
                .staggerLabels(true)
                .width(650)
                .height(400);

            d3.select(svg)
                .datum(data)
                .attr('width', 650)
                .attr('height', 400)
                .call(chart);

            nv.utils.windowResize(chart.update);
            return chart;
        });
    // Only column groupbys ****************************************************
    } else if ((dim_x === 0) && (dim_y >= 1)){
        _.each(pivot.cols.headers, function (header) {
            if (header.path.length === 1) {
                data.push({
                    key: header.title,
                    values: [{x:header.root.main.title, y: pivot.get_total(header)}]
                });
            }
        });
        nv.addGraph(function() {
            var chart = nv.models.multiBarChart()
                    .stacked(true)
                    .tooltips(false)
                    .showControls(false)
                    .width(400)
                    .height(500);

            d3.select(svg)
                .datum(data)
                .attr('width', 400)
                .attr('height', 500)
                .transition()
                .duration(500)
                .call(chart);

            nv.utils.windowResize(chart.update);

            return chart;
        });
    // Just 1 row groupby ******************************************************
    } else if ((dim_x === 1) && (dim_y === 0))  {
        data = _.map(pivot.rows.main.children, function (pt) {
            var value = pivot.get_value(pt.id, pivot.cols.main.id),
                title = (pt.title !== undefined) ? pt.title : 'Undefined';
            return {title: title, value: value};
        });
        data = [{key: 'Bar chart', values:data}];
        nv.addGraph(function () {
          var chart = nv.models.discreteBarChart()
                .x(function(d) { return d.title;})
                .y(function(d) { return d.value;})
                .tooltips(false)
                .showValues(true)
                .staggerLabels(true)
                .width(650)
                .height(400);

            d3.select(svg)
                .datum(data)
                .attr('width', 650)
                .attr('height', 400)
                .call(chart);

            nv.utils.windowResize(chart.update);
            return chart;
        });
    // 1 row groupby and some col groupbys**************************************
    } else if ((dim_x === 1) && (dim_y >= 1))  {
        data = [];
        _.each(pivot.cols.headers, function (colhdr) {
            if (colhdr.path.length === 1) {
                var values = [];
                _.each(pivot.rows.headers, function (header) {
                    if (header.path.length === 1) {
                        values.push({
                            x: header.title || 'Undefined',
                            y: pivot.get_value(header.id, colhdr.id, 0)
                        });
                    }
                });
                data.push({key: colhdr.title || 'Undefined', values: values});
            }
        });

        nv.addGraph(function () {
          var chart = nv.models.multiBarChart()
                .stacked(true)
                .staggerLabels(true)
                .tooltips(false)
                .width(650)
                .height(400);

            d3.select(svg)
                .datum(data)
                .attr('width', 650)
                .attr('height', 400)
                .call(chart);

            nv.utils.windowResize(chart.update);
            return chart;
        });
    // At least two row groupby*************************************************
    } else {
        data = [];
        var keys = _.uniq(_.map(_.filter(pivot.rows.headers, function (hdr) {
            return hdr.path.length === 2;
        }), function (hdr) {
            return hdr.title || 'Undefined';
        }));
        data = _.map(keys, function (key) {
            var values = [];
            _.each(pivot.rows.headers, function (hdr) {
                if (hdr.path.length === 1) {
                    var subhdr = _.find(hdr.children, function (child) {
                        return ((child.title === key) || ((child.title === undefined) && (key === 'Undefined')));
                    });
                    values.push({
                        x: hdr.title || 'Undefined', 
                        y: (subhdr) ? pivot.get_total(subhdr) : 0
                    });
                }
            });

            return {key:key, values: values};
        });<commit_msg>[REF] improves the charting code by using get_rows_depth and get_columns_depth methods (addon web_graph)<commit_after>
openerp.web_graph.draw_chart = function (mode, pivot, svg, measure_label) {
    openerp.web_graph[mode](pivot, svg, measure_label);
};


openerp.web_graph.bar_chart = function (pivot, svg, measure_label) {
    var dim_x = pivot.rows.groupby.length,
        dim_y = pivot.cols.groupby.length,
        data = [];

    // No groupby **************************************************************
    if ((dim_x === 0) && (dim_y === 0)) {
        data = [{key: 'Total', values:[{
            title: 'Total',
            value: pivot.get_value(pivot.rows.main.id, pivot.cols.main.id),
        }]}];
        nv.addGraph(function () {
          var chart = nv.models.discreteBarChart()
                .x(function(d) { return d.title;})
                .y(function(d) { return d.value;})
                .tooltips(false)
                .showValues(true)
                .staggerLabels(true)
                .width(650)
                .height(400);

            d3.select(svg)
                .datum(data)
                .attr('width', 650)
                .attr('height', 400)
                .call(chart);

            nv.utils.windowResize(chart.update);
            return chart;
        });
    // Only column groupbys ****************************************************
    } else if ((dim_x === 0) && (dim_y >= 1)){
        data =  _.map(pivot.get_columns_depth(1), function (header) {
            return {
                key: header.title,
                values: [{x:header.root.main.title, y: pivot.get_total(header)}]
            };
        });
        nv.addGraph(function() {
            var chart = nv.models.multiBarChart()
                    .stacked(true)
                    .tooltips(false)
                    .showControls(false)
                    .width(400)
                    .height(500);

            d3.select(svg)
                .datum(data)
                .attr('width', 400)
                .attr('height', 500)
                .transition()
                .duration(500)
                .call(chart);

            nv.utils.windowResize(chart.update);

            return chart;
        });
    // Just 1 row groupby ******************************************************
    } else if ((dim_x === 1) && (dim_y === 0))  {
        data = _.map(pivot.rows.main.children, function (pt) {
            var value = pivot.get_value(pt.id, pivot.cols.main.id),
                title = (pt.title !== undefined) ? pt.title : 'Undefined';
            return {title: title, value: value};
        });
        data = [{key: 'Bar chart', values:data}];
        nv.addGraph(function () {
          var chart = nv.models.discreteBarChart()
                .x(function(d) { return d.title;})
                .y(function(d) { return d.value;})
                .tooltips(false)
                .showValues(true)
                .staggerLabels(true)
                .width(650)
                .height(400);

            d3.select(svg)
                .datum(data)
                .attr('width', 650)
                .attr('height', 400)
                .call(chart);

            nv.utils.windowResize(chart.update);
            return chart;
        });
    // 1 row groupby and some col groupbys**************************************
    } else if ((dim_x === 1) && (dim_y >= 1))  {
        data = _.map(pivot.get_columns_depth(1), function (colhdr) {
            var values = _.map(pivot.get_rows_depth(1), function (header) {
                return {
                    x: header.title || 'Undefined',
                    y: pivot.get_value(header.id, colhdr.id, 0)
                };
            });
            return {key: colhdr.title || 'Undefined', values: values};
        });

        nv.addGraph(function () {
          var chart = nv.models.multiBarChart()
                .stacked(true)
                .staggerLabels(true)
                .tooltips(false)
                .width(650)
                .height(400);

            d3.select(svg)
                .datum(data)
                .attr('width', 650)
                .attr('height', 400)
                .call(chart);

            nv.utils.windowResize(chart.update);
            return chart;
        });
    // At least two row groupby*************************************************
    } else {
        var keys = _.uniq(_.map(pivot.get_rows_depth(2), function (hdr) {
            return hdr.title || 'Undefined';
        }));
        data = _.map(keys, function (key) {
            var values = _.map(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) ? pivot.get_total(subhdr) : 0
                };
            });
            return {key:key, values: values};
        });
{'fraction_non_alphanumeric': 0.18246445497630331, 'fraction_numerical': 0.013649289099526066, 'mean_word_length': 1.3936025408348458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5437232', 'n_tokens_mistral': 3144, 'n_tokens_neox': 2847, 'n_words': 554}