繁体   English   中英

R HIGHCHARTER-如何在海图悬停时显示其他系列值?

[英]R HIGHCHARTER - How to display additional series values on a highchart hover?

我想在我的flexdashboard上使用的海图悬停时显示其他数据系列。 我了解实现此目的的方法是使用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)
))

结果如下: 图片](http://imgur.com/a/rgcJm)[![其他系列的Highchart

如您所见,将鼠标悬停在图表上时得到的数据与x或y轴不同。 但是,它将显示所有5个条的完整数据,而不是为每个条显示一个值。

我知道我需要编写一个JavaScript函数来将值与其余数据帧进行匹配,但是我不确定该怎么做(可悲的是,因为我没有太多JS的经验或知识)。

如果我使用在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)
))

当您将鼠标悬停在生成的图表上时,它将显示完整的受访者系列,而不是显示各个值。

好的,我尝试了一些简单的方法,但似乎可行。 我只是将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM