R语言highcharter实现悬停同时展示所有序列tooltip的问题
问题原因
你设置shared=TRUE未生效主要有两个原因:
- 你在每个
hc_add_series中单独配置了tooltip参数,系列级的tooltip配置优先级高于全局配置,会覆盖shared的共享逻辑 - 散点(scatter)类型序列默认的点位匹配规则是按xy轴坐标找最近点,不会自动匹配同x轴值的其他序列点位,需要额外调整匹配规则
解决代码
require(highcharter) pd1 <- data.frame(x=1:10,y=rnorm(10)) pd2 <- data.frame(x=1:10,y=rnorm(10)) plt <- highchart() %>% # 全局开启tooltip共享,统一设置头部格式 hc_tooltip(shared = TRUE, headerFormat = "<b>当前x值:{point.x}</b><br/>") %>% # 统一配置系列追踪规则,按x轴值匹配点位 hc_plotOptions( scatter = list(findNearestPointBy = "x"), line = list(findNearestPointBy = "x") ) %>% hc_add_series(pd1, "scatter", hcaes(x = x, y = y), color = "#7cb4ed", pointFormat = "data1:y1 = {point.y}<br/>", name = "data1") %>% hc_add_series(pd2, "line", hcaes(x = x, y = y), color = "red", pointFormat = "data2:y2 = {point.y}<br/>", name = "data2") plt
如果需要保留每个系列独有的提示样式,只需要把header相关的格式统一放到全局tooltip的headerFormat中配置即可,不要在系列级单独设置header参数,就能保证共享逻辑正常生效。
内容的提问来源于stack exchange,提问作者Jonas
相关产品推荐
相关产品推荐

