I m trying to build a custom D3 tooltip that displays a tweet widget using the copy/paste code from publish.twitter.com.
I m setting up plots using plotly in R and would like a tweet widget to be displayed when clicking on the respective data point (using this example tweet here). I have managed to use the approach described here to display image tooltips and have tried to adapt it to show tweet widgets. However, while the text of the tweet, the twitter handle and date of the tweet are displayed on clicking, the tweet widget is not (Tweet text is displayed, widget isn t.. The widgets.js script source is provided in the copy/paste text from twitter, but seems to fail, leaving only the tweet text etc. to be displayed.
Here is a minimal example in R:
# Load packages
library(plotly)
library(htmlwidgets)
library(htmltools)
library(magrittr)
# Define text and position
x <- 1
y <- 1
label <- c("Click here for tweet")
# D3 dependency
d3 <-
htmltools::htmlDependency("d3", "7.3",
src = c(href = "https://cdnjs.cloudflare.com/ajax/libs/d3/7.3.0/"),
script = "d3.min.js")
# Define plot, provide .js file with custom tweet tooltip & add D3 dependency
p <- plot_ly() %>%
add_text(x = x, y = y, text = label) %>%
htmlwidgets::onRender(readLines("tweet_tooltip.js"))
p$dependencies <- c(p$dependencies, list(d3))
# Render plot
p
Where tweet_tooltip.js contains the following function:
// tweet_tooltip.js
// Adapted from https://stackoverflow.com/questions/71601216/r-plotly-display-image-on-hover
function(el) {
// Define tooltip
var tooltip = d3.select( # + el.id + .svg-container )
.append("div")
.attr("class", "my-custom-tooltip");
el.on( plotly_click , function(d) {
var pt = d.points[0];
// Choose a location (on the data scale) to place the image: upper left corner
var x = pt.xaxis.range[0];
var y = pt.yaxis.range[1];
// Transform the data scale to the pixel scale
var xPixel = pt.xaxis.l2p(x) + pt.xaxis._offset;
var yPixel = pt.yaxis.l2p(y) + pt.yaxis._offset;
// Define tweet variable with twitter blockquote/script
var tweet = <blockquote class="twitter-tweet"><p lang="en" dir="ltr">How much is it?</p>— Elon Musk (@elonmusk) <a href="https://twitter.com/elonmusk/status/943902052542849024?ref_src=twsrc%5Etfw">December 21, 2017</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
// Add to tooltip & define position
tooltip.html(tweet)
.style("position", "absolute")
.style("left", xPixel + "px")
.style("top", yPixel + "px");
tooltip
});
}