简体   繁体   English

如何控制 plotly.js 中轨迹的显示顺序?

[英]How do I control the display order of traces in plotly.js?

I have a large number of trace lines being displayed.我显示了大量的跟踪线。 50-200 typically.通常为 50-200。 I am comparing historical data to simulated data.我正在将历史数据与模拟数据进行比较。 I want to highlight a single simulated traceline by selection via a different color.我想通过选择不同的颜色来突出显示单个模拟轨迹线。 All of this works fine.所有这些工作正常。

However, the trace line being shown is sometimes hidden by the large number of other lines.但是,显示的跟踪线有时会被大量其他线隐藏。 Is there a way to force the specified trace to be drawn on top of the others, aside from redrawing the entire plot and adding that line last?除了重绘整个图并最后添加那条线之外,有没有办法强制将指定的轨迹绘制在其他轨迹之上? Some sort of z-index equivalent or "bring to front" mechanism?某种 z-index 等效或“带到前面”机制?

Perhaps this useful to other Plotly users as well.也许这对其他 Plotly 用户也有用。 The snippet pushed trace 7 to the foreground using Plotly's react function.该代码段使用 Plotly 的react函数将跟踪 7 推到了前台。

 var foreground = 7; //trace which is pushed to the foreground //generate some random data var trace1 = { x: [1, 2, 3, 4], y: [1, 2, 0.5, 1.5], type: 'lines', name: 'trace 0', line: { width: 2 } }; var data = [trace1]; var i = 0; var j = 0; for (i = 1; i < 10; i += 1) { var trace = { x: [], y: [], type: 'lines', name: 'trace ' + i, 'line': { 'width': 2 } }; for (j = 0; j < trace1.x.length; j += 1) { trace.x.push(trace1.x[j]) trace.y.push(trace1.y[j] + Math.random()) } data.push(trace); } var buttonForeground = document.getElementById("foreground"); var buttonReset = document.getElementById("reset"); var div = document.getElementById('myDiv'); Plotly.newPlot(div, data); buttonReset.disabled = true; buttonForeground.onclick = function() { var temp = data[foreground]; data[foreground] = data[data.length - 1]; data[data.length - 1] = temp; data[data.length - 1]['line']['width'] = 5; Plotly.react(div, data); switchButtons(); }; document.getElementById("reset").onclick = function() { var temp = data[data.length - 1]; data[data.length - 1] = data[foreground]; data[foreground] = temp; data[foreground]['line']['width'] = 2; Plotly.react(div, data, {}); switchButtons(); }; function switchButtons() { buttonForeground.disabled = !buttonForeground.disabled; buttonReset.disabled = !buttonReset.disabled; }
 <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <div id="myDiv" style="width: 480px; height: 400px;"></div> <button type="button" id='foreground'>Foreground</button> <button type="button" id='reset'>Reset</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM