Plotly update data

柔情痞子 提交于 2019-11-30 11:13:16

Plotly.redraw(gd) is the right way.
But you call Plotly.redraw incorrectly.
The right way is update the data object, instead of new a data object.

var data = [ {
    x: ['VALUE 1'], // in reality I have more values... 
    y: [20], 
    type: 'bar'
}
]; 
Plotly.newPlot('PlotlyTest', data); function adjustValue1(value) {
    data[0]['y'][0] = value; 
    Plotly.redraw('PlotlyTest');
}

Ref: http://www.mzan.com/article/35946484-most-performant-way-to-update-graph-with-new-data-with-plotly.shtml

I found the answer to the question.

Apprently i needed to use:

 Plotly.newPlot(element,charData,layout);

instead of redraw

According to a Plotly community moderator (see the first answer here), Plotly.restyle is faster than Plotly.redraw and Plotly.newPlot.

Example taken from the link:

var data = [{
    x: ['VALUE 1'], // in reality I have more values...
    y: [20],
    type: 'bar'
}];
Plotly.newPlot('PlotlyTest', data);

function adjustValue1(value)
{
    Plotly.restyle('PlotlyTest', 'y', [[value]]);
}

The extendTraces function should be what you are aiming for. It can add data points to your graph and redraws it. In contrast to redraw (@Honghe.Wu Answer), you do not need to update the reference when using extendTraces.

[extendTraces] This function has comparable performance to Plotly.react and is faster than redrawing the whole plot with Plotly.newPlot.

https://plot.ly/javascript/plotlyjs-function-reference/#plotlyextendtraces

Example usage

// initialise some data beforehand
var y = [];
for (var i = 0; i < 20; i ++) {
    y[i] = Math.random();
}

var trace = {
    // x: x,
    y: y,
    type: 'bar',
  };
var data = [trace];
// create the plotly graph
Plotly.newPlot('graph', data);

setInterval(function() {
  // add data to the trace via function call
  Plotly.extendTraces('graph', { y: [[getData()]] }, [0]);
  // y.push(getData()); Plotly.redraw('graph'); //similar effect
}, 400);

function getData() {
     return Math.random();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!