Search code examples
javascriptjqueryhighchartsstackedbarseries

How can I sort the stacked bar charts in Highcharts by the sum of positive response?


Can someone look at my bar chart and let me know how can I sort the stacks by the total positive response rate (sum of the top two green categories that are above 0%)?

https://jsfiddle.net/samwhite/tqLya8h1/1/

    let selection = departments.forEach((dat, d) => {
      data[dat]["104"].forEach((val, i) => {
        if (i<2) {
          options[i].data.push(val);
        } else {
          options[i].data.push(-val);
        }
      })
    });
    
    let chart = Highcharts.chart('container', {
      chart: {
        type: 'column',
      },
      series: options
    });

Solution

  • You can sort the departments in ascending order and later create new data by the result.

    departments.sort((a, b) => {
        return ((data[a]["104"][0] + data[a]["104"][1]) - (data[b]["104"][0] + data[b]["104"][1]))
    })
    

    Demo: https://jsfiddle.net/BlackLabel/cdun0mx3/