You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:37:41