[英]R HIGHCHARTER - How to display additional series values on a highchart hover?
I want to display an additional data series on the hover of the highcharts I'm using on my flexdashboard. 我想在我的flexdashboard上使用的海图悬停时显示其他数据系列。 I understand that the way to do this is using the 'formatter' function of highcharts. 我了解实现此目的的方法是使用highcharts的“格式化程序”功能。 I've been able to store the additional series in a random variable in my series data and call it using the formatter function, but I get the complete list for every bar in my highcharts plot. 我已经能够将其他系列存储在我的系列数据中的随机变量中,并使用格式化程序功能对其进行调用,但是我得到了海图图中每个柱的完整列表。 I need individual values for every point. 我需要每一点的个人价值观。
Here's the code: 这是代码:
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Bottom Five Suppliers (Overall)") %>%
hc_xAxis(categories = suppliers$Supplier[20:24], tickInterval = 1) %>%
hc_yAxis(tickInterval = 1) %>% hc_add_series(data = suppliers$x[20:24],
name = "Overall Supplier Score (Weighted)", resp = suppliers$respondents[20:24]) %>%
hc_tooltip(valueDecimals = 2, useHTML = TRUE, formatter = JS("function()
{return this.series.options.resp;}"))
%>% hc_exporting(enabled = TRUE) %>% hc_plotOptions(
series = list(
boderWidth = 0,
dataLabels = list(enabled = TRUE)
))
This is the result: 结果如下:
As you can see, the data coming on hovering over the chart is different than the x or y axis. 如您所见,将鼠标悬停在图表上时得到的数据与x或y轴不同。 However, it is displaying the complete data for all the 5 bars rather than displaying one value for every bar. 但是,它将显示所有5个条的完整数据,而不是为每个条显示一个值。
I know I need to write a JavaScript function to match the values to rest of the data frame but I'm not sure how to do that (since I dont have much experience or knowledge of JS, sadly). 我知道我需要编写一个JavaScript函数来将值与其余数据帧进行匹配,但是我不确定该怎么做(可悲的是,因为我没有太多JS的经验或知识)。
If I use a series being used on the x or y axis then it works fine, (using this.x or this.y) but using an additional series stored in a random variable does not work without this.series.options.seriesname and then also it returns the whole series. 如果我使用在x轴或y轴上使用的序列,那么它可以正常工作(使用this.x或this.y),但是如果没有this.series.options.seriesname和然后它还会返回整个系列。
EDIT 编辑
Here's the code to reproduce this example: 这是重现此示例的代码:
library (highcharter)
library (dplyr)
Suppliers= data.frame(Supplier = c('one','two','three','four','five'),
Value = c(1,2,3,4,5), respondents= c(5,1,4,12,5),
Category = c('cat1','cat2','cat3','cat4','cat5'))
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Bottom Five Suppliers (Overall)") %>%
hc_xAxis(categories = Suppliers$Supplier, tickInterval = 1) %>%
hc_yAxis(tickInterval = 1) %>% hc_add_series(data = Suppliers$Value,
name = "Overall Supplier Score (Weighted)", resp = Suppliers$respondents) %>%
hc_tooltip(valueDecimals = 2, useHTML = TRUE, formatter = JS("function()
{return this.series.options.resp;}"))
%>% hc_plotOptions(
series = list(
boderWidth = 0,
dataLabels = list(enabled = TRUE)
))
When you hover over the produced highchart, it shows the complete respondents series instead of it showing individual values. 当您将鼠标悬停在生成的图表上时,它将显示完整的受访者系列,而不是显示各个值。
Okay, I tried something simple and it seemed to work. 好的,我尝试了一些简单的方法,但似乎可行。 I just added the points of the x axis as index to the series I'm calling. 我只是将x轴的点添加为我正在调用的序列的索引。
library (highcharter)
library (dplyr)
Suppliers= data.frame(Supplier = c('one','two','three','four','five'),
Value = c(1,2,3,4,5), respondents= c(5,1,4,12,5),
Category = c('cat1','cat2','cat3','cat4','cat5'))
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Bottom Five Suppliers (Overall)") %>%
hc_xAxis(categories = Suppliers$Supplier, tickInterval = 1) %>%
hc_yAxis(tickInterval = 1) %>% hc_add_series(data = Suppliers$Value,
name = "Overall Supplier Score (Weighted)", resp = Suppliers$respondents) %>%
hc_tooltip(valueDecimals = 2, useHTML = TRUE, formatter = JS("function()
{return this.series.options.resp[this.point.x];}"))
%>% hc_plotOptions(
series = list(
boderWidth = 0,
dataLabels = list(enabled = TRUE)
))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.