Sort sankey chart nodes from low to high

1.2k Views Asked by At
const dataSeries = [{
        "name": "Containerburg"
      },
      {
        "name": "HTCont1"
      },
      {
        "name": "Kranstrom"
      },
      {
        "name": "Wago3"
      },
      {
        "name": "Wago5"
      },
      {
        "name": "Wago2"
      },
      {
        "name": "Heinrich Campus "
      },
      {
        "name": "SubCont1"
      },
      {
        "name": "Heinrich Campus"
      }
    ];

    const seriesLinks = [{
        "source": "Containerburg",
        "target": "HTCont1",
        "value": 20.874000000000024
      },
      {
        "source": "Kranstrom",
        "target": "Wago3",
        "value": 44.253999999999905
      },
      {
        "source": "Containerburg",
        "target": "Wago5",
        "value": 126.2489999999998
      },
      {
        "source": "Kranstrom",
        "target": "Wago2",
        "value": 151.63200000000006
      },
      {
        "source": "Heinrich Campus ",
        "target": "Kranstrom",
        "value": 195.88599999999997
      },
      {
        "source": "Containerburg",
        "target": "SubCont1",
        "value": 745.3199999999997
      },
      {
        "source": "Heinrich Campus",
        "target": "Containerburg",
        "value": 892.4429999999995
      }
    ];

option = {
  series: [
    {
      type: "sankey",
      data: dataSeries,
      links: seriesLinks,
    },
  ],
};

Apache ECharts automatically orders it from high value to low (SubCont1 < HTCont1) but I want to reverse the ordering so that HTCont1 is on top and SubCont1 is on the bottom.

This is a working example that can be pasted to https://echarts.apache.org/examples/en/editor.html or can be viewed here http://jsfiddle.net/y7emL1np/

2

There are 2 best solutions below

2
On BEST ANSWER

You can use the layoutIterations parameter to achieve this. Setting it to 0 (zero) disables all layout adjustments based on weight/value. http://jsfiddle.net/yj5u9htb/

    var option = {
      series: [{
        type: "sankey",
        data: dataSeries,
        links: seriesLinks,
        layoutIterations: 0
      }]
    };

    myChart.setOption(option);
0
On
series: [{
  // ...
  layoutIterations: 0,
  // ...
}]

You disabled layout optimization, now you need to change order by yourself :)

const seriesLinks = [{
  // ...
}].reverse() // <--- this

See a bit fixed example:

enter image description here