单击点时绘制散点图超链接

Plotly Scatterplot hyperlink when point is clicked

是否可以 link all/some 散点图的点,所以每当您单击它们时,都会打开一个新选项卡,并且 hyperlink linked 在这一点上被解雇了?

我在 Django 网络服务器实现中使用 plotly,这意味着 plotly 是使用 javascript.

呈现的

您可以使用 click handler 来实现它。但可能很难在新标签页中打开,因为大多数浏览器始终试图阻止弹出窗口。

var trace1 = {
  x: [1, 2, 3],
  y: [1, 6, 3],
  mode: 'markers',
  type: 'scatter',
  text: ['Plotly', 'Whosebug', 'Google'],
  hoverinfo: 'text',
  marker: { size: 12 }
};

var links = ['https://plot.ly/', 'http://whosebug.com/', 'https://google.com/'];

var data = [ trace1 ];

var layout = { 
  title:'Hyperlinked points'
};

var myPlot = document.getElementById('myDiv');
Plotly.newPlot(myPlot, data, layout);

myPlot.on('plotly_click', function(data){
  if (data.points.length === 1) {
    var link = links[data.points[0].pointNumber];
    
    // Note: window navigation here.
    window.location = link;
  }
});
<!-- Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>

<div id="myDiv" style="width: 480px; height: 300px;"></div>