When running the demo App below, the problem I run into is that hover messages for the bottom part of the plot end up running off the screen.
Does anybody know if there is a way to adjust the position so that the entire message always falls within the screen boundaries (l,r,t,b)?
require('shiny')
require('ggplot2')
library(DT)
ui <- pageWithSidebar(
headerPanel("Hover off the page"),
sidebarPanel(width = 2
),
mainPanel(
tags$head(
tags$style('
#my_tooltip {
position: absolute;
pointer-events:none;
z-index: 1;
padding: 0;
}'),
tags$script('
$(document).ready(function() {
setTimeout(function(){
$("[id^=FP1Plot]").mousemove(function(e) {
$("#my_tooltip").show();
$("#my_tooltip").css({
top: (e.offsetY) + "px",
left: (e.pageX -300) + "px"
});
});
},1000)});')
),
plotOutput('FP1Plot1' ,
width = 1000,
height = 800,
hover = hoverOpts(id = 'FP1Plot1_hover', delay = 0)
),
uiOutput("my_tooltip"),
style = 'width:1250px'
)
)
server <- function(input, output, session) {
ranges <- reactiveValues()
output$FP1Plot1 <- renderPlot({
ggplot(mtcars, aes(wt, mpg, color = as.factor(cyl))) + geom_point() +
coord_cartesian(xlim = ranges[[paste('FP1Plot1', 'x', sep = '')]],
ylim = ranges[[paste('FP1Plot1', 'y', sep = '')]]
)
})
tooltipTable <- reactive({
y <- nearPoints(mtcars, input$FP1Plot1_hover,
threshold = 15)
if(nrow(y)){
datatable(t(y), colnames = rep("", nrow(y)),
options = list(dom = 't'))
}
})
output$my_tooltip <- renderUI({
req(tooltipTable())
wellPanel(DTOutput("vals"),
style = 'background-color:#fff; padding:10px; width:400px;border-color:#339fff')
})
output$vals <- renderDT({
tooltipTable()
})
}
shinyApp(ui, server)
Here is a solution with the JS library qTip2.
@ stephane, I came up with another solution using 'sending css' code to update the position of the hover message. The only problem I still run into, is that the position doesn't update until the message content changes for the second time to a point in a quadrant.
there are 2 values for x offset and 2 for y offset, splitting the plot in 4 quadrants in fact. Switching to another quadrant puts the message in the last quadrants configuration, and doesn't correct this until I hover on a second point in the new quadrant.
Do you have any idea to push the css change more effectively? perhaps with sendcustommessage or so? I tried to do it that way, but couldn't get it to work at all with that approach. Here is my code attempt so far: