apexcharter/vignettes/sync-charts.Rmd

99 lines
2.1 KiB
Plaintext
Raw Normal View History

2020-02-12 18:37:17 +01:00
---
title: "Syncing charts"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Syncing charts}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(apexcharter)
data("economics", package = "ggplot2")
```
2020-03-04 16:52:37 +01:00
## Sync charts
2020-04-01 15:44:25 +02:00
With [Apexcharts](https://apexcharts.com) you can sync (tooltip, zoom) several charts together by providing a group and id to each charts. This works in Shiny and Markdown. Here an example where we create two charts, tooltip will be displayed on both charts when you hover one, and if you zoom on one, the other one will be synced :
2020-02-12 18:37:17 +01:00
```{r example-1, eval=FALSE}
apex(
2020-03-04 16:52:37 +01:00
data = tail(economics, 150),
2020-02-12 18:37:17 +01:00
mapping = aes(x = date, y = pce),
type = "line"
) %>%
2020-02-15 15:17:46 +01:00
ax_chart(
group = "economics", id = "pce" # <- define common group and unique id
)
2020-02-12 18:37:17 +01:00
apex(
2020-03-04 16:52:37 +01:00
data = tail(economics, 150),
2020-02-12 18:37:17 +01:00
mapping = aes(x = date, y = psavert),
type = "line"
) %>%
2020-02-15 15:17:46 +01:00
ax_chart(
group = "economics", id = "psavert" # <- define common group and unique id
)
2020-02-12 18:37:17 +01:00
```
```{r run-example-1, echo=FALSE, ref.label="example-1"}
```
2020-03-04 16:52:37 +01:00
## Brush chart
2020-04-01 15:44:25 +02:00
Create a brush chart to navigate into a synced chart : use the chart below to navigate in the chart above.
2020-03-04 16:52:37 +01:00
```{r example-2, eval=FALSE}
apex(
data = economics,
mapping = aes(x = date, y = psavert),
type = "line"
) %>%
ax_chart(
2020-03-04 19:02:01 +01:00
id = "target-chart", # <-- define target id here
2020-03-04 16:52:37 +01:00
toolbar = list(
autoSelected = "pan",
show = FALSE
)
)
apex(
data = economics,
mapping = aes(x = date, y = psavert),
type = "line",
height = "130px"
) %>%
ax_chart(
brush = list(
2020-03-04 19:02:01 +01:00
target = "target-chart", # <-- use target id here
2020-03-04 16:52:37 +01:00
enabled = TRUE
),
2020-03-04 19:02:01 +01:00
offsetY = -20,
2020-03-04 16:52:37 +01:00
selection = list(
2020-03-04 19:02:01 +01:00
enabled = TRUE, # <-- enable selection and define starting range
2020-03-04 16:52:37 +01:00
xaxis = list(
min = format_date(economics$date[1]),
max = format_date(economics$date[100])
)
)
) %>%
ax_xaxis(labels = list(show = FALSE)) %>%
ax_yaxis(labels = list(show = FALSE))
```
```{r run-example-2, echo=FALSE, ref.label="example-2"}
```