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

hyperlinks in group arcs #230

Open
wants to merge 2 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
101 changes: 61 additions & 40 deletions R/chordNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' \code{NULL} then height is automatically determined based on context)
#' @param width numeric width for the network graph's frame area in pixels (if
#' \code{NULL} then width is automatically determined based on context)
#' @param initialOpacity specify the opacity before the user mouses over
#' @param initialOpacity specify the opacity before the user mouses over
#' the link
#' @param colourScale specify the hexadecimal colours in which to display
#' the different categories. If there are fewer colours than categories,
Expand All @@ -21,31 +21,48 @@
#' The default is `0` which means no ticks will be drawn.
#' @param labelDistance integer distance in pixels (px) between
#' text labels and outer radius. The default is `30`.
#'
#' @param groupLinks Character vector of filenames to hyperlink to when a group
#' arc is clicked.
#' @param groupLinksWindow Where to open a linked page. Defaults to
#' \code{groupLinksWindow = "_self"} and the links are opened in the
#' current browser window. Using \code{groupLinksWindow = "_blank"}
#' would cause a new browser (or tab) to be opened.
#'
#' @examples
#' \dontrun{
#' #### Data about hair colour preferences, from
#' #### Data about hair colour preferences, from
#' ## https://github.com/mbostock/d3/wiki/Chord-Layout
#'
#'
#' hairColourData <- matrix(c(11975, 1951, 8010, 1013,
#' 5871, 10048, 16145, 990,
#' 8916, 2060, 8090, 940,
#' 2868, 6171, 8045, 6907),
#' nrow = 4)
#'
#' chordNetwork(Data = hairColourData,
#' width = 500,
#'
#' chordNetwork(Data = hairColourData,
#' width = 500,
#' height = 500,
#' colourScale = c("#000000",
#' "#FFDD89",
#' "#957244",
#' colourScale = c("#000000",
#' "#FFDD89",
#' "#957244",
#' "#F26223"),
#' labels = c("red", "brown", "blond", "gray"))
#'
#'
#'
#'chordNetwork(Data = hairColourData,
#' width = 500,
#' height = 500,
#' colourScale = c("#000000",
#' "#FFDD89",
#' "#957244",
#' "#F26223"),
#' labels = c("red", "brown", "blond", "gray"),
#' groupLinks=rep("https://CRAN.R-project.org/package=networkD3", 4),
#' groupLinksWindow="_self")

#' }
#'
#' @source
#' @source
#'
#' Mike Bostock: \url{https://github.com/mbostock/d3/wiki/Chord-Layout}.
#'
Expand All @@ -56,33 +73,35 @@ chordNetwork <- function(Data,
width = 500,
initialOpacity = 0.8,
useTicks = 0,
colourScale = c("#1f77b4",
"#aec7e8",
"#ff7f0e",
"#ffbb78",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5",
"#8c564b",
"#c49c94",
"#e377c2",
"#f7b6d2",
"#7f7f7f",
"#c7c7c7",
"#bcbd22",
"#dbdb8d",
"#17becf",
colourScale = c("#1f77b4",
"#aec7e8",
"#ff7f0e",
"#ffbb78",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5",
"#8c564b",
"#c49c94",
"#e377c2",
"#f7b6d2",
"#7f7f7f",
"#c7c7c7",
"#bcbd22",
"#dbdb8d",
"#17becf",
"#9edae5"),
padding = 0.1,
fontSize = 14,
fontFamily = "sans-serif",
labels = c(),
labelDistance = 30)
labelDistance = 30,
groupLinks = FALSE,
groupLinksWindow = "_blank")

{
{
options <- list(
width = width,
height = height,
Expand All @@ -93,14 +112,16 @@ chordNetwork <- function(Data,
font_size = fontSize,
font_family = fontFamily,
labels = labels,
label_distance = labelDistance
label_distance = labelDistance,
group_links = groupLinks,
group_links_window = groupLinksWindow
)

if (!is.matrix(Data) && !is.data.frame(Data))
{
stop("Data must be of type matrix or data frame")
}

if (nrow(Data) != ncol(Data))
{
stop(paste("Data must have the same number of rows and columns; given ",
Expand All @@ -110,16 +131,16 @@ chordNetwork <- function(Data,
"columns",
sep = " "))
}

if(length(labels)!=0 && length(labels)!=ncol(Data)){
stop(paste("Length of labels vector should be the same as the number of rows"))
}

if (is.data.frame(Data))
{
Data = data.matrix(Data)
}

# create widget
htmlwidgets::createWidget(
name = "chordNetwork",
Expand All @@ -138,7 +159,7 @@ chordNetwork <- function(Data,
#' @rdname networkD3-shiny
#' @export
chordNetworkOutput <- function(outputId, width = "100%", height = "500px") {
shinyWidgetOutput(outputId, "chordNetwork", width, height,
shinyWidgetOutput(outputId, "chordNetwork", width, height,
package = "networkD3")
}

Expand Down
14 changes: 14 additions & 0 deletions inst/htmlwidgets/chordNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ HTMLWidgets.widget({
.attr("d", d3.arc().innerRadius(innerRadius).outerRadius(outerRadius))
.on("mouseover", fade(.1))
.on("mouseout", fade(1));

if(x.options.group_links){
s.selectAll("g")
.on("click", function(d){
window.open(x.options.group_links[d.index], name=x.options.group_links_window);
})
.on("mouseover", function(d){
d3.select(this).style("cursor", "pointer");
})
.on("mouseout", function(d){
d3.select(this).style("cursor", "default");
});
}


if(x.options.labels) {
// Forumulas taken from http://sdk.gooddata.com/gooddata-js/example/chord-chart-to-analyze-sales/
Expand Down