<commit_before>
(function(){

    var MAX_LABEL_WIDTH = 160;

    dw.visualization.register('bar-chart', 'raphael-chart', {

        render: function(el) {
            this.setRoot(el);

            var me = this, row = 0,
                dataset = me.dataset,
                sortBars = me.get('sort-values', false),
                reverse = me.get('reverse-order'),
                useNegativeColor = me.get('negative-color', false);

            me.axesDef = me.axes();
            if (!me.axesDef) return;

            if (!_.isUndefined(me.get('selected-row'))) {
                row = me.get('selected-row', 0);
                row = row > me.axesDef.bars.length ? 0 : row;
            }
            me.__lastRow = row;

            var sliceColumns = _.map(me.axesDef.bars, function(i) { return dataset.column(i); }),
                filter = dw.utils.filter(dw.utils.columnNameColumn(sliceColumns), row),
                filterUI = filter.ui(me);

            if (filterUI) {
                $('#header').append(filterUI);
                filter.change(function(val, i) {
                    me.__lastRow = i;
                    me.update(i);
                });
            }

            var c = me.initCanvas({
                h: Math.max(
                    dw.utils.getMaxChartHeight(el)+5,
                    18 * 1.35 * me.getBarColumn().length + 5
                )
            });

            var
                chart_width = c.w - c.lpad - c.rpad,
                series_gap = 0.05, // pull from theme
                row_gap = 0.01,
                formatValue = me.chart().columnFormatter(me.getBarColumn()),
                barvalues = me.getBarValues(sortBars, reverse);

            me.init();
            me.initDimensions();

            $('.tooltip').hide();

            c.lastBarY = 0;

            function render(barv) {
                var d = me.barDimensions(barv, s, row),
                    lpos = me.labelPosition(barv, s, row),
                    fill = me.getKeyColor(barv.name, barv.value, useNegativeColor),
                    stroke = chroma.color(fill).darken(14).hex();

                // draw bar
                var bar = me.registerElement(c.paper.rect(d.x, d.y, d.width, d.height).attr({
                    'stroke': stroke,
                    'fill': fill
                }).data('strokeCol', stroke), barv.name);
                if (me.theme().barChart.barAttrs) {
                    bar.attr(me.theme().barChart.barAttrs);
                }

                if (lpos.show_val) {
                    me.registerLabel(me.label(lpos.val_x, lpos.top, formatValue(barv.value, true),{
                        // w: 40,
                        align: lpos.val_align,
                        cl: 'value' + lpos.lblClass
                    }), barv.name);
                }

                if (lpos.show_lbl) {
                    me.registerLabel(me.label(lpos.lbl_x , lpos.top, barv.name, {
                        w: Math.min(me.labelWidth(barv.name, 'series')+20, MAX_LABEL_WIDTH),
                        align: lpos.lbl_align,
                        valign: 'middle',
                        cl: 'series' + lpos.lblClass
                    }), barv.name, me.axes().labels, barv.row);
                }

                c.lastBarY = Math.max(c.lastBarY, d.y + d.height);
                if (s == barvalues.length-1) complete();
                s++;
            }

            var barGroups = _.groupBy(barvalues, function(bar, i) {
                return Math.floor(i/10);
            });
            var s = 0;
            _.each(barGroups, function(bars) {
                // defer rendering if we got plenty of bars
                // to prevent UI blocking
                _.defer(function() {
                    _.each(bars, render);
                });
            });

            // complete is called as soon all bars are rendered
            function complete() {
                if (me.__domain[0] < 0) {
                    var x = c.lpad + c.zero ;
                    // add y-axis
                    me.__yaxis = me.path('M' + [x, c.tpad] + 'V' + c.lastBarY, 'axis')
                        .attr(me.theme().yAxis);
                }
                // enable mouse events
                el.mousemove(_.bind(me.onMouseMove, me));
                me.renderingComplete();
            }
        },

        getBarValues: function(sortBars, reverse) {
            var me = this,
                values = [],
                filter = me.__lastRow,
                labels = me.axes(true).labels,
                column = me.getBarColumn(filter),
                fmt = me.chart().columnFormatter(labels);

            column.each(function(val, i) {
                values.push({
                    name: fmt(labels.val(i)),
                    value: val,
                    row: i
                });
            });
            if (sortBars) values.sort(function(a,b) { return b.value - a.value; });
            if (reverse) values.reverse();
            return values;
        },

        getBarColumn: function() {
            var me = this,
                filter = me.__lastRow;
            if (_.isUndefined(filter)) throw 'filter must not be undefined';
            return me.axes(true).bars[filter];
        },

        update: function(row) {
            var me = this,
                formatValue = me.chart().columnFormatter(me.getBarColumn());

            // update scales
            me.initDimensions();

            // update bar heights and labels
            _.each(me.getBarValues(me.get('sort-values', false)), function(bar, s) {
                _.each(me.__elements[bar.name], function(rect) {
                    var dim = me.barDimensions(bar, s, row);
                    dim.fill = me.getKeyColor(bar.name, bar.value, me.get('negative-color', false));
                    dim.stroke = chroma.color(dim.fill).darken(14).hex();
                    rect.animate(dim, me.theme().duration, me.theme().easing);<commit_msg>fixed sorting in bar chart<commit_after>
(function(){

    var MAX_LABEL_WIDTH = 160;

    dw.visualization.register('bar-chart', 'raphael-chart', {

        render: function(el) {
            this.setRoot(el);

            var me = this, row = 0,
                dataset = me.dataset,
                sortBars = me.get('sort-values', false),
                reverse = me.get('reverse-order'),
                useNegativeColor = me.get('negative-color', false);

            me.axesDef = me.axes();
            if (!me.axesDef) return;

            if (!_.isUndefined(me.get('selected-row'))) {
                row = me.get('selected-row', 0);
                row = row > me.axesDef.bars.length ? 0 : row;
            }
            me.__lastRow = row;

            var sliceColumns = _.map(me.axesDef.bars, function(i) { return dataset.column(i); }),
                filter = dw.utils.filter(dw.utils.columnNameColumn(sliceColumns), row),
                filterUI = filter.ui(me);

            if (filterUI) {
                $('#header').append(filterUI);
                filter.change(function(val, i) {
                    me.__lastRow = i;
                    me.update(i);
                });
            }

            var c = me.initCanvas({
                h: Math.max(
                    dw.utils.getMaxChartHeight(el)+5,
                    18 * 1.35 * me.getBarColumn().length + 5
                )
            });

            var
                chart_width = c.w - c.lpad - c.rpad,
                series_gap = 0.05, // pull from theme
                row_gap = 0.01,
                formatValue = me.chart().columnFormatter(me.getBarColumn()),
                barvalues = me.getBarValues(sortBars, reverse);

            me.init();
            me.initDimensions();

            $('.tooltip').hide();

            c.lastBarY = 0;

            function render(barv) {
                var d = me.barDimensions(barv, s, row),
                    lpos = me.labelPosition(barv, s, row),
                    fill = me.getKeyColor(barv.name, barv.value, useNegativeColor),
                    stroke = chroma.color(fill).darken(14).hex();

                // draw bar
                var bar = me.registerElement(c.paper.rect(d.x, d.y, d.width, d.height).attr({
                    'stroke': stroke,
                    'fill': fill
                }).data('strokeCol', stroke), barv.name);
                if (me.theme().barChart.barAttrs) {
                    bar.attr(me.theme().barChart.barAttrs);
                }

                if (lpos.show_val) {
                    me.registerLabel(me.label(lpos.val_x, lpos.top, formatValue(barv.value, true),{
                        // w: 40,
                        align: lpos.val_align,
                        cl: 'value' + lpos.lblClass
                    }), barv.name);
                }

                if (lpos.show_lbl) {
                    me.registerLabel(me.label(lpos.lbl_x , lpos.top, barv.name, {
                        w: Math.min(me.labelWidth(barv.name, 'series')+20, MAX_LABEL_WIDTH),
                        align: lpos.lbl_align,
                        valign: 'middle',
                        cl: 'series' + lpos.lblClass
                    }), barv.name, me.axes().labels, barv.row);
                }

                c.lastBarY = Math.max(c.lastBarY, d.y + d.height);
                if (s == barvalues.length-1) complete();
                s++;
            }

            var barGroups = _.groupBy(barvalues, function(bar, i) {
                return Math.floor(i/10);
            });
            var s = 0;
            _.each(barGroups, function(bars) {
                // defer rendering if we got plenty of bars
                // to prevent UI blocking
                _.defer(function() {
                    _.each(bars, render);
                });
            });

            // complete is called as soon all bars are rendered
            function complete() {
                if (me.__domain[0] < 0) {
                    var x = c.lpad + c.zero ;
                    // add y-axis
                    me.__yaxis = me.path('M' + [x, c.tpad] + 'V' + c.lastBarY, 'axis')
                        .attr(me.theme().yAxis);
                }
                // enable mouse events
                el.mousemove(_.bind(me.onMouseMove, me));
                me.renderingComplete();
            }
        },

        getBarValues: function(sortBars, reverse) {
            var me = this,
                values = [],
                filter = me.__lastRow,
                labels = me.axes(true).labels,
                column = me.getBarColumn(filter),
                fmt = me.chart().columnFormatter(labels);

            column.each(function(val, i) {
                values.push({
                    name: fmt(labels.val(i)),
                    value: val,
                    row: i
                });
            });
            if (sortBars) values.sort(function(a,b) { return (isNaN(b.value) ? 0 : b.value) - (isNaN(a.value) ? 0 : a.value); });
            if (reverse) values.reverse();
            return values;
        },

        getBarColumn: function() {
            var me = this,
                filter = me.__lastRow;
            if (_.isUndefined(filter)) throw 'filter must not be undefined';
            return me.axes(true).bars[filter];
        },

        update: function(row) {
            var me = this,
                formatValue = me.chart().columnFormatter(me.getBarColumn());

            // update scales
            me.initDimensions();

            // update bar heights and labels
            _.each(me.getBarValues(me.get('sort-values', false)), function(bar, s) {
                _.each(me.__elements[bar.name], function(rect) {
                    var dim = me.barDimensions(bar, s, row);
                    dim.fill = me.getKeyColor(bar.name, bar.value, me.get('negative-color', false));
                    dim.stroke = chroma.color(dim.fill).darken(14).hex();
                    rect.animate(dim, me.theme().duration, me.theme().easing);
{'fraction_non_alphanumeric': 0.1298057048367094, 'fraction_numerical': 0.005787515502273667, 'mean_word_length': 1.206896551724138, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5038945', 'n_tokens_mistral': 3531, 'n_tokens_neox': 3253, 'n_words': 645}