如何在没有注释的情况下显示在dygraph中显示的工具提示 [英] How can I get tooltips showing in dygraphs without annotation
问题描述
我正在尝试使用dygraphs的R实现
I am trying to use the R implementation of dygraphs
提供的示例是
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上会产生工具提示使用'Korea'
Hovering over the 'A' produces a tooltip with 'Korea'
我希望每个点都有一个工具提示,最好完全符合文本要求 - 尽管将文本设置为具有最小的高度/宽度值可能满足。我还想以编程方式从带有日期和工具提示列的文件中附加dyAnnotations
I am keen to have a tooltip for every point preferably dispensing entirely with the text requirement - though setting text to "" with minimal height/width values might suffice. I would also want to attach the dyAnnotations programmatically from a file with date and tooltip columns
df <- data.frame(date=as.Date(c("1950-7-1","1965-1-1")),tooltip=c("Korea","Vietnam"))
这是可行的,如果是这样,怎么样?
TIA
Is this feasible,and, if so, how? TIA
推荐答案
好的,正如所承诺的,这是我们如何使用图例来获取您的信息的开始。我们粗略地覆盖了传说。如果您还想要一个图例,这种行为可以更有礼貌。此外,您可以提供带有 data.frame
的对象/哈希来查找 x
并返回信息性描述。
Alright, as promised, here is a start to how we might use the legend for your information. We crudely overwrite the legend. This behavior can be made much more polite if you also want a legend. In addition, you could provide an object/hash with a data.frame
to lookup the x
and return an informative description.
我添加了一个调试器
所以如果你在Chrome等中打开调试器,你可以看到发生了什么。
I added a debugger
so if you open your debugger in Chrome, etc. you can see what is happening.
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))))
)
)
下面,我已更改为添加到图例而不是替换。
Below, I have changed to add to the legend rather than replace.
# 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))))
)
)
这篇关于如何在没有注释的情况下显示在dygraph中显示的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!