如果 Plotly 没有为我们提供隐藏由以下人员创建的图例的方法highlight
,那么让我们使用 Plotly 自带的函数来实现这一点。
隐藏一次跟踪所需的 JavaScript 代码:
var updated = {showlegend: false};
var myPlot = document.getElementsByClassName('plotly')[0];
Plotly.restyle(myPlot, updated, [2]);
在这种情况下,它始终是第三个元素[2]
需要隐藏其图例,通常最好根据某些条件动态获取索引。
让我们将其添加到 Plotly 中on_click
事件以确保传奇在未来也将不可见。
myPlot.on('plotly_click', function(data){
Plotly.restyle(myPlot, updated, [2]);
});
最后将所有内容添加到输出中,我们就可以了。
javascript <- "
var updated = {showlegend: false};
var myPlot = document.getElementsByClassName('plotly')[0];
Plotly.restyle(myPlot, updated, [2]);
myPlot.on('plotly_click', function(data){
Plotly.restyle(myPlot, updated, [2]);
});
"
w <- plotly::as_widget(gp)
w <- htmlwidgets::prependContent(w, onStaticRenderComplete(javascript), data=list(''))
htmlwidgets::saveWidget(w, "cities.html")
w