如何按正响应的总和对 Highcharts 中的堆叠条形图进行排序?

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

谁能看看我的条形图,让我知道如何按总正面响应率(高于 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
    });

要对任何数组进行排序,您可以使用此函数 sort

function sort(arr, dir) {
  for (var i = 0; i < arr.length-1; i++) {
    for (var j = 0; j < arr.length-i-1; j++) {
      if ((arr[j+1] > arr[j] && dir == "desc") || (arr[j+1] < arr[j] && dir == "asc")) {
        var tmp = arr[j+1];
        arr[j+1] = arr[j];
        arr[j] = tmp;
      }
    }
  }
}

var testArr = [6, 34, 22, 12, 3, 3, 4];

sort(testArr, "asc");
console.log(testArr);

sort(testArr, "desc");
console.log(testArr);

您可以按升序对部门进行排序,然后根据结果创建新数据。

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

演示:https://jsfiddle.net/BlackLabel/cdun0mx3/