首先,加载recharts
:
library(recharts)
提示框是交互的关键控件。它可以多级配置,作用于全局或局部。可以用setTooltip
(setTT
)配置全局或setSeries
配置局部。
用提示框,建议对JavaScript略有了解,以便更好地利用formatter
模板。
建议用chart %>% setTT(...)
格式调用setTT
。
所有函数名都有小驼峰法和小写连划线(Hadly Wickham推荐此法)两种形式。为了便于偷懒,还有缩写版(如tooltip缩写成tt)。
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,
...)
参数 | 要求 |
---|---|
chart |
|
series |
数据系列名称或索引值向量,如 |
timeslots |
时间轴切片名称或索引值向量,如 |
trigger |
触发类型,‘item’或’axis’ |
formatter |
提示框文本格式
{a}, {b}, {c}, {d}的含义:
JS函数参数模板…
|
islandFormatter |
数据岛样式,默认 |
position |
可以是固定值 |
enterable |
用户是否可以点击提示框的内容,默认为FALSE. |
axisPointer |
坐标指示器格式,默认为 |
textStyle |
提示框文本格式,默认为 |
showDelay |
显示延时(ms). 默认为20ms. |
hideDelay |
隐藏延时(ms). 默认为100ms. |
transitionDuration |
动画切换的耗时 ,默认为0.4. 如要实时交互,设为0. |
bgColor |
提示框背景色,默认为’rgba(0,0,0,0.7)’ (半透明深灰色). |
borderColor |
提示框边框色,默认为’#333’. |
borderWidth |
提示框边框宽度,默认为0 (不显示). |
borderRadius |
提示框边框圆角半径,默认为4px. |
show |
逻辑型,是否显示提示框。默认为TRUE. |
… |
省略号 |
g <- echartr(iris, Sepal.Width, Petal.Width, Species, weight=Petal.Length,
type='bubble') %>% setSymbols('circle')
g
鼠标掠过数据点,可以发现气泡图提示框内容的格式为series:\nx, y, weight
, 在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;
}
}
}
可以略作调整。
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];
}'))
现在其格式变成 series:\n<x: >...\n<y: >...\n<weight: >...
.
散点图的trigger
默认为axis
。如改为item
,则axisPointer
属性被禁用。
g %>% setTT(trigger='item')
修改背景色或任何其他美工属性。
g %>% setTT(bgColor='green')
如果只想设置某些特定系列的提示框格式,可指定特定的series
或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'))