javascript - 在 Plotly 中使用循环制作形状
问题描述
到目前为止,我得到了这段代码`
shapes: [
{
type: "line",
xref: "x",
yref: "paper",
x0: data[i],
y0: 0,
x1: data[i],
y1: 1,
line: {
color: "red",
width: 2,
dash: "longdash",
},
},
],
我想在我的情节中的特定位置设置一条垂直线,这很好用。现在我怎样才能做更多完全相同的行?导致我的数据数组将在一个按钮后更新。如果我的数组中只有一个点,我会得到一条线,但如果尝试设置第二条线,它将无法正常工作。我可以以某种方式制作一个 for 循环来绘制这些线条吗?我只想在不同的地方绘制多个垂直车道。
解决方案
您可以构造shapes
为包含多个字典的数组:形式为[{shape 1 info}, ..., {shape N info}]
. 为此,您可以遍历data
数组,并将data[i]
其用作数组中每一行的 x 坐标shapes
。
这是一些示例代码和codepen。
var data = [1,2,3,5]
shapes = []
for (let i = 0; i < data.length; i++) {
shapes.push({
type: "line",
xref: "x",
yref: "paper",
x0: data[i],
y0: 0,
x1: data[i],
y1: 1,
line: {
color: "red",
width: 2,
dash: "longdash",
},
})
}
var trace1 = {
x: [2, 6],
y: [1, 2],
mode: 'markers+lines'
};
var layout = {
title: 'Vertical Line Annotations',
xaxis: {
range: [0, 7]
},
yaxis: {
range: [0, 6]
},
shapes: shapes
};
var plotdata = [trace1];
Plotly.newPlot('myDiv', plotdata, layout);
推荐阅读
- android - Kotlin 房间数据库单例模式
- python - 使用更多方向的最大路径和问题的变化
- python - 计算行组的平均值
- javascript - Blazor JSInterop 间歇性失败 - 找不到“”
- vb.net - 在 VB.NET 中使用 2 个组合框填充数据网格视图
- c - 为什么 fread 读取文件中的字节虽然我没有指出接下来要读取的字节是什么?
- python - matplotlib 分组条形图和折线图未与 x 轴对齐
- typescript - TypeScript 泛型 - 期望类型参数是枚举,并引用它的键
- azure - 如何从 Azure 数据存储容器中获取文件夹 ID?
- python - 从 pyspark 中的列表创建数据框