【问题标题】:Plotly update data绘制更新数据
【发布时间】:2015-11-13 23:38:05
【问题描述】:

好的,我有以下代码:

    var element = document.getElementById(scope.changeid);

function getData(division,redraw) {
    var employeeData = [];
    if (!division) {
        $http.get(api.getUrl('competenceUserAverageByMyDivisions', null)).success(function (response) {
            processData(response,redraw);
        });
    }
    else {
        $http.get(api.getUrl('competenceUserAverageByDivision', division)).success(function (response) {
            processData(response,redraw);
        })
    }

}

function processData(data,redraw) {
    var y = [],
        x1 = [],
        x2 = [];

    data.forEach(function (item) {
        y.push(item.user.profile.firstname);
        x1.push(item.current_level);
        x2.push(item.expected);
    });

    var charData = [{
            x: x1,
            y: y,
            type: 'bar',
            orientation: 'h',

            name: 'Nuværende'
        }, {
            x: x2,
            y: y,
            type: 'bar',
            orientation: 'h',

            name: 'Forventet'
        }],
        layout = {
            barmode: 'stack',
            legend: {
                traceorder: 'reversed',
                orientation: 'h'

            }
        };

    if(!redraw){
        Plotly.plot(element, charData, layout);
    }
    else
    {
        Plotly.redraw(element,charData,layout);
    }
}

scope.$watch('divisionId', function (newValue, oldValue) {
    if (newValue) {
        getData(newValue.id,true);
    }
}, true);

getData(null,false);

创建以下图表:

现在你可以看到我添加了watcher

            scope.$watch('divisionId', function (newValue, oldValue) {
            if (newValue) {
                getData(newValue.id,true);
            }
        }, true);

现在当我触发它时,它应该更新图表并调用Plotly.redraw(element,charData,layout);

但是,当它这样做时,图表根本不会改变。控制台没有错误,所以我不太确定该怎么办?

【问题讨论】:

  • 你在 Angular 指令中使用 plotly.js 吗?

标签: javascript angularjs plotly


【解决方案1】:

Plotly.redraw(gd) 是正确的方法。
但是你打错了Plotly.redraw
正确的方法是更新data 对象,而不是新的data 对象。

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');
}

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

【讨论】:

  • 链接失效
【解决方案2】:

我找到了问题的答案。

显然我需要使用:

 Plotly.newPlot(element,charData,layout);

而不是redraw

【讨论】:

【解决方案3】:

根据 Plotly 社区版主的说法(见第一个答案 here),Plotly.restylePlotly.redrawPlotly.newPlot 快​​。

示例取自链接:

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]]);
}

【讨论】:

    【解决方案4】:

    extendTraces 函数应该是您的目标。它可以将数据点添加到您的图表并重新绘制它。与重绘(@Honghe.Wu 回答)相比,使用 extendTraces 时不需要更新引用。

    [extendTraces] 此函数的性能与 Plotly.react 相当,并且比使用 Plotly.newPlot 重绘整个绘图更快。

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

    示例用法

    // 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();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 2019-08-06
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      相关资源
      最近更新 更多