diff --git a/DESCRIPTION b/DESCRIPTION index 0ad0d2f..f0208f6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 Maintainer: Bob Rudis Description: A streamgraph (or "stream graph") is a type of stacked area graph @@ -30,4 +30,4 @@ Imports: tidyr, dplyr VignetteBuilder: knitr -RoxygenNote: 6.1.1 +RoxygenNote: 7.1.1 diff --git a/NAMESPACE b/NAMESPACE index 8fbd9f9..88578b7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/streamgraph.R b/R/streamgraph.R index 4a0790d..35a4794 100644 --- a/R/streamgraph.R +++ b/R/streamgraph.R @@ -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. @@ -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 @@ -54,6 +58,8 @@ streamgraph <- function(data, key, value, date, + xaxislab = NULL, + yaxislab = NULL, width=NULL, height=NULL, offset="silhouette", interpolate="cardinal", @@ -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" @@ -153,6 +161,11 @@ streamgraph <- function(data, } + + # add px to tooltip font size + tooltipfs <- paste0(tooltipfs, "px") + + params = list( data=data, markers=NULL, @@ -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, @@ -212,7 +229,24 @@ streamgraph_html <- function(id, style, class, width, height, ...) { HTML(sprintf("
", 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('

%s

', header) + if(!is.null(header)) + htmlwidgets::prependContent(sg, htmltools::HTML(header)) +} diff --git a/inst/htmlwidgets/streamgraph.js b/inst/htmlwidgets/streamgraph.js index 21b015a..c5136fb 100644 --- a/inst/htmlwidgets/streamgraph.js +++ b/inst/htmlwidgets/streamgraph.js @@ -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); }) @@ -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; diff --git a/man/sg_header.Rd b/man/sg_header.Rd new file mode 100644 index 0000000..59592eb --- /dev/null +++ b/man/sg_header.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/streamgraph.R +\name{sg_header} +\alias{sg_header} +\title{Add labels to the streamgraph} +\usage{ +sg_header(sg, header = NULL, as_html = FALSE) +} +\arguments{ +\item{sg}{streamgraph object} + +\item{header}{Title/header (Note, this is a different implementation of \code{sg_title()}, returning an sg object)} + +\item{as_html}{Whether to interpret \code{title}, \code{xaxis_label}, and \code{yaxis_label} arguments as HTML code. Default is FALSE} +} +\value{ +sg object +} +\description{ +Add labels to the streamgraph +} +\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. +} diff --git a/man/streamgraph.Rd b/man/streamgraph.Rd index f2e6c4d..22d79d1 100644 --- a/man/streamgraph.Rd +++ b/man/streamgraph.Rd @@ -4,11 +4,29 @@ \alias{streamgraph} \title{Create a new streamgraph} \usage{ -streamgraph(data, key, value, date, width = NULL, height = NULL, - offset = "silhouette", interpolate = "cardinal", - interactive = TRUE, scale = "date", top = 20, right = 40, - bottom = 30, left = 50, sort = TRUE, complete = TRUE, - order = c("compat", "asis", "inside-out", "reverse")) +streamgraph( + data, + key, + value, + date, + xaxislab = NULL, + yaxislab = NULL, + width = NULL, + height = NULL, + offset = "silhouette", + interpolate = "cardinal", + interactive = TRUE, + scale = "date", + top = 20, + right = 40, + bottom = 30, + left = 50, + sort = TRUE, + complete = TRUE, + order = c("compat", "asis", "inside-out", "reverse"), + tooltipfs = 20, + round = 10 +) } \arguments{ \item{data}{data frame} @@ -19,6 +37,10 @@ streamgraph(data, key, value, date, width = NULL, height = NULL, \item{date}{bare or quoted name of the date column (defaults to \code{date})} +\item{xaxislab}{Text of x-axis label to be displayed} + +\item{yaxislab}{Test of y-axis label to be displayed} + \item{width}{Width in pixels (optional, defaults to automatic sizing)} \item{height}{Height in pixels (optional, defaults to automatic sizing)} @@ -51,6 +73,10 @@ The default is probably fine fore most uses, but can be one of \code{cardinal} ( \item{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.} + +\item{tooltipfs}{Tooltip font size} + +\item{round}{Number of decimal places to round to in the tooltip (defaults to \code{10})} } \value{ streamgraph object