Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sankey highlight entire link path #156

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions R/sankeyNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)

Expand Down
24 changes: 22 additions & 2 deletions inst/htmlwidgets/sankeyNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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; })
Expand Down