How to order a stacked column chart using a BreakBy

Comments

2 comments

  • Avatar
    Elliott Herz

    I also built a script that does the same thing! Here is another version:

     

     

    var sortType = 'DESC';

    widget.on('processresult', function(w, args) {

                    var columnTotals = []; //list to store total values

                    var categories = jQuery.extend(true, {},args.result.xAxis.categories); //save initial categories

                    var series = jQuery.extend(true, {},args.result.series); //save initial series data results

                   

                    //loop through the results and calculate the bar totals

                    for(var i=0; i<args.result.xAxis.categories.length; i++) {

                                    var total = 0;

                                    for(var j=0; j<args.result.series.length; j++) {

                                                    if(args.result.series[j].data[i].y !== null) {

                                                                    total += args.result.series[j].data[i].y

                                                    }

                                    }

                                    columnTotals.push(total);

                    }

                   

                    // temporary array holds objects with position and sort-value

                    var mapped = columnTotals.map(function(el, i) {

                                    return { index: i, value: el };

                    })

     

                    // sorting the mapped array containing the reduced values

                    mapped.sort(function(a, b) {

                                    if(sortType === 'ASC') {

                                                    return a.value - b.value;

                                    } else {

                                                    return b.value - a.value;

                                    }

                    });

     

                    // container for the resulting order

                    var result = mapped.map(function(el){

                                    return columnTotals[el.index];

                    });

                   

                    //update the series data/categories to reflect the sorted mapping

                    for(var i=0; i<args.result.xAxis.categories.length; i++) {

                                    for(var j=0; j<args.result.series.length; j++) {

                                                    args.result.series[j].data[i].y = series[j].data[mapped[i].index].y;

                                                   

                                                    if(args.result.series[j].data[i].selectionData === undefined) {

                                                                    args.result.series[j].data[i].selectionData = {};

                                                    }

                                                    args.result.series[j].data[i].selectionData[0] = categories[mapped[i].index];

                                                    args.result.series[j].data[i].selected = false;

                                    }

                                    args.result.xAxis.categories[i] = categories[mapped[i].index];

                    }

    });

    0
    Comment actions Permalink
  • Avatar
    Elliott Herz

    Here is a link to a plugin to give options to easily show bar totals, sort the categories, and sort the break bys.

    https://support.sisense.com/hc/en-us/community/posts/360020822513-Custom-Bar-Column-Chart-Plugin-Show-Totals-Sort-Categories-Sort-Breakbys-

    0
    Comment actions Permalink

Please sign in to leave a comment.