当通过Javascript API使用任意类型的可绘制图形(plotly.js,而不是plotly-nodejs)时,如何在图形加载后仅运行一些javascript?我正在寻找可绘制的javascript API中的机制,例如,与使用Google Maps API时执行的’addListenerOnce’以及’tilesloaded’或’ready’相同的功能.
例如,假设我要在呈现https://plot.ly/~PlotBot/685所示的示例后立即简单地显示警报,可以在https://plot.ly/~PlotBot/685.js看到html和js代码.
这样做有很多原因,但是我现在遇到的一个问题是使用wkhtml2pdf,它必须等待页面呈现后才能显示(请参阅wkhtmltopdf javascript delay)(是的,我知道我可以在wkhtml2pdf中使用–javascript-delay ,但是硬编码固定的时间量,并希望您的程序等待足够长的时间才能使其下一步成功,这并不是我正在寻找的解决方案-而且,该方法特定于wkhtml2pdf,并且无法回答一般性问题) .
首选与浏览器无关的解决方案,但至少需要一种可在Chrome上运行的解决方案
解决方法:
如上面的评论中所述,使用诺言是正确的解决方案.但是,承诺会在第一个动画帧完成渲染之前触发.解决方法是,您可以预订诺言,然后使用requestAnimationFrame
等待动画帧完成.
编辑:单个动画框架足以用于Firefox,但不能满足Chrome的需求.因此,我正在编辑使用一对动画帧,这似乎可以解决Chrome中的问题,并且应该可以在Firefox中继续使用.当然,这只是Plotly中问题的解决方法,因为在实际绘制图形之前,诺言才应解决.
这是一个例子:
var trace1 = {
z: [[8.83, 8.89, 8.81, 8.87, 8.9, 8.87], [8.89, 8.94, 8.85, 8.94, 8.96, 8.92], [8.84, 8.9, 8.82, 8.92, 8.93, 8.91], [8.79, 8.85, 8.79, 8.9, 8.94, 8.92], [8.79, 8.88, 8.81, 8.9, 8.95, 8.92], [8.8, 8.82, 8.78, 8.91, 8.94, 8.92], [8.75, 8.78, 8.77, 8.91, 8.95, 8.92], [8.8, 8.8, 8.77, 8.91, 8.95, 8.94], [8.74, 8.81, 8.76, 8.93, 8.98, 8.99], [8.89, 8.99, 8.92, 9.1, 9.13, 9.11], [8.97, 8.97, 8.91, 9.09, 9.11, 9.11], [9.04, 9.08, 9.05, 9.25, 9.28, 9.27], [9, 9.01, 9, 9.2, 9.23, 9.2], [8.99, 8.99, 8.98, 9.18, 9.2, 9.19], [8.93, 8.97, 8.97, 9.18, 9.2, 9.18]],
colorbar: {
xpad: 0,
ypad: 0
},
showscale: false,
type: "surface",
uid: "3f95e8",
zmax: 9.28,
zmin: 8.74
};
var trace2 = {
z: [[9.83, 9.89, 9.81, 9.87, 9.9, 9.87], [9.89, 9.94, 9.85, 9.94, 9.96, 9.92], [9.84, 9.9, 9.82, 9.92, 9.93, 9.91], [9.79, 9.85, 9.79, 9.9, 9.94, 9.92], [9.79, 9.88, 9.81, 9.9, 9.95, 9.92], [9.8, 9.82, 9.78, 9.91, 9.94, 9.92], [9.75, 9.78, 9.77, 9.91, 9.95, 9.92], [9.8, 9.8, 9.77, 9.91, 9.95, 9.94], [9.74, 9.81, 9.76, 9.93, 9.98, 9.99], [9.89, 9.99, 9.92, 10.1, 10.13, 10.11], [9.97, 9.97, 9.91, 10.09, 10.11, 10.11], [10.04, 10.08, 10.05, 10.25, 10.28, 10.27], [10, 10.01, 10, 10.2, 10.23, 10.2], [9.99, 9.99, 9.98, 10.18, 10.2, 10.19], [9.93, 9.97, 9.97, 10.18, 10.2, 10.18]],
opacity: 0.9,
showscale: false,
type: "surface",
uid: "443a24",
zmax: 10.28,
zmin: 9.74
};
var trace3 = {
z: [[7.83, 7.89, 7.81, 7.87, 7.9, 7.87], [7.89, 7.94, 7.85, 7.94, 7.96, 7.92], [7.84, 7.9, 7.82, 7.92, 7.93, 7.91], [7.79, 7.85, 7.79, 7.9, 7.94, 7.92], [7.79, 7.88, 7.81, 7.9, 7.95, 7.92], [7.8, 7.82, 7.78, 7.91, 7.94, 7.92], [7.75, 7.78, 7.77, 7.91, 7.95, 7.92], [7.8, 7.8, 7.77, 7.91, 7.95, 7.94], [7.74, 7.81, 7.76, 7.93, 7.98, 7.99], [7.89, 7.99, 7.92, 8.1, 8.13, 8.11], [7.97, 7.97, 7.91, 8.09, 8.11, 8.11], [8.04, 8.08, 8.05, 8.25, 8.28, 8.27], [8, 8.01, 8, 8.2, 8.23, 8.2], [7.99, 7.99, 7.98, 8.18, 8.2, 8.19], [7.93, 7.97, 7.97, 8.18, 8.2, 8.18]],
opacity: 0.9,
showscale: false,
type: "surface",
uid: "47cf99",
zmax: 8.28,
zmin: 7.74
};
var data = [trace1, trace2, trace3];
var layout = {
autosize: false,
height: 200,
margin: {
r: 0,
t: 0,
autoexpand: false,
b: 0,
l: 0
},
scene: {
aspectmode: "manual",
aspectratio: {
x: 1,
y: 1,
z: 1
},
camera: {
center: {
x: 0,
y: 0,
z: 0
},
eye: {
x: 1.59999582495,
y: 1.57131061558,
z: 0.970117065684
},
up: {
x: 0,
y: 0,
z: 1
}
}
},
width: 650
};
Plotly.plot('plotly-div', data, layout).then(function() {
window.requestAnimationFrame(function() {
window.requestAnimationFrame(function() {
window.alert('Your plot is done.');
});
});
});
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="plotly-div"></div>