簡體   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