Skip to content
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
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: streamgraph
Type: Package
Title: streamgraph is an htmlwidget for building streamgraph visualizations
Version: 0.9.0
Date: 2019-05-02
Version: 0.9.1
Date: 2021-04-15
Author: Bob Rudis <bob@rud.is>
Maintainer: Bob Rudis <bob@rud.is>
Description: A streamgraph (or "stream graph") is a type of stacked area graph
Expand Down Expand Up @@ -30,4 +30,4 @@ Imports:
tidyr,
dplyr
VignetteBuilder: knitr
RoxygenNote: 6.1.1
RoxygenNote: 7.1.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(sg_colors)
export(sg_fill_brewer)
export(sg_fill_manual)
export(sg_fill_tableau)
export(sg_header)
export(sg_legend)
export(sg_title)
export(streamgraph)
Expand Down
36 changes: 35 additions & 1 deletion R/streamgraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#' @param key bare or quoted name of the category column (defaults to \code{key})
#' @param value bare or quoted name of the value column (defaults to \code{value})
#' @param date bare or quoted name of the date column (defaults to \code{date})
#' @param round Number of decimal places to round to in the tooltip (defaults to \code{10})
#' @param xaxislab Text of x-axis label to be displayed
#' @param yaxislab Test of y-axis label to be displayed
#' @param width Width in pixels (optional, defaults to automatic sizing)
#' @param height Height in pixels (optional, defaults to automatic sizing)
#' @param offset see d3's \href{https://github.com/mbostock/d3/wiki/Stack-Layout#offset}{offset layout} for more details.
Expand All @@ -33,6 +36,7 @@
#' @param order streamgraph ribbon order. "`compat`" to match the orignial package behavior,
#' "`asis`" to use the input order, "`inside-out`" to sort by index of maximum value,
#' then use balanced weighting, or "`reverse`" to reverse the input layer order.
#' @param tooltipfs Tooltip font size
#' @import htmlwidgets htmltools
#' @importFrom tidyr expand
#' @return streamgraph object
Expand All @@ -54,6 +58,8 @@ streamgraph <- function(data,
key,
value,
date,
xaxislab = NULL,
yaxislab = NULL,
width=NULL, height=NULL,
offset="silhouette",
interpolate="cardinal",
Expand All @@ -65,7 +71,9 @@ streamgraph <- function(data,
left=50,
sort=TRUE,
complete=TRUE,
order = c("compat", "asis", "inside-out", "reverse")) {
order = c("compat", "asis", "inside-out", "reverse"),
tooltipfs = 20,
round = 10) {

order <- match.arg(order, choices = c("compat", "asis", "inside-out", "reverse"))
if (order == "compat") order <- "none"
Expand Down Expand Up @@ -153,6 +161,11 @@ streamgraph <- function(data,

}


# add px to tooltip font size
tooltipfs <- paste0(tooltipfs, "px")


params = list(
data=data,
markers=NULL,
Expand All @@ -163,6 +176,10 @@ streamgraph <- function(data,
palette="Spectral",
text="black",
tooltip="black",
tooltipfs=tooltipfs,
round = round,
xaxislab = xaxislab,
yaxislab = yaxislab,
x_tick_interval=xti,
x_tick_units=xtu,
x_tick_format=xtf,
Expand Down Expand Up @@ -212,7 +229,24 @@ streamgraph_html <- function(id, style, class, width, height, ...) {
HTML(sprintf("<center><label style='padding-right:5px' for='%s-select'></label><select id='%s-select' style='visibility:hidden;'></select></center>", id, id))))
}

#' Add labels to the streamgraph
#'
#' @param sg streamgraph object
#' @param header Title/header (Note, this is a different implementation of \code{sg_title()}, returning an sg object)
#' @param as_html Whether to interpret \code{title}, \code{xaxis_label}, and \code{yaxis_label} arguments as HTML code. Default is FALSE
#'
#' @details
#' If \code{as_html = TRUE}, the given header will be passed to htmltools::HTML and interpreted as HTML code.
#' The default (\code{FALSE}) passes the header string as above but as bold, arial, paragraph text.
#'
#' @return sg object
#' @export
sg_header <- function(sg, header = NULL, as_html = FALSE){

if(!as_html) header <- sprintf('<b><p style="font-family:arial;">%s</p></b>', header)

if(!is.null(header))
htmlwidgets::prependContent(sg, htmltools::HTML(header))
}


17 changes: 16 additions & 1 deletion inst/htmlwidgets/streamgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ HTMLWidgets.widget({
.attr("stroke", strokecolor)
.attr("stroke-width", "0.5px");

tooltip.text(dd.key + ": " + d.value).attr("fill", params.tooltip);
tooltip.text(dd.key + ": " + d3.round(d.value, params.round))
.attr("fill", params.tooltip).
style("font-size", params.tooltipfs);

})

Expand Down Expand Up @@ -222,11 +224,24 @@ HTMLWidgets.widget({
.attr("fill", params.text)
.call(xAxis);

svg.append("text")
.attr("transform", "translate(" + (width / 2) + " ," + (height + margin.bottom) + ")")
.style("text-anchor", "middle")
.text(params.xaxislab);

svg.append("g")
.attr("class", "y axis")
.attr("fill", params.text)
.call(yAxis);

svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text(params.yaxislab);

function onselchange() {

var selected_value = d3.event.target.value;
Expand Down
25 changes: 25 additions & 0 deletions man/sg_header.Rd

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

36 changes: 31 additions & 5 deletions man/streamgraph.Rd

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