From 6e1c24e9b11d964f2078bbf745938232b8bc3e8b Mon Sep 17 00:00:00 2001 From: nredell Date: Fri, 14 Oct 2016 16:07:38 -0700 Subject: [PATCH 1/4] Highlight entire link path These changes add an optional "LinkName" argument which will support highlighting an individual variable's entire path through the Sankey diagram. --- R/sankeyNetwork.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/sankeyNetwork.R b/R/sankeyNetwork.R index 839c5d4f..658b9af5 100644 --- a/R/sankeyNetwork.R +++ b/R/sankeyNetwork.R @@ -73,8 +73,8 @@ #' @export sankeyNetwork <- function(Links, Nodes, Source, Target, Value, - NodeID, NodeGroup = NodeID, LinkGroup = NULL, units = "", - colourScale = JS("d3.scale.category20()"), fontSize = 7, + NodeID, NodeGroup = NodeID, LinkGroup = NULL, LinkName = NULL, + units = "", colourScale = JS("d3.scale.category20()"), fontSize = 7, fontFamily = NULL, nodeWidth = 15, nodePadding = 10, margin = NULL, height = NULL, width = NULL, iterations = 32, sinksRight = TRUE) { @@ -125,6 +125,11 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value, if (is.character(LinkGroup)) { LinksDF$group <- Links[, LinkGroup] } + + # specify an optional name for an individual link + if (is.character(LinkName)) { + LinksDF$linkName <- Links[, LinkName] + } margin <- margin_handler(margin) From 5fd07f10d3a8fd3d7914cebaf22fc229f511fe62 Mon Sep 17 00:00:00 2001 From: nredell Date: Fri, 14 Oct 2016 21:50:48 -0700 Subject: [PATCH 2/4] Highlight entire link path on mouseover These changes allow someone to see a given variable's path the the entire diagram. A subtitle that contains the link/variable name was added to the tooltip. --- inst/htmlwidgets/sankeyNetwork.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/inst/htmlwidgets/sankeyNetwork.js b/inst/htmlwidgets/sankeyNetwork.js index 6b64ed83..36770d66 100644 --- a/inst/htmlwidgets/sankeyNetwork.js +++ b/inst/htmlwidgets/sankeyNetwork.js @@ -131,13 +131,26 @@ HTMLWidgets.widget({ .style("stroke", color_link) .style("stroke-opacity", opacity_link) .sort(function(a, b) { return b.dy - a.dy; }) + .attr("link-name", function(d){ + return d.linkName; + }) .on("mouseover", function(d) { - d3.select(this) + if (d.linkName) { + ds.selectAll("[link-name=" + d.linkName + "]") .style("stroke-opacity", function(d){return opacity_link(d) + 0.3}); + } else { + d3.select(this) + .style("stroke-opacity", function(d){return opacity_link(d) + 0.3}); + } }) .on("mouseout", function(d) { + if (d.linkName) { + ds.selectAll("[link-name=" + d.linkName + "]") + .style("stroke-opacity", function(d){return opacity_link(d) + 0.3}); + } else { d3.select(this) .style("stroke-opacity", opacity_link); + } }); // add backwards class to cycles @@ -170,7 +183,7 @@ HTMLWidgets.widget({ // note: u2192 is right-arrow link.append("title") .text(function(d) { return d.source.name + " \u2192 " + d.target.name + - "\n" + format(d.value) + " " + options.units; }); + "\n" + format(d.value) + " " + options.units + " (" + d.linkName + ")"; }); node.append("rect") .attr("height", function(d) { return d.dy; }) From a5dd4fd9fdf22023d20601cdc0f94c0795842f81 Mon Sep 17 00:00:00 2001 From: nredell Date: Fri, 14 Oct 2016 22:38:48 -0700 Subject: [PATCH 3/4] Fixed typos on link highlight on mouseover Changed 'ds.' typos to .d3.' Changed the mouseout on link highlight to return the link to normal opacity. --- inst/htmlwidgets/sankeyNetwork.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/inst/htmlwidgets/sankeyNetwork.js b/inst/htmlwidgets/sankeyNetwork.js index 36770d66..5f040e17 100644 --- a/inst/htmlwidgets/sankeyNetwork.js +++ b/inst/htmlwidgets/sankeyNetwork.js @@ -136,17 +136,17 @@ HTMLWidgets.widget({ }) .on("mouseover", function(d) { if (d.linkName) { - ds.selectAll("[link-name=" + d.linkName + "]") + d3.selectAll("[link-name=" + d.linkName + "]") .style("stroke-opacity", function(d){return opacity_link(d) + 0.3}); } else { - d3.select(this) - .style("stroke-opacity", function(d){return opacity_link(d) + 0.3}); + d3.select(this) + .style("stroke-opacity", function(d){return opacity_link(d) + 0.3}); } }) .on("mouseout", function(d) { if (d.linkName) { - ds.selectAll("[link-name=" + d.linkName + "]") - .style("stroke-opacity", function(d){return opacity_link(d) + 0.3}); + d3.selectAll("[link-name=" + d.linkName + "]") + .style("stroke-opacity", opacity_link); } else { d3.select(this) .style("stroke-opacity", opacity_link); From 2c4e88e5c2b0ce7a6a55ffb2f70c481bfa49ee7a Mon Sep 17 00:00:00 2001 From: nredell Date: Fri, 14 Oct 2016 22:57:26 -0700 Subject: [PATCH 4/4] Remove LinkName from tooltip when no R argument Final commit for this functionality...the LinkName appears in parentheses when the argument is specified and is absent when it's not. --- inst/htmlwidgets/sankeyNetwork.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/inst/htmlwidgets/sankeyNetwork.js b/inst/htmlwidgets/sankeyNetwork.js index 5f040e17..ebf7bf7e 100644 --- a/inst/htmlwidgets/sankeyNetwork.js +++ b/inst/htmlwidgets/sankeyNetwork.js @@ -182,8 +182,15 @@ HTMLWidgets.widget({ }); // note: u2192 is right-arrow link.append("title") - .text(function(d) { return d.source.name + " \u2192 " + d.target.name + - "\n" + format(d.value) + " " + options.units + " (" + d.linkName + ")"; }); + .text(function(d) { + if (d.linkName) { + return d.source.name + " \u2192 " + d.target.name + + "\n" + format(d.value) + " " + options.units + " (" + d.linkName + ")"; + } else { + return d.source.name + " \u2192 " + d.target.name + + "\n" + format(d.value) + " " + options.units; + } + }); node.append("rect") .attr("height", function(d) { return d.dy; })