首页 > 解决方案 > 有没有办法从 plotly 或 d3.js 中检索图例

问题描述

在谷歌图表中,为了获得选定的图例,可以使用以下代码段

    google.visualization.events.addListener(chart, 'select', function () {
       var sel = chart.getSelection();
       // sel contains all the selected legends 
    }

情节中有没有类似的行为?这背后的意图是,根据传说,我必须得出不同的情节。例如,如果您选择两个图例,那么我必须绘制这两个变量之间的关系。我已经绑定了情节图例事件,因此每当我单击图例名称时,都会触发该事件。不幸的是,我无法检索任何图例名称。

或者是否可以在 d3.js 中获得选定的图例?

标签: javascriptd3.jsplotly

解决方案


使用 plotly 的选择器,我认为这样的事情可能会有所帮助。

var graphDiv = document.getElementById('graph');
Plotly.plot(graphDiv, figure);
graphDiv.on('plotly_selected', function(selectedData) {
    // do something with selectedData
});

推荐阅读