How can I get tooltips displayed in digraphs without annotation

I am trying to use the implementation of R dygraphs

The given example:

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") 

leading to a chart enter image description here

Hovering over "A" brings up a tooltip with "Korea."

I really want to have a tooltip for each point, it is preferable to completely abandon the text requirement - although setting the text to "" with the minimum height and width values ​​may be sufficient. I would also like to programmatically use dyAnnotations 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")) 

Is this possible, and if so, how? TIA

+7
javascript r dygraphs
source share
2 answers

Well, as promised, here is the beginning of how we can use the legend for your information. We are roughly rewriting the legend. This behavior can be made more polite if you also want a legend. Alternatively, you can provide an object / hash with data.frame to search for x and return an informative description.

I added debugger , so if you open your debugger in Chrome, etc., you can see what happens.

 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, not 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)))) ) ) 
+2
source share

Most of the settings for Dygraphs are CSS style. For example, here's how we can change the default tooltip behavior. With that in mind and a bit of help for the Dygraphs annotation documentation , we can do something similar for the first question.

 # answers Qaru 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 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; } " 

for the second question, here is one way to achieve this

 # 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 
+2
source share

All Articles