Plotly JSON散点图点击开新标签:R代码失效及解决方案
问题:Plotly JSON散点图点击跳转新标签页功能失效及解决方法
我生成了一个包含散点图的Plotly JSON文件,希望点击图中数据点时打开新浏览器标签页。用R编写了以下代码实现该功能:
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="")
但渲染JSON文件时这段JavaScript无法生效,不清楚问题出在哪,希望得到帮助。
解决方法
通过添加以下JavaScript代码解决了问题:
$.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); } }); });
内容的提问来源于stack exchange,提问作者Ann
相关产品推荐
相关产品推荐

