Javascript和人力车。如何更新实时流媒体?

时间:2013-03-10 19:21:40

标签: javascript rickshaw

如何使用实时流式更新人力车数据?

我按照以下方式启动图表。

for (var i = 0; i < json_data.length; i++) {
    meta = json_data[i];
    seriesJsonData.push({color: palette.color(),data: meta['series'],name: meta['name']});
}
var graph = new Rickshaw.Graph( {
    element: document.getElementById("chart"),
    width: 900,
    height: 500,
    renderer: 'area',
    stroke: true,
    series: seriesJsonData
} );

然后我想回电话以获取此格式的数据。

//new data to plot.
setInterval( function() {
    // data = mycallback('ajax_time_series/');
    data = {y: 0.013342943046016261, x: 1362914220};
    graph.series.addData(data);
    graph.update();
}, 3000 );

json_data = [{"series": [{y: 0.013342943046016261, x: 1362914220}, {y: 0.03332711471721182, x: 1362914280}, {y: 0.01995987637052034, x: 1362914340}, {y: 0.02667808501229039, x: 1362914400}, {y: 0.026667576407988548, x: 1362914460}, {y: 0.02666090465447435, x: 1362914520}, {y: 0.039902022061890564, x: 1362914580}, {y: 0.040065222851502304, x: 1362914640}, {y: 0.026725544622339118, x: 1362914700}, {y: 0.03328597880035014, x: 1362914760}, {y: 0.020025583265634844, x: 1362914820}, {y: 0.013351483328567889, x: 1362914880}, {y: 0.013327807174645642, x: 1362914940}, {y: 0.03327930933245091, x: 1362915000}, {y: 0.060011496243413356, x: 1362915060}, {y: 0.026726823599964183, x: 1362915120}, {y: 0.06625748681407234, x: 1362915180}, {y: 0.08716853007016215, x: 1362915240}, {y: 0.019958925851731625, x: 1362915300}, {y: 0.026628071716283376, x: 1362915360}, {y: 0.053539335295842476, x: 1362915420}, {y: 0.013329419200217766, x: 1362915480}, {y: 0.020005110867173173, x: 1362915540}, {y: 0.03985835992510624, x: 1362915600}, {y: 0.04009389940367332, x: 1362915660}, {y: 0.020015945088387825, x: 1362915720}, {y: 0.02661011443819408, x: 1362915780}, {y: 0.06656881334811096, x: 1362915840}, {y: 0.04690556277785193, x: 1362915900}, {y: 0.03326433994636323, x: 1362915960}, {y: 0.02670133346693278, x: 1362916020}, {y: 0.033335701795225714, x: 1362916080}, {y: 0.020001391989344027, x: 1362916140}, {y: 0.02000903195967164, x: 1362916200}, {y: 0.013308784414422375, x: 1362916260}, {y: 0.058279492803009744, x: 1362916320}, {y: 0.034243644495436376, x: 1362916380}, {y: 0.020081534665136593, x: 1362916440}, {y: 0.026653921622562717, x: 1362916500}, {y: 0.0199648119499449, x: 1362916560}, {y: 0.03338215889686262, x: 1362916620}, {y: 0.04659716637539024, x: 1362916680}, {y: 0.033199969663029336, x: 1362916740}, {y: 0.020053008656297493, x: 1362916800}, {y: 0.020083574882737414, x: 1362916860}, {y: 0.013338712371294185, x: 1362916920}, {y: 0.013285462289789874, x: 1362916980}, {y: 0.03342980523147545, x: 1362917040}, {y: 0.013318560614522242, x: 1362917100}, {y: 0.020054761094019935, x: 1362917160}, {y: 0.02649323226604952, x: 1362917220}, {y: 0.08046447455222104, x: 1362917280}, {y: 0.033380214683152935, x: 1362917340}, {y: 0.01998459181555401, x: 1362917400}, {y: 0.11263974301203006, x: 1362917460}, {y: 0.03351645707155687, x: 1362917520}, {y: 0.02667373118205504, x: 1362917580}, {y: 0.020004237840478896, x: 1362917640}, {y: 0.0867493421006396, x: 1362917700}], "name": "cpuUsage"}];

我将有多个系列要更新。我的图表没有更新,我不理解要更新的文档。

另外......我不是用人力车做的。是否有一个具有良好文档的实时流式JS库?人力车看起来很漂亮,但实时记录很少。

2 个答案:

答案 0 :(得分:3)

我想让您了解我的解决方法:

  1. 保存图表div的内部html供以后使用(在图表渲染后,内部html被更改)

  2. 添加数据时,请将其添加到稍后可以访问的数组中。

  3. 使用数据数组正常渲染图表。

  4. 当您需要添加数据时,将数据添加到包含数据的数组中,并从头开始重新生成图表。只有在再次渲染之前再将原始html插回到图表div中时,才能执行此操作。

  5. 这适用于每5秒更新8个图表,每个图表有20个数据点。它几乎不占用任何资源。如果你增加数据点的数量或使用高级渲染选项,我相信它会需要很多。

    我应该为此获得诺贝尔奖。

答案 1 :(得分:2)

对于那些落后的人来说,FixedDuration series可能就是你想要的。

除了this example demonstrating real time updates with a fixed window之外,它似乎没有记录在任何地方,只有it's been around since before the v1.1 release in August 2012

以下是在他们的例子中生成情节的代码:

var tv = 250;

// instantiate our graph!
var graph = new Rickshaw.Graph( {
    element: document.getElementById("chart"),
    width: 900,
    height: 500,
    renderer: 'line',
    series: new Rickshaw.Series.FixedDuration([{ name: 'one' }], undefined, {
        timeInterval: tv,
        maxDataPoints: 100,
        timeBase: new Date().getTime() / 1000
    }) 
} );

graph.render();

// add some data every so often

var i = 0;
var iv = setInterval( function() {

    var data = { one: Math.floor(Math.random() * 40) + 120 };

    var randInt = Math.floor(Math.random()*100);
    data.two = (Math.sin(i++ / 40) + 4) * (randInt + 400);
    data.three = randInt + 300;

    graph.series.addData(data);
    graph.render();

}, tv );