如何让Highcharter多系列共用一个名称且tooltip中不重复显示
解决方案
你当前的问题是所有系列共用同一个名称,而pointFormat会作用在每一个系列的提示条目上,导致系列名重复输出。结合你已经开启shared = TRUE(共享tooltip)的配置,只需要调整tooltip的结构,把公共的系列名移到仅展示一次的header部分即可,具体修改如下:
series_name = "Census" # Default plot settings hc = highchart(type="stock") %>% hc_add_series(data = census.by.day, hcaes(x = time_ts, y = census, group = area), type = "line", name = series_name) %>% hc_legend(enabled=TRUE) %>% hc_tooltip(crosshairs = TRUE, backgroundColor = "#FCFFC5",shared = TRUE, borderWidth = 1, split = FALSE, # 新增headerFormat,公共内容仅展示一次 headerFormat = paste0('<b>', series_name, '</b><br>{point.key}<br>'), # 移除pointFormat里的series.name字段 pointFormat = '{point.area}: {point.y:.0f} <br>') %>% hc_xAxis(title=list(text="Date Hour")) %>% hc_chart(backgroundColor="white")
配置说明
headerFormat是共享tooltip的头部公共区域,所有系列共用,仅渲染一次,这里放入统一的系列名,还可以同时展示x轴的时间值,{point.key}默认会自动适配x轴的时间格式,你也可以自定义时间格式,比如{point.key:%Y-%m-%d %H:%M}指定精确到小时的展示格式- 调整后的
pointFormat仅保留每个系列独有的区域、数值信息,不会再重复输出系列名 - 后续如果要修改统一系列名,只需要修改
series_name变量即可,不需要同步修改tooltip配置
内容的提问来源于stack exchange,提问作者Jamie
相关产品推荐
相关产品推荐

