First, you should load recharts
:
library(recharts)
Tooltip is the key widget for interaction. It could take effect globally or locally. You can either use setTooltip
(setTT
) or setSeries
to do the trick.
When you use tooltip, you are recommended to learn some basics about JavaScript to make full use of formatter
template.
The recommended approach to use setTT
is chart %>% setTT(...)
.
All the function names have two forms: small camel and lowercase_underscore (endorsed by Hadley Wickham). To be lazy, we also provide short forms as well (e.g., tt for tooltip).
setTooltip(chart, series = NULL, timeslots = NULL, trigger = NULL,
formatter = NULL, islandFormatter = "{a} < br/>{b} : {c}",
position = NULL, enterable = FALSE, axisPointer = NULL,
textStyle = NULL, showDelay = 20, hideDelay = 100,
transitionDuration = 0.4, bgColor = "rgba(0,0,0,0.7)",
borderColor = "#333", borderWidth = 0, borderRadius = 4, show = TRUE,
...)
setTT(chart, series = NULL, timeslots = NULL, trigger = NULL,
formatter = NULL, islandFormatter = "{a} < br/>{b} : {c}",
position = NULL, enterable = FALSE, axisPointer = NULL,
textStyle = NULL, showDelay = 20, hideDelay = 100,
transitionDuration = 0.4, bgColor = "rgba(0,0,0,0.7)",
borderColor = "#333", borderWidth = 0, borderRadius = 4, show = TRUE,
...)
set_tt(chart, series = NULL, timeslots = NULL, trigger = NULL,
formatter = NULL, islandFormatter = "{a} < br/>{b} : {c}",
position = NULL, enterable = FALSE, axisPointer = NULL,
textStyle = NULL, showDelay = 20, hideDelay = 100,
transitionDuration = 0.4, bgColor = "rgba(0,0,0,0.7)",
borderColor = "#333", borderWidth = 0, borderRadius = 4, show = TRUE,
...)
set_tooltip(chart, series = NULL, timeslots = NULL, trigger = NULL,
formatter = NULL, islandFormatter = "{a} < br/>{b} : {c}",
position = NULL, enterable = FALSE, axisPointer = NULL,
textStyle = NULL, showDelay = 20, hideDelay = 100,
transitionDuration = 0.4, bgColor = "rgba(0,0,0,0.7)",
borderColor = "#333", borderWidth = 0, borderRadius = 4, show = TRUE,
...)
Arg | Requirement |
---|---|
chart |
Echarts object generated by |
series |
A vector of series indices or names. e.g., |
timeslots |
A vector of time slices indices or names, e.g., |
trigger |
Type of trigger, ‘item’ or ‘axis’ |
formatter |
The format of the tooltip content.
Meaning of {a}, {b}, {c}, {d}:
JS Function Param Template…
|
islandFormatter |
Data island formatter, default |
position |
Can be fixed position array |
enterable |
If users can click into the tooltip for interacions. Default FALSE. |
axisPointer |
The pointer formatter of axis. Default is a list |
textStyle |
Text style of the tooltip. In a list form. Default |
showDelay |
Delayed time at show (ms). Default 20ms. |
hideDelay |
Delayed time at hide (ms). Default 100ms. |
transitionDuration |
The time spent at animation exchange. Default 0.4. Set if 0 if you want real-time interaction. |
bgColor |
Background color of tooltips. Default ‘rgba(0,0,0,0.7)’ (semi-transparent dark gray). |
borderColor |
Borderline color of the tooltips. Default ‘#333’. |
borderWidth |
Border width of the tooltips. Default 0 (not shown). |
borderRadius |
Border radius of the tooltips. Default 4px. |
show |
Logical. If the tooltips are shown. Default TRUE. |
… |
Elipsis. |
g <- echartr(iris, Sepal.Width, Petal.Width, Species, weight=Petal.Length,
type='bubble') %>% setSymbols('circle')
g
Hover the mouse over the data points and you’ll find that the default formatter of the tooltip in bubble charts is series:\nx, y, weight
, i.e., in JavaScript:
function (params) {
var i;
var text;
if (params.seriesName === null || params.seriesName === ""){
if (params.value.length > 1) {
text = params.value[0];
for (i = 1; i < params.value.length; i++){
text += " , " + params.value[i];
}
return text;
} else {
return params.name + " : " + params.value;
}
} else {
if (params.value.length > 1) {
text = params.seriesName + " :<br/>" + params.value[0];
for (i = 1; i < params.value.length; i++) {
text += " , " + params.value[i];
}
return text;
} else {
return params.seriesName + " :<br/>"
+ params.name + " : " + params.value;
}
}
}
You can change the formatter a little bit.
g %>% setTT(formatter=JS('function (params) {
return params.seriesName + "<br/>Sepal Width: " + params.value[0] +
"<br/>Petal Width: " + params.value[1] + "<br/>Petal Length: " +
params.value[2];
}'))
Now it becomes series:\n<x: >...\n<y: >...\n<weight: >...
.
The trigger
for scatterplots are axis
by default. If change it to item
, the axisPointer
effect is disabled.
g %>% setTT(trigger='item')
Change the bgColor or other aesthetics as you like.
g %>% setTT(bgColor='green')
If you only want to change the tooltip in some of the series, you can assign specific series
or timeslots
.
g %>% setTT(1, bgColor='orange', textStyle=textStyle(
fontFamily='Times New Roman', color='black')) %>%
setTT(2, bgColor='lightblue', textStyle=textStyle(
fontFamily='Courier New', color='black')) %>%
setTT(3, bgColor='purple', textStyle=textStyle(fontFamily='Verdana'))