Javascript 绘图更新数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32116368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotly update data
提问by Marc Rasmussen
Okay so i have the following code:
好的,所以我有以下代码:
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);
Which creates the following chart:
这将创建以下图表:
Now as you can see ive added a watcher
现在你可以看到我添加了一个 watcher
scope.$watch('divisionId', function (newValue, oldValue) {
if (newValue) {
getData(newValue.id,true);
}
}, true);
Now when i trigger this it should update the chart and call Plotly.redraw(element,charData,layout);
现在,当我触发它时,它应该更新图表并调用 Plotly.redraw(element,charData,layout);
However when it does this the chart does not change at all. There is no error in the console so i am not quite sure what to do?
然而,当它这样做时,图表根本不会改变。控制台中没有错误,所以我不太确定该怎么办?
采纳答案by Marc Rasmussen
I found the answer to the question.
我找到了问题的答案。
Apprently i needed to use:
显然我需要使用:
Plotly.newPlot(element,charData,layout);
instead of redraw
代替 redraw
回答by Honghe.Wu
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.
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
回答by Safwan
According to a Plotly community moderator (see the first answer here), Plotly.restyle
is faster than Plotly.redraw
and Plotly.newPlot
.
根据 Plotly 社区版主的说法(请参阅此处的第一个答案),Plotly.restyle
比Plotly.redraw
和快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]]);
}
回答by Andrei
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 函数应该是您的目标。它可以向您的图形添加数据点并重新绘制它。与重绘(@Honghe.Wu 答案)相反,使用extendTraces 时不需要更新引用。
[extendTraces] This function has comparable performance to Plotly.react and is faster than redrawing the whole plot with Plotly.newPlot.
[extendTraces] 该函数的性能与 Plotly.react 相当,并且比使用 Plotly.newPlot 重新绘制整个图更快。
https://plot.ly/javascript/plotlyjs-function-reference/#plotlyextendtraces
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();
}