Search code examples
chart.jsstackedbarseries

Chart.js stacked bar ordering depending on value


I have a stacked bar but I want to order the bars based on the summed value of my two datasets. Does any one have any idea how to do this using chart.js functionality?

See here

data: {
        labels: stores,
        datasets: [
        {
            label: 'Rev',
            data: dataPack1,
                        backgroundColor: "rgba(55, 160, 225, 0.7)",
                        hoverBackgroundColor: "rgba(55, 160, 225, 0.7)",
                        hoverBorderWidth: 2,
                        hoverBorderColor: 'lightgrey'
        },
        {
            label: 'Tax1',
            data: dataPack2,
                        backgroundColor: "rgba(225, 58, 55, 0.7)",
                        hoverBackgroundColor: "rgba(225, 58, 55, 0.7)",
                        hoverBorderWidth: 2,
                        hoverBorderColor: 'lightgrey'
        },
        ]
    },

Solution

  • If you have a scenario where multiple stacked bars have the same sum total, the other answer will duplicate the labels.

    This will not duplicate the labels:

            Chart.plugins.register({                                                                                                                                                             
            datasets: [],                                                                                                                                                                    
            getData(labels, datasets) {                                                                                                                                                      
                const sum = [];                                                                                                                                                              
                for (i = 0; i < datasets[0].length; i++) {                                                                                                                                   
                    sum.push({                                                                                                                                                               
                        label: labels[i],                                                                                                                                                    
                        data: datasets.map(e => e[i]),                                                                                                                                       
                            get sum() { // ES6 - getter                                                                                                                                      
                                return this.data.reduce((a, b) => a + b);                                                                                                                    
                            }                                                                                                                                                                
                    });                                                                                                                                                                      
                }                                                                                                                                                                            
                return sum;                                                                                                                                                                  
            },                                                                                                                                                                               
            beforeInit(chart) {                                                                                                                                                              
                chart.data.datasets.forEach((dataset, datasetIndex) => {                                                                                                                     
                    this.datasets.push(dataset.data);                                                                                                                                        
                });                                                                                                                                                                          
                const data_store = this.getData(chart.data.labels, this.datasets).sort((a,b) => b.sum - a.sum);                                                                              
                                                                                                                                                                                             
                data_store.forEach((d,i) => {                                                                                                                                                
                    chart.data.labels[i] = d.label;                                                                                                                                          
                    d.data.forEach((v, vi) => {                                                                                                                                              
                        chart.data.datasets[vi].data[i] = v;                                                                                                                                 
                    });                                                                                                                                                                      
                });                                                                                                                                                                          
            }                                                                                                                                                                                
        });