function getData() {
return Math.random();
};
function plotGraph(graph_div) {
let UPDATE_INTERVAL = 300;
Plotly.plot(graph_div,[{
y: [1,2,3].map(getData),name: 'x',mode: 'lines',line: { color: '#80CAF6' }
},{
y: [1,name: 'y',line: { color: '#DF56F1' }
},name: 'z',line: { color: '#4D92E9' }
}]);
var cnt = 0;
var interval = setInterval(function () {
var time = new Date();
Plotly.extendTraces(graph_div,{
y: [[getData()],[getData()],[getData()]]
},[0,1])
cnt = cnt+1;
if (cnt === 100) clearInterval(interval);
},UPDATE_INTERVAL);
}
错误:
plotly-latest.min.js:7 Uncaught Error: attribute y must be an array of length equal to indices array length
at plotly-latest.min.js:7
at R (plotly-latest.min.js:7)
at Object.t [as extendTraces] (plotly-latest.min.js:7)
at realtime_vis.js:40
指向
Plotly.extendTraces(graph_div,1])
官方文档中的示例仅显示了如何绘制2条线,但该示例不适用于3条线.
有什么帮助吗?我假设我可以显式指定数组的大小?!
最佳答案
Plotly文档并不清楚here,但是第三个参数是要修改的打印索引数组.
在您的情况下,您要告诉Plotly修改[0,1],但要提供3个新的y值.如果将其更改为[0,1,2],它应该可以工作,或者您只能提供两个新的y值.
function getData() {
return Math.random();
};
Plotly.plot(graph_div,[{
y: [1,line: { color: '#80CAF6' }
},{
y: [1,line: { color: '#DF56F1' }
},line: { color: '#4D92E9' }
}]);
var cnt = 0;
var interval = setInterval(function () {
var time = new Date();
Plotly.extendTraces(graph_div,{
y: [[getData()],[getData()]]
},2])
cnt = cnt+1;
if (cnt === 100) clearInterval(interval);
},300);
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="graph_div"></div>
</body>