From 311dc0f18d2c742482c0e5a056ff7491b26afe46 Mon Sep 17 00:00:00 2001 From: pvictor Date: Fri, 17 Apr 2020 11:29:11 +0200 Subject: [PATCH] added add_event_marker() --- NAMESPACE | 1 + R/annotations.R | 40 ++++++++++++++++++- examples/add_event_marker.R | 23 +++++++++++ man/add_event_marker.Rd | 76 +++++++++++++++++++++++++++++++++++++ man/spark_box.Rd | 4 +- 5 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 examples/add_event_marker.R create mode 100644 man/add_event_marker.Rd diff --git a/NAMESPACE b/NAMESPACE index 9c7b22b..0dfeb78 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export("%>%") export(JS) export(add_event) +export(add_event_marker) export(add_hline) export(add_point) export(add_shade) diff --git a/R/annotations.R b/R/annotations.R index de76a0d..9a7802a 100644 --- a/R/annotations.R +++ b/R/annotations.R @@ -287,6 +287,44 @@ add_event <- function(ax, when, color = "#E41A1C", dash = 4, label = NULL, ...) } +#' Add an event marker to a chart +#' +#' @param when Vector of position to place the event. +#' @inheritParams add_point +#' +#' @return An \code{apexcharts} \code{htmlwidget} object. +#' @export +#' +#' @example examples/add_event_marker.R +add_event_marker <- function(ax, when, y, + size = 5, + color = "#000", + fill = "#FFF", + width = 2, + shape = "circle", + radius = 2, + label = NULL, ...) { + add_annotation( + ax = ax, + type_annotation = "points", + position = "front", + as_date = TRUE, + x = when, y = y, + marker = marker( + size = size, + fillColor = fill, + strokeColor = color, + strokeWidth = width, + shape = shape, + radius = radius + ), + label = label, + ... + ) +} + + + @@ -386,4 +424,4 @@ add_point <- function(ax, x, y, label = label, ... ) -} \ No newline at end of file +} diff --git a/examples/add_event_marker.R b/examples/add_event_marker.R new file mode 100644 index 0000000..1a1363a --- /dev/null +++ b/examples/add_event_marker.R @@ -0,0 +1,23 @@ +library(apexcharter) +data("consumption") + +# add a marker +apex(consumption, aes(date, value, group = type), "spline") %>% + add_event_marker(when = "2020-01-22", y = 1805) + +# with a label +apex(consumption, aes(date, value, group = type), "spline") %>% + add_event_marker(when = "2020-01-22", y = 1805, label = "Consumption peak") + +# add several markers +apex(consumption, aes(date, value, group = type), "spline") %>% + add_event_marker( + when = c("2020-01-02", "2020-01-06", "2020-01-13", + "2020-01-22", "2020-01-28", "2020-02-06", + "2020-02-13", "2020-02-19", "2020-02-27"), + y = c(1545, 1659, 1614, + 1805, 1637, 1636, + 1597, 1547, 1631), + size = 10, + color = "firebrick" + ) diff --git a/man/add_event_marker.Rd b/man/add_event_marker.Rd new file mode 100644 index 0000000..e37d2ff --- /dev/null +++ b/man/add_event_marker.Rd @@ -0,0 +1,76 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/annotations.R +\name{add_event_marker} +\alias{add_event_marker} +\title{Add an event marker to a chart} +\usage{ +add_event_marker( + ax, + when, + y, + size = 5, + color = "#000", + fill = "#FFF", + width = 2, + shape = "circle", + radius = 2, + label = NULL, + ... +) +} +\arguments{ +\item{ax}{An \code{apexcharts} \code{htmlwidget} object.} + +\item{when}{Vector of position to place the event.} + +\item{y}{Coordinate(s) on the y-axis.} + +\item{size}{Size of the marker.} + +\item{color}{Stroke Color of the marker point.} + +\item{fill}{Fill Color of the marker point.} + +\item{width}{Stroke Size of the marker point.} + +\item{shape}{Shape of the marker: \code{"circle"} or \code{"square"}.} + +\item{radius}{Radius of the marker (applies to square shape).} + +\item{label}{Add a label to the shade, use a \code{character} +or see \code{\link{label}} for more controls.} + +\item{...}{Additional arguments, see +\url{https://apexcharts.com/docs/options/annotations/} for possible options.} +} +\value{ +An \code{apexcharts} \code{htmlwidget} object. +} +\description{ +Add an event marker to a chart +} +\examples{ +library(apexcharter) +data("consumption") + +# add a marker +apex(consumption, aes(date, value, group = type), "spline") \%>\% + add_event_marker(when = "2020-01-22", y = 1805) + +# with a label +apex(consumption, aes(date, value, group = type), "spline") \%>\% + add_event_marker(when = "2020-01-22", y = 1805, label = "Consumption peak") + +# add several markers +apex(consumption, aes(date, value, group = type), "spline") \%>\% + add_event_marker( + when = c("2020-01-02", "2020-01-06", "2020-01-13", + "2020-01-22", "2020-01-28", "2020-02-06", + "2020-02-13", "2020-02-19", "2020-02-27"), + y = c(1545, 1659, 1614, + 1805, 1637, 1636, + 1597, 1547, 1631), + size = 10, + color = "firebrick" + ) +} diff --git a/man/spark_box.Rd b/man/spark_box.Rd index 785d9f6..07a19f7 100644 --- a/man/spark_box.Rd +++ b/man/spark_box.Rd @@ -10,7 +10,7 @@ spark_box( subtitle = NULL, color = "#2E93fA", background = "#FFF", - type = c("area", "line", "spline"), + type = c("area", "line", "spline", "column"), synchronize = NULL, title_style = NULL, subtitle_style = NULL, @@ -32,7 +32,7 @@ first is mapped to x-axis, second to y-axis.} \item{background}{Background color of the box.} \item{type}{Type of chart, currently type supported are : -\code{"area"} (default), \code{"line"}, \code{"spline"}.} +\code{"area"} (default), \code{"line"}, \code{"spline"}, \code{"column"}.} \item{synchronize}{Give a common id to charts to synchronize them (tooltip and zoom).}