gpt4 book ai didi

javascript - 当传递一组轨迹时,Plot.ly 不呈现

转载 作者:行者123 更新时间:2023-11-30 20:26:28 24 4
gpt4 key购买 nike

下面的代码无法呈现数据。没有异常被抛出。但是,如第二个代码段所示,仅给出两条轨迹之一时,相同的代码确实有效。唯一的区别是调用

    Plotly.newPlot(target_target, traces); // doesn't render

代替:

    Plotly.newPlot(target_target, trace0); // trace1 also works.

我是一个 javascript 菜鸟,所以我的 traces 数组可能有问题,但它看起来像我查看的示例代码。

不起作用的代码(完整):

<html>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id='target' style="width:600px;height:450px;"></div>
<script>
var target_target = document.getElementById('target');

var trace0 =
[{
x: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
y: [0.5, 1.5, 6.5, 14.5, 25.5, 39.5],
type: 'scatter'
}];

var trace1 =
[{
x: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
y: [0.0, 1.0, 6.0, 14.0, 25.0, 39.0],
type: 'scatter'
}];

var traces = [ trace0, trace1 ];
Plotly.newPlot(target_target, traces);

</script>
</body>
</html>

有效的代码(完整):

<html>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id='target' style="width:600px;height:450px;"></div>
<script>
var target_target = document.getElementById('target');

var trace1 =
[{
x: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
y: [0.0, 1.0, 6.0, 14.0, 25.0, 39.0],
type: 'scatter'
}];

Plotly.newPlot(target_target, trace1);

</script>
</body>
</html>

最佳答案

你犯的错误只是个人痕迹应该是 objects不是array of a single object ,您需要做的就是进行此更正。

之前:

var trace0 =
[{
x: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
y: [0.5, 1.5, 6.5, 14.5, 25.5, 39.5],
type: 'scatter'
}];

之后:

var trace0 =
{
x: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
y: [0.5, 1.5, 6.5, 14.5, 25.5, 39.5],
type: 'scatter'
};

然后需要将这些单独的痕迹(对象)分组到一个数组中并在 plotly 中设置。

var traces = [ trace0, trace1 ];
Plotly.newPlot(target_target, traces);

这就是第二个示例起作用的原因,因为跟踪是作为对象数组接收的。

请引用以下示例并检查您的问题是否已解决!

var target_target = document.getElementById('target');

var trace0 = {
x: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
y: [0.5, 1.5, 6.5, 14.5, 25.5, 39.5],
type: 'scatter'
};

var trace1 = {
x: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
y: [0.0, 1.0, 6.0, 14.0, 25.0, 39.0],
type: 'scatter'
};

var traces = [trace0, trace1];
Plotly.newPlot(target_target, traces);
<html>

<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>

<body>
<div id='target' style="width:600px;height:450px;"></div>
</body>

</html>

关于javascript - 当传递一组轨迹时,Plot.ly 不呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50860653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com