简体   繁体   English

C3.js实现实时数据可视化

[英]real time data visualization with C3.js

I am new to javascript and want to use C3.js for streaming time series data visualization. 我是javascript新手,想使用C3.js进行流时间序列数据可视化。 I want to display the 5 most recent data points on the chart. 我想在图表上显示5个最新数据点。 The new data point is streamed every 2 seconds. 新数据点每2秒传输一次。 So, the display should discard the oldest data point and refresh the graph with this additional data point. 因此,显示应该丢弃最旧的数据点,并使用该附加数据点刷新图形。

Example: x axis has time in seconds: 示例:x轴的时间以秒为单位:

xData = [ 0, 2, 4, 6, 8] 
yData = [10, 11, 12, 13, 14]

The new data point (arriving after 2 seconds) would be x = 10, y = 16 新的数据点(2秒后到达)将为x = 10, y = 16
So, I want to update the xData and yData arrays as, 因此,我想将xData和yData数组更新为

xData = [ 2, 4, 6, 8, 10] 
yData = [ 11, 12, 13, 14, 16]

I looked at the 'flow' API example of C3.js. 我看了C3.js的“流程” API 示例 But it appears like a new array is created everytime. 但是似乎每次都创建一个新数组。

Could someone please illustrate how to accomplish this in c3? 有人可以说明如何在c3中完成此操作吗? I am using node.js. 我正在使用node.js。

Initialize your initial chart with the data you want. 使用所需的数据初始化初始图表。 Let's say: 比方说:

var data = [
    ['x', 0, 2, 4, 6, 8],
    ['data1', 10, 11, 12, 13, 14],
];

Add data, restrict to last 5 elements, and reflow the chart: 添加数据,限制为最后5个元素,然后重排图表:

data[0].push(10);
data[1].push(16);

for ( var i=0; i<data.length; i++ ) {
    //Restrict to last 5 elements (Preserves first element)
    data[i] = [data[i][0]].concat(data[i].slice(1).slice(-5));
}

/*
 * Data now looks like this:
[
    ["x", 2, 4, 6, 8, 10],
    ["data1", 11, 12, 13, 14, 16]
]
 *
 */

//Reflow chart with new data

Well, if you don't care about prior data points you could simply do this: 好吧,如果您不关心先前的数据点,则只需执行以下操作:

success: function(data, textStatus, xhr) { 
  xData.shift();
  yData.shift();
  xData.push(data.x);
  yData.push(data.y);
  // now you can recall the chart function to use the new data
}

Also if you want to keep a record of past data points you can always record the shifted off number by doing something like historicalXpointArray.push(xData.shift()); 同样,如果您想记录过去的数据点,则可以始终通过执行诸如historyXpointArray.push(xData.shift())之类的方法来记录移位的数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM