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) diff --git a/inst/htmlwidgets/sankeyNetwork.js b/inst/htmlwidgets/sankeyNetwork.js index 6b64ed83..ebf7bf7e 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) { + if (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}); + } }) .on("mouseout", function(d) { + if (d.linkName) { + d3.selectAll("[link-name=" + d.linkName + "]") + .style("stroke-opacity", opacity_link); + } else { d3.select(this) .style("stroke-opacity", opacity_link); + } }); // add backwards class to cycles @@ -169,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; }); + .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; })