Skip to content

Commit

Permalink
Merge pull request #16 from bragil-massoud/custom-y-order
Browse files Browse the repository at this point in the history
Added custom yOrderComparator function for vertical ordering of nodes
  • Loading branch information
fbreitwieser authored Mar 14, 2018
2 parents 8672cba + 6304a19 commit 760c671
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
6 changes: 4 additions & 2 deletions R/sankeyNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value,
height = NULL, width = NULL, iterations = 32, zoom = FALSE, align = "justify",
showNodeValues = TRUE, linkType = "bezier", curvature = .5, linkColor = "#A0A0A0",
nodeLabelMargin = 2, linkOpacity = .5, linkGradient = FALSE, nodeShadow = FALSE,
scaleNodeBreadthsByString = FALSE, xScalingFactor = 1)
scaleNodeBreadthsByString = FALSE, xScalingFactor = 1,
yOrderComparator = NULL)
{
# Check if data is zero indexed
check_zero(Links[, Source], Links[, Target])
Expand Down Expand Up @@ -215,7 +216,8 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value,
showNodeValues = showNodeValues, align = align, xAxisDomain = xAxisDomain,
title = title, nodeLabelMargin = nodeLabelMargin,
linkColor = linkColor, linkOpacity = linkOpacity, linkGradient = linkGradient, nodeShadow = nodeShadow,
scaleNodeBreadthsByString = scaleNodeBreadthsByString, xScalingFactor = xScalingFactor)
scaleNodeBreadthsByString = scaleNodeBreadthsByString, xScalingFactor = xScalingFactor,
yOrderComparator = yOrderComparator)

# create widget
htmlwidgets::createWidget(name = "sankeyNetwork", x = list(links = LinksDF,
Expand Down
23 changes: 14 additions & 9 deletions inst/htmlwidgets/lib/d3-sankey/src/sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ d3.sankey = function() {
nodeCornerRadius = 0,
nodes = [],
links = [];
yOrderComparator = function ascendingDepth(a, b) {
if (orderByPath) {
return ( a.path < b.path ? -1 : (a.path > b.path ? 1 : 0 ));
} else {
return a.y - b.y;
}
}

sankey.nodeWidth = function(_) {
if (!arguments.length) return nodeWidth;
Expand Down Expand Up @@ -103,6 +110,12 @@ d3.sankey = function() {
computeLinkDepths();
return sankey;
};

sankey.yOrderComparator = function(_) {
if (!arguments.length) return yOrderComparator;
yOrderComparator = _;
return sankey;
};

// SVG path data generator, to be used as "d" attribute on "path" element selection.
sankey.link = function() {
Expand Down Expand Up @@ -556,7 +569,7 @@ d3.sankey = function() {
i;

// Push any overlapping nodes down.
nodes.sort(ascendingDepth);
nodes.sort(yOrderComparator);
for (i = 0; i < n; ++i) {
node = nodes[i];
dy = y0 - node.y;
Expand All @@ -579,14 +592,6 @@ d3.sankey = function() {
}
});
}

function ascendingDepth(a, b) {
if (orderByPath) {
return ( a.path < b.path ? -1 : (a.path > b.path ? 1 : 0 ));
} else {
return a.y - b.y;
}
}
}

// Compute y-offset of the source endpoint (sy) and target endpoints (ty) of links,
Expand Down
10 changes: 7 additions & 3 deletions inst/htmlwidgets/sankeyNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ HTMLWidgets.widget({
}
return formated_num;
}

// create d3 sankey layout
sankey
.nodes(d3.values(nodes))
Expand All @@ -143,9 +143,13 @@ HTMLWidgets.widget({
.curvature(options.curvature)
.orderByPath(options.orderByPath)
.showNodeValues(options.showNodeValues)
.nodeCornerRadius(options.nodeCornerRadius)
.layout(options.iterations);
.nodeCornerRadius(options.nodeCornerRadius);

if(options.yOrderComparator) {
sankey = sankey.yOrderComparator(eval(options.yOrderComparator));
}

sankey.layout(options.iterations);

var max_posX = d3.max(sankey.nodes(), function(d) { return d.posX; });
sankey.nodes().forEach(function(node) { node.x = node.x * options.xScalingFactor; });
Expand Down
5 changes: 4 additions & 1 deletion man/sankeyNetwork.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 760c671

Please sign in to comment.