Plotly更新数据

Mar*_*sen 18 javascript angularjs plotly

好的,所以我有以下代码:

    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);
Run Code Online (Sandbox Code Playgroud)

这会创建以下图表:

在此输入图像描述

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

            scope.$watch('divisionId', function (newValue, oldValue) {
            if (newValue) {
                getData(newValue.id,true);
            }
        }, true);
Run Code Online (Sandbox Code Playgroud)

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

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

Hon*_*.Wu 16

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');
}
Run Code Online (Sandbox Code Playgroud)

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

  • 链接坏了 (4认同)

Mar*_*sen 13

我找到了问题的答案.

显然我需要使用:

 Plotly.newPlot(element,charData,layout);
Run Code Online (Sandbox Code Playgroud)

代替 redraw

  • 这可行,但可能不是最好的解决方案,必须有另一种方式. (4认同)
  • 嗯... http://www.mzan.com/article/35946484-most-performant-way-to-update-graph-with-new-data-with-plotly.shtml (2认同)
  • @ChristopheRoussy链接已死。 (2认同)