gpt4 book ai didi

javascript - plotly 更新数据

转载 作者:可可西里 更新时间:2023-11-01 01:16:31 27 4
gpt4 key购买 nike

好的,我有以下代码:

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

创建以下图表:

enter image description here

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

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

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

然而,当它执行此操作时,图表根本不会改变。控制台没有错误,所以我不太确定该怎么做?

最佳答案

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

关于javascript - plotly 更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32116368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com