/
tooltip.Rmd
118 lines (96 loc) · 2.78 KB
/
tooltip.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
title: "Tooltip"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Tooltip}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(g2r)
```
G2.js provides a custom tooltip but often times one might want to customise it.
## Default
```{r}
data(penguins, package = "palmerpenguins")
g2(penguins, asp(bill_length_mm, bill_depth_mm, color = species)) %>%
fig_point(asp(shape = "circle")) %>%
gauge_color(c("#0C59FE", "#FEC700", "#FC0F00"))
```
## Global
The global tooltip can be customised with the `tooltip` function. This means applying changes to all tooltips, of all figures.
```{r}
penguins %>%
g2(asp(bill_length_mm, bill_depth_mm, color = species)) %>%
fig_point(asp(shape = "circle")) %>%
gauge_color(c("#0C59FE", "#FEC700", "#FC0F00")) %>%
tooltip(title = "Penguins!")
```
This can be used to remove the marker that shows on-hover.
```{r}
df <- data.frame(x = letters, y = runif(26))
g2(df, asp(x, y)) %>%
fig_interval() %>%
tooltip(showMarkers = FALSE)
```
```{r}
g2(iris, asp(Petal.Width, Petal.Length, color = Species)) %>%
fig_point(asp(shape = Species)) %>%
tooltip(
showCrosshairs = TRUE,
crosshairs = list(
type = "xy"
)
) %>%
gauge_color_set2() %>%
gauge_shape(c("square", "circle", "triangle"))
```
## Aspects
One somewhat confusing thing perhaps is that the `tooltip` aspect can be used multiple times: G2.js allows using multiple columns from the dataset in the tooltip, including columns that are not used elsewhere.
```{r}
g2(penguins, asp(bill_length_mm, bill_depth_mm, color = species)) %>%
fig_point(
asp(
shape = "circle",
tooltip = island,
tooltip = bill_length_mm,
tooltip = bill_depth_mm
)
) %>%
gauge_color(c("#0C59FE", "#FEC700", "#FC0F00"))
```
## Template & Gauge
The `tooltip` aspect can be combined with a template and a gauge. The template should be passed to the `itemTpl` argument of the `tooltip` function. The DOM classes can be found [here](https://g2.antv.vision/en/docs/api/general/tooltip); the HTML template must be passed as a string.
There are also two convenience function to create such templates: `tpl`, and `tpl_item`.
```{r}
template <- tpl(
tpl_item(
island,
bill_depth_mm
)
)
cb <- htmlwidgets::JS(
"(island, bill_depth_mm) => {
return {
island: island,
bill_depth_mm: bill_depth_mm
};
}"
)
penguins %>%
g2(asp(bill_length_mm, bill_depth_mm, color = species), elementId = "x") %>%
fig_point(
asp(
shape = "circle",
tooltip = island,
tooltip = bill_depth_mm
)
) %>%
gauge_color(c("#0C59FE", "#FEC700", "#FC0F00")) %>%
gauge_tooltip(cb) %>%
tooltip(itemTpl = template)
```