如何在没有注释的情况下获取dygraph中显示的工具提示

我正在尝试使用dygraphs的R实现

提供的例子是

library(dygraphs)

dygraph(presidents, main = "Presidential Approval") %>%
dyAxis("y", valueRange = c(0, 100)) %>%
dyAnnotation("1950-7-1", text = "A", tooltip = "Korea") %>%
dyAnnotation("1965-1-1", text = "B",  tooltip = "Vietnam")

这导致了图表 在这里输入图像描述

悬停在'A'上会产生一个提示'韩国'

我非常希望为每个点提供一个工具提示,最好完全符合文本要求 - 尽管将文本设置为“”并且最小高度/宽度值可能就足够了。 我也想通过日期和工具提示栏以编程方式附加dyAnnotations

df <- data.frame(date=as.Date(c("1950-7-1","1965-1-1")),tooltip=c("Korea","Vietnam"))

这是否可行,如果是这样,如何? TIA


好吧,按照承诺,这里是我们如何使用图例来获取信息的开始。 我们粗暴地覆盖了这个传说。 如果你还想要一个图例,这种行为可以变得更有礼貌。 另外,你可以提供一个带有data.frame的对象/散列来查找x并返回一个信息丰富的描述。

我添加了一个debugger所以如果你在Chrome中打开调试器等,你可以看到发生了什么。

library(dygraphs)

dyG = dygraph(presidents, main = "Presidential Approval") %>%
    dyAxis("y", valueRange = c(0, 100))

#  explore the legend route
dyG %>%
    dyCallbacks(
        highlightCallback = sprintf(
'function(e, x, pts, row) {

// added to illustrate what is happening
//   remove once satisfied with your code
debugger;  

var customLegend = %s

// should get our htmlwidget
e.target.parentNode.parentNode
  .querySelectorAll(".dygraph-legend")[0]
  .innerText = customLegend[row] + row;
}'
            ,# supply a vector or text that you would like
            jsonlite::toJSON(rep('something here',length(as.vector(presidents))))
        )
    )

下面,我改变了添加到图例而不是替换。

#  explore the legend route
#    add to legend rather than replace
dyG %>%
  dyCallbacks(
    highlightCallback = sprintf(
      'function(e, x, pts, row) {

      // added to illustrate what is happening
      //   remove once satisfied with your code
      debugger;  

      var customLegend = %s


      // should get our htmlwidget
      var legendel = e.target.parentNode.parentNode
        .querySelectorAll(".dygraph-legend")[0];

      // should get our htmlwidget
      legendel.innerHTML = legendel.innerHTML + "<br>" + customLegend[row];
      }'
            ,# supply a vector or text that you would like
      jsonlite::toJSON(rep('something here',length(as.vector(presidents))))
    )
  )

大多数Dygraphs自定义都发生在CSS样式中。 例如,以下是我们如何更改默认工具提示行为。 考虑到这一点以及Dygraphs注释文档的一些帮助,我们可以为第一个问题做这样的事情。

# answers Stack Overflow question
#   http://stackoverflow.com/questions/27671576/how-can-i-get-tooltips-showing-in-dygraphs-without-annotation
# on how to customize annotations

library(dygraphs)

# question is two parts - let's handle part 1 first
dygraph(presidents, main = "Presidential Approval") %>%
  dyAxis("y", valueRange = c(0, 100))  %>%
  dyAnnotation("1950-7-1", text = "Korea", tooltip = ""
               # this is not necessary but think it better to be specific
               ,cssClass = "specialAnnotation") %>%
  # will leave this as before 
  dyAnnotation("1965-1-1", text = "Vietnam", tooltip = "") -> dyG

#this is a hack to set css directly
#  dyCSS designed to read a text css file
dyG$x$css = "
  /* if cssClass not assigned use .dygraphDefaultAnnotation */
  /*  !important is critical for the rules to be applied */
  .specialAnnotation {
    overflow: visible !important;
    width: initial !important;
  }
"

对于第二个问题,这里是我们可以完成这个的一个方法

# now for part 2 
dyG = dygraph(presidents, main = "Presidential Approval") %>%
  dyAxis("y", valueRange = c(0, 100))

tooltips = list(
  list(x = "1950-7-1", tooltip = "", text = "Korea")
  ,list(x = "1965-1-1", tooltip = "", text = "Vietnam")
) 

annotator <- function(x,y){
  d = do.call(dyAnnotation,modifyList(list(dygraph=x),y))
  return(d)
}

dyG = Reduce( annotator, tooltips, init=dyG )

#this is a hack to set css directly
#  dyCSS designed to read a text css file
dyG$x$css = "
  /* if cssClass not assigned use .dygraphDefaultAnnotation */
  /*  !important is critical for the rules to be applied */
  .dygraphDefaultAnnotation {
    overflow: visible !important;
    width: initial !important;
    border: none !important;
    font-size: 200% !important;
  }
"

dyG
链接地址: http://www.djcxy.com/p/82987.html

上一篇: How can I get tooltips showing in dygraphs without annotation

下一篇: Not being prompted to enable Location Services in App