How can I get tooltips showing in dygraphs without annotation

前端 未结 2 1999
隐瞒了意图╮
隐瞒了意图╮ 2021-02-03 11:46

I am trying to use the R implementation of dygraphs

The example provided is

library(dygraphs)

dygraph(presidents, main = \"Presidential Approval\") %>         


        
2条回答
  •  面向向阳花
    2021-02-03 11:51

    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.

    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 + "
    " + customLegend[row]; }' ,# supply a vector or text that you would like jsonlite::toJSON(rep('something here',length(as.vector(presidents)))) ) )

提交回复
热议问题