Plotly.extendTraces仅适用于两条轨迹,但不适用于三条轨迹

时间:2019-04-21 15:55:21

标签: javascript charts plotly

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, 2, 3].map(getData),
    name: 'y',
    mode: 'lines',
    line: { color: '#DF56F1' }
  }, {
    y: [1, 2, 3].map(getData),
    name: 'z',
    mode: 'lines',
    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, {
      y: [[getData()], [getData()], [getData()]]
    }, [0, 1])

官方文档中的示例仅显示了如何绘制2条线,但该示例不适用于3条线。 有什么帮助吗?我假设我可以显式指定数组的大小?!

1 个答案:

答案 0 :(得分:2)

Plotly文档并不清楚here,但是第三个参数是要修改的打印索引数组。

在您的情况下,您要告诉Plotly修改[0, 1],但要提供3个新的y值。如果将其更改为[0, 1, 2],它应该可以工作,或者您只能提供两个新的y值。

function getData() {
  return Math.random();
};

Plotly.plot(graph_div, [{
	y: [1, 2, 3].map(getData),
	name: 'x',
	mode: 'lines',
	line: { color: '#80CAF6' }
}, {
	y: [1, 2, 3].map(getData),
	name: 'y',
	mode: 'lines',
	line: { color: '#DF56F1' }
}, {
	y: [1, 2, 3].map(getData),
	name: 'z',
	mode: 'lines',
	line: { color: '#4D92E9' }
}]);

var cnt = 0;

var interval = setInterval(function () {
	var time = new Date();

	Plotly.extendTraces(graph_div, {
		y: [[getData()], [getData()], [getData()]]
	}, [0, 1, 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>