forked from boeric/d3RealTimeChartMulti
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runRealTimeChart.js
52 lines (37 loc) · 1.56 KB
/
runRealTimeChart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
// ----------------------------------------------------------------------------
// configure the real time chart
// ----------------------------------------------------------------------------
// create the real time chart
const chart = initRealTimeChart();
// invoke the real time chart
d3.select("#viewDiv").append("div").attr("id", "chartDiv").call(chart);
// ----------------------------------------------------------------------------
// configure the data generator
// ----------------------------------------------------------------------------
// in a normal use case, real time data would arrive through the network or some other mechanism
// the timeout units is milliseconds (1000 ms = 1 sec), one new data point is sent to the real
// time chart after every timeout
const timeout = 100;
// define data generator
function dataGenerator() {
setTimeout(function() {
// control flags for debugging and testing, toggled with checkboxes
const debug = $("#debug").is(":checked");
const halted = $("#halt").is(":checked");
// if the real time data chart is paused, stop generating data until it's un-paused
if (!halted) {
// generate simulated data
let obj = {
sensorReading: Math.ceil(50 + (15 * Math.random())),
index: 0
};
if (debug) console.log("simulated sensor reading: ", obj);
chart.insertData(obj);
}
// do forever
dataGenerator();
}, timeout);
}
// start the data generator
dataGenerator();