gpt4 book ai didi

javascript - 实时更新人力车图

转载 作者:数据小太阳 更新时间:2023-10-29 05:48:19 24 4
gpt4 key购买 nike

我正在尝试使用 Rickshaw 创建一个漂亮的占位符图表,显示随机数据的实时更新,如下所示:

var series = [[], []];
var random = new Rickshaw.Fixtures.RandomData(150);

for(var i = 0; i < 80; i++) {
random.addData(series);
}

var throughput = new Rickshaw.Graph( {
element: document.querySelector("#throughput_chart"),
width: "300",
height: "200",
renderer: "line",
series: [{
color: "gold",
data: series[0]
}]
});

var alerts = new Rickshaw.Graph( {
element: document.querySelector("#alerts_chart"),
width: "300",
height: "200",
renderer: "line",
series: [{
color: "red",
data: series[1]
}]
});

throughput.render();
alerts.render();

setInterval(function() {
random.addData(series);
throughput.update();
alerts.update();
/* XXX: This causes the data to stop drawing properly
series.forEach(function(s) {
s.shift();
});
*/
}, 1000);

这有效,除了注释掉的部分。事实上,它不断添加数据,这意味着图表变得越来越拥挤。我希望每次在末尾添加一个新项目时都能够删除第一个项目,以便使图形具有相同数量的数据点,但是当我将该代码放入其中时,很快就会导致图形不显示任何数据,即使 console.log 显示数组仍然充满数据。

我怎样才能让它工作,以便我一次只显示固定数量的数据点?

最佳答案

在 Rickshaw ( http://code.shutterstock.com/rickshaw/examples/fixed.html ) 文档中的例子的引导下,我拼凑了一些与您需要的类似的东西:

JS FIDDLE

var tv = 50;

var throughput = new Rickshaw.Graph({
element: document.querySelector("#throughput_chart"),
width: "300",
height: "200",
renderer: "line",
series: new Rickshaw.Series.FixedDuration([{
name: 'one', color: 'gold'
}], undefined, {
timeInterval: tv,
maxDataPoints: 100,
timeBase: new Date().getTime() / 1000
})
});

var alerts = new Rickshaw.Graph({
element: document.querySelector("#alerts_chart"),
width: "300",
height: "200",
renderer: "line",
series: new Rickshaw.Series.FixedDuration([{
name: 'one', color: 'red'
}], undefined, {
timeInterval: tv,
maxDataPoints: 100,
timeBase: new Date().getTime() / 1000
})
});

for(var i = 0; i < 100; i++){
addRandomData(throughput);
addRandomData(alerts);
}

throughput.render();
alerts.render();

setInterval(function () {

addRandomData(throughput);
addRandomData(alerts);
throughput.render();
alerts.render();

}, tv);

function addRandomData(chart) {
var data = {
one: Math.floor(Math.random() * 40) + 120
};
chart.series.addData(data);
}

关于javascript - 实时更新人力车图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16944128/

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