D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
tomshanley
Full window
Github gist
Sankey column pre-sort
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> function constant(x) { return function(d) { return d[x]; }; } function getNodeSort (node, id) { return id(node) } ////////////////////////////////////////////////////// let nodes = [ {"id": 1, "c": 0, "d": "A"}, {"id": 2, "c": 1, "d": "A"}, {"id": 3, "c": 1, "d": "E"}, {"id": 4, "c": 3, "d": "AA"}, {"id": 5, "c": 4, "d": "A"} ] /*let s = function(node) { // return node.c // return node.d // return 1 return node.c < 2 ? 0 : 1 }*/ let s = "d" ////////////////////////////////////////////////////// let sortNodes = typeof s === 'function' ? s : constant(s) ////////////////////////////////////////////////////// let columnSet = new Set() let columnArray = [] nodes.forEach(function(node) { columnSet.add(getNodeSort(node, sortNodes)) }) console.log("------------------------------------------") console.log(columnSet) columnArray = [...columnSet].sort() nodes.forEach(function(node) { node.column1 = columnArray.indexOf(getNodeSort(node, sortNodes)) }) ////////////////////////////////////////////////////// console.log(columnArray) console.log(nodes) console.log("------------------------------------------") </script> </body>
https://d3js.org/d3.v4.min.js