added add_point()

This commit is contained in:
pvictor 2020-04-04 13:05:19 +02:00
parent 92b2217f56
commit 8d751a485e
4 changed files with 245 additions and 0 deletions

View File

@ -4,6 +4,7 @@ export("%>%")
export(JS)
export(add_event)
export(add_hline)
export(add_point)
export(add_shade)
export(add_shade_weekend)
export(add_vline)

View File

@ -6,6 +6,9 @@ add_annotation <- function(ax, type_annotation = c("xaxis", "yaxis", "points"),
if (!is.null(config$label) && is.character(config$label)) {
config$label <- list(text = config$label)
}
if (!is.null(config$marker) && is.numeric(config$marker)) {
config$marker <- list(size = config$marker)
}
if (identical(type_annotation, "yaxis")) {
len <- length(config$y)
} else {
@ -135,6 +138,44 @@ label <- function(text = NULL,
}
#' Marker for annotations
#'
#' @param size Size of the marker.
#' @param fillColor Fill Color of the marker point.
#' @param strokeColor Stroke Color of the marker point.
#' @param strokeWidth Stroke Size of the marker point.
#' @param shape Shape of the marker: \code{"circle"} or \code{"square"}.
#' @param radius Radius of the marker (applies to square shape).
#' @param OffsetX Sets the left offset of the marker.
#' @param OffsetY Sets the top offset of the marker.
#' @param cssClass Additional CSS classes to append to the marker.
#'
#' @return A \code{list} that can be used in \code{\link{add_point}}.
#' @noRd
#'
marker <- function(size = NULL,
fillColor = NULL,
strokeColor = NULL,
strokeWidth = NULL,
shape = NULL,
radius = NULL,
OffsetX = NULL,
OffsetY = NULL,
cssClass = NULL) {
dropNulls(list(
size = size,
fillColor = fillColor,
strokeColor = strokeColor,
strokeWidth = strokeWidth,
shape = shape,
radius = radius,
OffsetX = OffsetX,
OffsetY = OffsetY,
cssClass = cssClass
))
}
#' @title Add a shaded area to a chart
#'
#' @description \code{add_shade()} allow to add a shaded area on specified range,
@ -300,3 +341,49 @@ add_vline <- function(ax, value, color = "#000", dash = 0, label = NULL, ...) {
#' Add an annotation point
#'
#' @param ax An \code{apexcharts} \code{htmlwidget} object.
#' @param x Coordinate(s) on the x-axis.
#' @param y Coordinate(s) on the y-axis.
#' @param size Size of the marker.
#' @param color Stroke Color of the marker point.
#' @param fill Fill Color of the marker point.
#' @param width Stroke Size of the marker point.
#' @param shape Shape of the marker: \code{"circle"} or \code{"square"}.
#' @param radius Radius of the marker (applies to square shape).
#' @param label Add a label to the shade, use a \code{character}
#' or see \code{\link{label}} for more controls.
#' @param ... Additional arguments, see
#' \url{https://apexcharts.com/docs/options/annotations/} for possible options.
#'
#' @return An \code{apexcharts} \code{htmlwidget} object.
#' @export
#'
#' @example examples/add_point.R
add_point <- function(ax, x, 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 = FALSE,
x = x, y = y,
marker = marker(
size = size,
fillColor = fill,
strokeColor = color,
strokeWidth = width,
shape = shape,
radius = radius
),
label = label,
...
)
}

52
examples/add_point.R Normal file
View File

@ -0,0 +1,52 @@
library(apexcharter)
# On scatter chart
apex(
data = iris,
aes(Sepal.Length, Sepal.Width),
"scatter"
) %>%
add_point(
x = mean(iris$Sepal.Length),
y = mean(iris$Sepal.Width)
)
# Some options
apex(
data = iris,
aes(Sepal.Length, Sepal.Width),
"scatter"
) %>%
add_point(
x = mean(iris$Sepal.Length),
y = mean(iris$Sepal.Width),
fill = "firebrick",
color = "firebrick",
size = 8,
label = label(text = "Mean", offsetY = 0)
)
# Several points
clusters <- kmeans(iris[, 1:2], 3)
apex(
data = iris,
aes(Sepal.Length, Sepal.Width),
"scatter"
) %>%
add_point(
x = clusters$centers[, 1],
y = clusters$centers[, 2]
)

105
man/add_point.Rd Normal file
View File

@ -0,0 +1,105 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/annotations.R
\name{add_point}
\alias{add_point}
\title{Add an annotation point}
\usage{
add_point(
ax,
x,
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{x}{Coordinate(s) on the x-axis.}
\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 annotation point
}
\examples{
library(apexcharter)
# On scatter chart
apex(
data = iris,
aes(Sepal.Length, Sepal.Width),
"scatter"
) \%>\%
add_point(
x = mean(iris$Sepal.Length),
y = mean(iris$Sepal.Width)
)
# Some options
apex(
data = iris,
aes(Sepal.Length, Sepal.Width),
"scatter"
) \%>\%
add_point(
x = mean(iris$Sepal.Length),
y = mean(iris$Sepal.Width),
fill = "firebrick",
color = "firebrick",
size = 8,
label = label(text = "Mean", offsetY = 0)
)
# Several points
clusters <- kmeans(iris[, 1:2], 3)
apex(
data = iris,
aes(Sepal.Length, Sepal.Width),
"scatter"
) \%>\%
add_point(
x = clusters$centers[, 1],
y = clusters$centers[, 2]
)
}