Javascript 如何在plotly中为形状添加名称或标签?
我创建了一个简单的plotly线图,如下所示:Javascript 如何在plotly中为形状添加名称或标签?,javascript,plotly,Javascript,Plotly,我创建了一个简单的plotly线图,如下所示: var trace1 = { x: [1, 2, 3, 4], y: [10, 15, 13, 17], type: 'scatter' }; var layout = { xaxis:{ title:"x-axis", tickangle:45, rangemode:'nonnegative', autorange:true, exponentformat: "none" }, yaxis:{ title: "Time", tickangle
var trace1 = {
x: [1, 2, 3, 4],
y: [10, 15, 13, 17],
type: 'scatter'
};
var layout = {
xaxis:{
title:"x-axis",
tickangle:45,
rangemode:'nonnegative',
autorange:true,
exponentformat: "none"
},
yaxis:{
title: "Time",
tickangle:45,
rangemode:'nonnegative',
range:[0,20],
autorange: false
},
shapes: [
{
type: 'line',
xref: 'paper',
x0: 0,
y0: threshold1,
x1: 1,
y1: threshold1,
line:{
color: 'rgb(255, 0, 0)',
width: 2,
dash:'dot'
},
},
{
type: 'line',
xref: 'paper',
x0: 0,
y0: threshold2,
x1: 1,
y1: threshold2,
line:{
color: 'rgb(0, 255, 0)',
width: 2,
dash:'dot'
},
}
]
};
Plotly.newPlot(myDiv,[trace1],layout);
现在,我想给我在“布局”下的“形状”中创建的阈值1和阈值2添加一个名称或标签。我搜索了plotly JS文档,但没有得到任何有用的东西。我怎样才能做到这一点。任何帮助都将不胜感激。将just name属性添加到您的跟踪中
name = 'Xyz'
可能会帮助您解决问题。您可以使用创建另一个跟踪。当然有不止一种方法可以做到这一点,但这种方法很简单,不需要复杂的数据复制
var阈值1=12;
var阈值2=16;
var偏移=0.75;
变量trace1={
x:[1,2,3,4],
y:[10,15,13,17],
键入:“散布”
};
变量trace2={
x:[Math.min.apply(Math,trace1.x)+偏移,
Math.max.apply(Math,trace1.x)-偏移量],
y:[阈值1-偏移,阈值2-偏移],
模式:“文本”,
文本:[“下限”、“上限”],
showlegend:错误
}
变量布局={
xaxis:{
标题:“x轴”,
角度:45,
rangemode:'非负',
自动范围:对,
指数格式:“无”
},
亚克斯:{
标题:“时间”,
角度:45,
rangemode:'非负',
范围:[0,20],
自动范围:false
},
形状:[{
键入:“行”,
外部参照:“纸张”,
x0:0,
y0:阈值1,
x1:1,
y1:阈值1,
行:{
颜色:“rgb(255,0,0)”,
宽度:2,
破折号:“圆点”
},
}, {
键入:“行”,
外部参照:“纸张”,
x0:0,
y0:阈值2,
x1:1,
y1:阈值2,
行:{
颜色:“rgb(0,255,0)”,
宽度:2,
破折号:“圆点”
},
}]
};
Plotly.newPlot(myDiv[trace1,trace2],布局)代码>
您可以使用注释:
layout.annotations = [
{
showarrow: false,
text: "Your text here",
align: "right",
x: 1,
xanchor: "right",
y: threshold1,
yanchor: "bottom"
},
{
showarrow: false,
text: "Your second text here",
align: "right",
x: 1,
xanchor: "right",
y: threshold2,
yanchor: "bottom"
}
],
将name属性添加到跟踪会为折线图命名。我想做的是给在布局中创建的阈值线命名,在ShapesTanks下,这就是我想要的。