Positioning of tooltip
The default position of a tooltip is to the right and below the point hovered over/clicked on. This means that for points near the foot of the y axis and right of x axis a tooltip of any significant size is partially obscured.
Scroll bars do appear but it is still a bit fiddly with the click option and with hover, if one moves off the point to access the scrollbars then the tooltip disappears
Am I missing something? I could not see options in add_tooltip which might help. It would be helpful if ggvis was smart enough to position the tooltip vis a vis the point dependent on its position on the chart
cheers
Could it be possible to make the default position of the tooltip to be between the geometry hovered-over (or clicked on) and the center of the plot?
Consider a point at the right edge of the plot being "tooltipped". In this case, could the tooltip appear to the left of the point?
Hello @ijlyttle , @pssguy Is there any chance you accomplished the desired behavior and can share it?
Hello @ijlyttle @iSevenDays ,
Could you try this? Does it work?
add_tooltip <- function(vis, html, on = c("hover", "click")) {
on <- match.arg(on)
show_tooltip2 <- function(data, location, session, ...) {
if (is.null(data)) {
hide_tooltip(session)
return()
}
#Take all data in your data frame
fulldata = vis$cur_data()
#Select the 2 data labels that you plot on your graph
x = paste("fulldata", vis$scales[[1]]$label, sep="$")
y = paste("fulldata", vis$scales[[2]]$label, sep="$")
#Select the 2 data variable
x = eval(parse(text = x))
y = eval(parse(text = y))
#you make some quantile
q1.x = quantile(x,0.25)
q1.y = quantile(y,0.25)
q3.x = quantile(x,0.75)
q3.y = quantile(y,0.75)
#the coordonates of the selected point
point.x = data[,1]
point.y = data[,2]
html <- html(data)
if (is.null(html)) {
hide_tooltip(session)
} else {
#Put a position to the tooltip depending of the value compare to the 1st or 3rd quartile.
if(point.x < q1.x & point.y < q1.y){
# "+57", "-55", +"5" and "-10" can be changed if the tooltip doesn't appear exactly where you want
show_tooltip(session, location$x + 57, location$y -55, html)
}else{
if(point.x < q1.x & point.y > q3.y){
show_tooltip(session, location$x +57, location$y +5, html)
}else{
if(point.x > q3.x & point.y > q3.y){
show_tooltip(session, location$x -10, location$y +5, html)
}else{
show_tooltip(session, location$x - 10, location$y -55, html)
}
}
}
}
}
I have tried it on this little example and it was working:
all_values <- function(x) {
if(is.null(x)) return(NULL)
paste0(names(x), ": ", format(x), collapse = "<br />")
}
base <- mtcars %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_points()
base %>% add_tooltip(all_values, "hover")
I have not looked at this problem, as @hadley put it into his enhancement workflow for ggvis - I imagine he has a pretty good idea for how he will want to address it.
@woozaa - I have not had time to try out your proposed solution, but I see no reason it should not work and I'm glad that it works for you. If the 25% and 75% points of the x and y axes are available, I wonder if that might provide more-consistent behavior than using quantiles. Similarly, you might be able to set the x-offset (either -10 or 57) and the y-offset (-55 or 5) independently of each other.
@ijlyttle 👍 , thanks for your suggestion, I will continue to work on it to have a more consistent solution.