I have generated a plotly json file that has a scatterplot. I would like to have it open a new browser tab when one of the points is clicked. I have coded the following, in R, to the plot to allow for that:
js <- "function(el, x) {
el.on('plotly_click', function(d) {
var point = d.points[0];
var url = point.data.customdata[point.pointIndex];
window.open(url, '_blank');
});
}"
fig <- fig %>% config(displayModeBar = F)
fig <- fig %>% onRender(js)
pj <- plotly_json(p = fig, FALSE)
filename.plot <- paste(barcode,".json",sep="")
However, when the JSON file is rendered the Javascript is not functioning. I am not sure where this is going wrong. Any help would be much appreciated.
Edited Thanks everyone. I was able to fix it by adding the javascript below:
`` $.getJSON(locfilepath, function(data) { Plotly.plot('locgraph', data, []);
var myPlot = document.getElementById('locgraph');
myPlot.on('plotly_click', function(data){
var urlStr = data.points[0].customdata;
if(urlStr.length>0){
window.open(data.points[0].customdata);
}
});
});
``