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

Directed arrows for Sankey Network #283

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ Suggests:
tibble
Enhances: knitr, shiny
LazyData: true
RoxygenNote: 6.0.1
RoxygenNote: 7.1.1
7 changes: 5 additions & 2 deletions R/sankeyNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#' browser on the client so don't push it too high.
#' @param sinksRight boolean. If \code{TRUE}, the last nodes are moved to the
#' right border of the plot.
#' @param arrows logical value to enable directional link arrows.
#'
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -76,7 +77,8 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value,
NodeID, NodeGroup = NodeID, LinkGroup = NULL, units = "",
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory20);"), fontSize = 7,
fontFamily = NULL, nodeWidth = 15, nodePadding = 10, margin = NULL,
height = NULL, width = NULL, iterations = 32, sinksRight = TRUE)
height = NULL, width = NULL, iterations = 32, sinksRight = TRUE,
arrows=FALSE)
{
# Check if data is zero indexed
check_zero(Links[, Source], Links[, Target])
Expand Down Expand Up @@ -132,7 +134,8 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value,
options = list(NodeID = NodeID, NodeGroup = NodeGroup, LinkGroup = LinkGroup,
colourScale = colourScale, fontSize = fontSize, fontFamily = fontFamily,
nodeWidth = nodeWidth, nodePadding = nodePadding, units = units,
margin = margin, iterations = iterations, sinksRight = sinksRight)
margin = margin, iterations = iterations, sinksRight = sinksRight,
arrows = arrows)

# create widget
htmlwidgets::createWidget(name = "sankeyNetwork", x = list(links = LinksDF,
Expand Down
28 changes: 27 additions & 1 deletion inst/htmlwidgets/sankeyNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ HTMLWidgets.widget({
var path = sankey.link();

// draw links
var link = svg.selectAll(".link")
var link = svg.selectAll(".node")
.data(links)
.enter().append("path")
.attr("class", "link")
Expand All @@ -140,6 +140,8 @@ HTMLWidgets.widget({
.style("stroke-opacity", opacity_link);
});



// add backwards class to cycles
link.classed('backwards', function (d) { return d.target.x < d.source.x; });

Expand Down Expand Up @@ -173,6 +175,30 @@ HTMLWidgets.widget({
.html(function(d) { return "<pre>" + d.source.name + " \u2192 " + d.target.name +
"\n" + format(d.value) + " " + options.units + "</pre>"; });

if (options.arrows) {
link.style("marker-end", function(d) { return "url(#arrow-" + d.key + ")"; });

var linkColoursArr = d3.nest().key(function(d) { return d.colour; }).entries(links);

node.selectAll(".link")
.data(linkColoursArr)
.enter().append("marker")
.attr("id", function(d) { return "arrow-" + d.key; })
.attr("viewBox", "0, -5, 10, 10")
.attr("refX", 10)
.attr("refY", 0)
.attr("preserveAspectRatio", "xMaxYMin meet")
.attr("Width", sankey.nodeWidth())
.attr("markerHeight", 1)
.attr("orient", "auto-start-reverse")
//.attr("transform", "skewY(40)")
.style("fill", "context-fill")
//.style("fill", function(d) { return d.key; })
.style("opacity", 0.5)
.append("path")
.attr("d", "M0,-5 L10,0 L0,5");
}

node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
Expand Down
4 changes: 3 additions & 1 deletion man/MisLinks.Rd

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

6 changes: 4 additions & 2 deletions man/MisNodes.Rd

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

4 changes: 3 additions & 1 deletion man/SchoolsJournals.Rd

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

10 changes: 8 additions & 2 deletions man/as_treenetdf.data.frame.Rd

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

3 changes: 1 addition & 2 deletions man/as_treenetdf.list.Rd

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

21 changes: 15 additions & 6 deletions man/chordNetwork.Rd

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

22 changes: 17 additions & 5 deletions man/dendroNetwork.Rd

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

17 changes: 13 additions & 4 deletions man/diagonalNetwork.Rd

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

34 changes: 26 additions & 8 deletions man/forceNetwork.Rd

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

17 changes: 13 additions & 4 deletions man/radialNetwork.Rd

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

29 changes: 24 additions & 5 deletions man/sankeyNetwork.Rd

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

18 changes: 15 additions & 3 deletions man/simpleNetwork.Rd

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

16 changes: 13 additions & 3 deletions man/treeNetwork.Rd

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