我希望在我的flexdashboard上使用的高架图上悬停显示一个额外的数据系列。我知道这样做的方法是使用'格式化程序' highcharts的功能。我已经能够将附加系列存储在我的系列数据中的随机变量中,并使用formatter函数调用它,但我得到了我的highcharts图中每个条形的完整列表。我需要为每一点提供单独的价值观。
以下是代码:
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)
))
这是结果:
如您所见,悬停在图表上的数据与x轴或y轴不同。但是,它显示所有5个柱的完整数据,而不是为每个柱显示一个值。
我知道我需要编写一个JavaScript函数来匹配数据框其余部分的值,但我不知道该怎么做(因为我没有太多经验或知识的JS,遗憾的是)。 / p>
如果我使用在x或y轴上使用的系列,那么它工作正常,(使用this.x或this.y)但是如果没有this.series.options,使用存储在随机变量中的附加系列是行不通的.seriesname然后它也返回整个系列。
修改
以下是重现此示例的代码:
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)
))
当您将鼠标悬停在生成的状态图上时,它会显示完整的受访者系列,而不是显示单个值。
答案 0 :(得分:0)
好的,我尝试了一些简单的东西,似乎有效。我只是将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)
))