diff --git a/DESCRIPTION b/DESCRIPTION index da68243..e616467 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: streamgraph Type: Package Title: streamgraph is an htmlwidget for building streamgraph visualizations -Version: 0.5.1 -Date: 2015-02-15 +Version: 0.7 +Date: 2015-03-12 Author: Bob Rudis (@hrbrmstr) Maintainer: Bob Rudis Description: A streamgraph (or "stream graph") is a type of stacked area graph diff --git a/NAMESPACE b/NAMESPACE index 2bb5c30..cb03302 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,4 @@ -# Generated by roxygen2 (4.1.0.9000): do not edit by hand +# Generated by roxygen2 (4.1.0): do not edit by hand export("%>%") export(renderStreamgraph) diff --git a/R/streamgraph.R b/R/streamgraph.R index 4288700..449e360 100644 --- a/R/streamgraph.R +++ b/R/streamgraph.R @@ -427,12 +427,25 @@ sg_legend <- function(sg, show=FALSE, label="") { #' Add a vertical marker (with optional label) to streamgraph #' -#' @export -sg_add_marker <- function(sg, x, stroke_width=0.5, stroke="#7f7f7f", space=5, - y=0, label="", color="#7f7f7f", size=12) { +#' This is useful for marking/labeling notable events along the streams. +#' +#' @param x horizontal position +#' @param label text for the annotation +#' @param stroke_width line width +#' @param stroke line color +#' @param space space (in points) from the marker to place the label +#' @param y vertical position +#' @param color color of the label +#' @param size font size#' @export +#' @param anchor how to justify the label (one of \code{start} [left], +#' \code{middle} [center] or \code{end} [right]) +sg_add_marker <- function(sg, x, label="", stroke_width=0.5, stroke="#7f7f7f", space=5, + y=0, color="#7f7f7f", size=12, anchor="start") { + + if (inherits(x, "Date")) { x <- format(x, "%Y-%m-%d") } mark <- data.frame(x=x, y=y, label=label, color=color, stroke_width=stroke_width, stroke=stroke, - space=space, size=size, stringsAsFactors=FALSE) + space=space, size=size, anchor=anchor, stringsAsFactors=FALSE) if (is.null(sg$x$markers)) { sg$x$markers <- mark @@ -446,8 +459,19 @@ sg_add_marker <- function(sg, x, stroke_width=0.5, stroke="#7f7f7f", space=5, #' Add text annotation to streamgraph #' +#' Use this function to place text at any point on a streamgraph. This +#' is especially useful for non-interactive streamgraphs (i.e. to label +#' a particular stream). +#' +#' @param label text for the annotation +#' @param x horizontal position +#' @param y vertical position +#' @param color color of the label +#' @param size font size #' @export -sg_annotate <- function(sg, label, x, y, color="black", size="12") { +sg_annotate <- function(sg, label, x, y, color="black", size=12) { + + if (inherits(x, "Date")) { x <- format(x, "%Y-%m-%d") } ann <- data.frame(label=label, x=x, y=y, color=color, size=size, stringsAsFactors=FALSE) diff --git a/inst/htmlwidgets/streamgraph.js b/inst/htmlwidgets/streamgraph.js index 5d01073..a826edd 100644 --- a/inst/htmlwidgets/streamgraph.js +++ b/inst/htmlwidgets/streamgraph.js @@ -266,6 +266,15 @@ HTMLWidgets.widget({ if (params.annotations != null) { var ann = HTMLWidgets.dataframeToD3(params.annotations) ; + + ann.forEach(function(d) { + if (params.x_scale == "date") { + d.x = format.parse(d.x); + } else { + d.x = +d.x; + } + }); + svg.selectAll(".annotation") .data(ann) .enter().append("text") @@ -279,6 +288,15 @@ HTMLWidgets.widget({ if (params.markers != null) { var mrk = HTMLWidgets.dataframeToD3(params.markers) ; + + mrk.forEach(function(d) { + if (params.x_scale == "date") { + d.x = format.parse(d.x); + } else { + d.x = +d.x; + } + }); + dbg3 = mrk ; svg.selectAll(".marker") .data(mrk) @@ -294,11 +312,15 @@ HTMLWidgets.widget({ svg.selectAll(".markerlab") .data(mrk) .enter().append("text") - .attr("x", function(d) { return(x(d.x)+d.space) ; }) + .attr("x", function(d) { + if (d.anchor=="end") { d.space = -d.space; } + if (d.anchor=="middle") { d.space = 0; } + return(x(d.x)+d.space) ; + }) .attr("y", function(d) { return(y(d.y)) ; }) .attr("fill", function(d) { return(d.color) ; }) .style("font-size", function(d) { return(d.size+"px") ; }) - //.style("text-anchor", function(d) { return(d.align) ; }) + .style("text-anchor", function(d) { return(d.anchor) ; }) .text(function(d) { return(d.label) ;}) } diff --git a/man/renderStreamgraph.Rd b/man/renderStreamgraph.Rd index 758090b..66c1ca9 100644 --- a/man/renderStreamgraph.Rd +++ b/man/renderStreamgraph.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{renderStreamgraph} \alias{renderStreamgraph} diff --git a/man/sg_add_marker.Rd b/man/sg_add_marker.Rd index 55b51b0..dcf7559 100644 --- a/man/sg_add_marker.Rd +++ b/man/sg_add_marker.Rd @@ -1,13 +1,33 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{sg_add_marker} \alias{sg_add_marker} \title{Add a vertical marker (with optional label) to streamgraph} \usage{ -sg_add_marker(sg, x, stroke_width = 0.5, stroke = "#7f7f7f", space = 5, - y = 0, label = "", color = "#7f7f7f", size = 12) +sg_add_marker(sg, x, label = "", stroke_width = 0.5, stroke = "#7f7f7f", + space = 5, y = 0, color = "#7f7f7f", size = 12, anchor = "start") +} +\arguments{ +\item{x}{horizontal position} + +\item{label}{text for the annotation} + +\item{stroke_width}{line width} + +\item{stroke}{line color} + +\item{space}{space (in points) from the marker to place the label} + +\item{y}{vertical position} + +\item{color}{color of the label} + +\item{size}{font size#'} + +\item{anchor}{how to justify the label (one of \code{start} [left], +\code{middle} [center] or \code{end} [right])} } \description{ -Add a vertical marker (with optional label) to streamgraph +This is useful for marking/labeling notable events along the streams. } diff --git a/man/sg_annotate.Rd b/man/sg_annotate.Rd index b0918b1..57d7733 100644 --- a/man/sg_annotate.Rd +++ b/man/sg_annotate.Rd @@ -1,12 +1,25 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{sg_annotate} \alias{sg_annotate} \title{Add text annotation to streamgraph} \usage{ -sg_annotate(sg, label, x, y, color = "black", size = "12") +sg_annotate(sg, label, x, y, color = "black", size = 12) +} +\arguments{ +\item{label}{text for the annotation} + +\item{x}{horizontal position} + +\item{y}{vertical position} + +\item{color}{color of the label} + +\item{size}{font size} } \description{ -Add text annotation to streamgraph +Use this function to place text at any point on a streamgraph. This +is especially useful for non-interactive streamgraphs (i.e. to label +a particular stream). } diff --git a/man/sg_axis_x.Rd b/man/sg_axis_x.Rd index 8f8dbfc..9ec9bdc 100644 --- a/man/sg_axis_x.Rd +++ b/man/sg_axis_x.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{sg_axis_x} \alias{sg_axis_x} diff --git a/man/sg_axis_y.Rd b/man/sg_axis_y.Rd index dc75cf9..facf38e 100644 --- a/man/sg_axis_y.Rd +++ b/man/sg_axis_y.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{sg_axis_y} \alias{sg_axis_y} diff --git a/man/sg_colors.Rd b/man/sg_colors.Rd index 2478e9a..83e14a9 100644 --- a/man/sg_colors.Rd +++ b/man/sg_colors.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{sg_colors} \alias{sg_colors} diff --git a/man/sg_fill_brewer.Rd b/man/sg_fill_brewer.Rd index 1638a89..f12c5df 100644 --- a/man/sg_fill_brewer.Rd +++ b/man/sg_fill_brewer.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{sg_fill_brewer} \alias{sg_fill_brewer} diff --git a/man/sg_fill_manual.Rd b/man/sg_fill_manual.Rd index 72d33a4..f67b2e6 100644 --- a/man/sg_fill_manual.Rd +++ b/man/sg_fill_manual.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{sg_fill_manual} \alias{sg_fill_manual} diff --git a/man/sg_fill_tableau.Rd b/man/sg_fill_tableau.Rd index b3ad8b3..1036a82 100644 --- a/man/sg_fill_tableau.Rd +++ b/man/sg_fill_tableau.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{sg_fill_tableau} \alias{sg_fill_tableau} diff --git a/man/sg_legend.Rd b/man/sg_legend.Rd index ba49bcc..4507e86 100644 --- a/man/sg_legend.Rd +++ b/man/sg_legend.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{sg_legend} \alias{sg_legend} diff --git a/man/streamgraph-exports.Rd b/man/streamgraph-exports.Rd index fd4ef16..a0415ea 100644 --- a/man/streamgraph-exports.Rd +++ b/man/streamgraph-exports.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph-package.R \name{streamgraph-exports} \alias{\%>\%} diff --git a/man/streamgraph-package.Rd b/man/streamgraph-package.Rd index 040a4e9..0f3d5d3 100644 --- a/man/streamgraph-package.Rd +++ b/man/streamgraph-package.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph-package.R \docType{package} \name{streamgraph-package} diff --git a/man/streamgraph.Rd b/man/streamgraph.Rd index 6345bb8..5839448 100644 --- a/man/streamgraph.Rd +++ b/man/streamgraph.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{streamgraph} \alias{streamgraph} diff --git a/man/streamgraphOutput.Rd b/man/streamgraphOutput.Rd index e5d379e..df35622 100644 --- a/man/streamgraphOutput.Rd +++ b/man/streamgraphOutput.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.0.9000): do not edit by hand +% Generated by roxygen2 (4.1.0): do not edit by hand % Please edit documentation in R/streamgraph.R \name{streamgraphOutput} \alias{streamgraphOutput}