From 04b13ab276ab7cccc3bb7688aa89ce3d011af106 Mon Sep 17 00:00:00 2001 From: chris-stone Date: Fri, 21 Mar 2014 14:15:50 -0600 Subject: [PATCH] Source link sorting by angle Incoming source links to a node are sorted by the angle between the source and target nodes, rather than their difference in y. It produces cleaner diagrams, and has a customizable weighting factor to give more weight to the x difference between nodes. The default value of 3 seems to work well. --- sankey/sankey.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sankey/sankey.js b/sankey/sankey.js index c3bc59f..a5c5f1a 100644 --- a/sankey/sankey.js +++ b/sankey/sankey.js @@ -4,7 +4,8 @@ d3.sankey = function() { nodePadding = 8, size = [1, 1], nodes = [], - links = []; + links = [], + sourceDepthSortWeight = 3; sankey.nodeWidth = function(_) { if (!arguments.length) return nodeWidth; @@ -272,7 +273,7 @@ d3.sankey = function() { }); function ascendingSourceDepth(a, b) { - return a.source.y - b.source.y; + return -(Math.atan((a.target.y - a.source.y) / Math.pow(a.target.x - a.source.x, sourceDepthSortWeight)) - Math.atan((b.target.y - b.source.y) / Math.pow(b.target.x - b.source.x, sourceDepthSortWeight))); } function ascendingTargetDepth(a, b) {