Javascript 从plotly.js plot中删除所有记录道

Javascript 从plotly.js plot中删除所有记录道,javascript,plotly,Javascript,Plotly,我有一个包含plot.ly plot的页面,我想将数据写入其中几次,覆盖以前的内容 我找不到从plotly.js绘图中删除所有记录道的方法,只需重新填充即可添加数据而不删除旧的记录道。有两种方法可以做到这一点,两种方法都在列表中列出 选项1(告诉plotly删除有问题的跟踪): 其中第二个参数是要删除的跟踪的跟踪索引。请注意,第二个参数也可以是一个索引数组,允许您一次删除多个记录道 选项2(告诉plotly使用新数据进行新绘图): 其中参数与Plotly.plot的参数相同。这将仅根据第二个参数

我有一个包含plot.ly plot的页面,我想将数据写入其中几次,覆盖以前的内容


我找不到从plotly.js绘图中删除所有记录道的方法,只需重新填充即可添加数据而不删除旧的记录道。

有两种方法可以做到这一点,两种方法都在列表中列出

选项1(告诉plotly删除有问题的跟踪):

其中第二个参数是要删除的跟踪的跟踪索引。请注意,第二个参数也可以是一个索引数组,允许您一次删除多个记录道

选项2(告诉plotly使用新数据进行新绘图):


其中参数与Plotly.plot的参数相同。这将仅根据第二个参数中发送的数据创建新的打印图形。更准确地说,
Plotly.newPlot
是幂等的,而
Plotly.plot
不是。

您可以删除最后一个跟踪和上一个跟踪,直到最后一个跟踪:

while(graphDiv.data.length>0)
{
      Plotly.deleteTraces(graphDiv, [0]);
}

Newplot、Purge+Newplot、react和delete跟踪都不工作。需要一个更好的“清除”功能,直接从plotly获得支持。

清理图表是不够的,还必须清理数据

以下是我绘制散点图的方式:

function tracciaGrafico() {
    data = [];
    trace = [];
    indexTrace = 0;

    // Cleanup only if not first start (else it is already clean, and this would raise an error):
    if (FirstStart) {
        FirstStart = false;
    } else { // Clear chart before plotting if it has already peing plotted.
        Plotly.deleteTraces('myDiv', clearData);
        clearData = [];
    }


    Squadre.forEach(function(squadra) {
        trace[indexTrace] = puntiSquadraCumulativi[squadra]; // Copy trace data from my source array
        data.push(
            {
                x: xArray, 
                y: puntiSquadraCumulativi[squadra], 
                type: "scatter", 
                name: squadra, // from "forEach"
                line: {width: 5}
            }
        ); // Add trace to data array
        clearData.push(indexTrace); // Add trace index to array I will use to clean the old chart
        indexTrace++;
    });
    Plotly.newPlot('myDiv', data); // Plot data
}
在构建图表时,我构建了一个数组(clearData),然后我可以使用它来清除图表:它只包含所有记录道的所有索引,它是Plotly.deleteTraces('myDiv',clearData)的参数

但也许您实际上不想删除跟踪,而只是更新跟踪:


谢谢。我看到了deleteTraces,但绘图上的跟踪数量可变,因此无法找到填充跟踪索引数组的简单方法。此外,我还尝试了用
newPlot
代替
plot
,但跟踪仍在添加,而不是替换,并且没有更新绘图标题。
newPlot
是生成新的绘图,还是删除所有记录道并添加新的记录道?试图找出有关
newPlot
的错误之处时,出现了一个bug,它影响了使用字符串id作为第一个参数的调用签名。更新版本将很快推出。给您带来不便,我们深表歉意。同时,`Plotly.newPlot(document.getElementById('plot-id'),data,layout)应该可以工作。该链接无效选项1不起作用。。只删除一个跟踪。即使删除了所有痕迹,仍保留标题等。使用选项2better,但不删除标题和轴等。。如上所述使用newPlot
while(graphDiv.data.length>0)
{
      Plotly.deleteTraces(graphDiv, [0]);
}
function tracciaGrafico() {
    data = [];
    trace = [];
    indexTrace = 0;

    // Cleanup only if not first start (else it is already clean, and this would raise an error):
    if (FirstStart) {
        FirstStart = false;
    } else { // Clear chart before plotting if it has already peing plotted.
        Plotly.deleteTraces('myDiv', clearData);
        clearData = [];
    }


    Squadre.forEach(function(squadra) {
        trace[indexTrace] = puntiSquadraCumulativi[squadra]; // Copy trace data from my source array
        data.push(
            {
                x: xArray, 
                y: puntiSquadraCumulativi[squadra], 
                type: "scatter", 
                name: squadra, // from "forEach"
                line: {width: 5}
            }
        ); // Add trace to data array
        clearData.push(indexTrace); // Add trace index to array I will use to clean the old chart
        indexTrace++;
    });
    Plotly.newPlot('myDiv', data); // Plot data
}