artdaq_node_server  v1_01_01c
MakeGraph.js
1 var n = 120, //Displayed time range is n * duration, so default is 60 seconds.
2  duration = 500; //ms between updates
3 
4 function tick(paths, line, axes, x, y, ids, tag) {
5  var transition = d3.select(tag).transition()
6  .duration(duration)
7  .ease("linear");
8  transition = transition.each(function () {
9 
10  // update the domains
11  now = new Date();
12  x.domain([now - (n - 1) * duration, now]);
13 
14  var extent = [0x1000000, 0];
15  for (var name in ids) {
16  var group = ids[name];
17  var thisExtent = d3.extent(group.data, function (d) { return d.value; });
18  if (thisExtent[0] < extent[0]) { extent[0] = thisExtent[0]; }
19  if (thisExtent[1] > extent[1]) { extent[1] = thisExtent[1]; }
20  }
21  y.domain([extent[0] * 98 / 100, extent[1] * 102 / 100]);
22 
23  for (var name in ids) {
24  d3.json(ids[name].jsonPath, function (json) {
25  ids[json.name].data.push({ value: json.value, time: new Date(json.time) });
26  });
27 
28  // redraw the line
29  ids[name].path.attr("d", line).attr("transform", null);
30  }
31 
32  // slide the x-axis left
33  axes[0].call(x.axis);
34  axes[1].call(y.axis);
35 
36  for (var name in ids) {
37  ids[name].path
38  .transition()
39  .duration(duration)
40  .ease("linear")
41  .attr("transform", "translate(" + x(now - n * duration) + ")");
42  }
43 
44  for (var name in ids) {
45  while (ids[name].data.length > 0 && ids[name].data[0].time < now - n * duration) {
46  ids[name].data.shift();
47  }
48  }
49 
50  }).transition().each("start", function () { setTimeout(tick(paths, line, axes, x, y, ids, tag), (duration * 3) / 4) });
51 }
52 
53 function makeGraph(tag, ids) {
54  var now = new Date(Date.now() - duration);
55 
56  var margin = { top: 6, right: 20, bottom: 20, left: 60 },
57  width = $(tag).width() - margin.right - margin.left,
58  height = 120 - margin.top - margin.bottom;
59 
60  var x = d3.time.scale()
61  .range([0, width]);
62 
63  var y = d3.scale.linear()
64  .range([height, 0]);
65 
66  var line = d3.svg.line()
67  .interpolate("linear")
68  .x(function (d) { return x(d.time); })
69  .y(function (d) { return y(d.value); });
70 
71  var svg = d3.select(tag).append("svg")
72  .attr("width", width + margin.left + margin.right)
73  .attr("height", height + margin.top + margin.bottom)
74  .style("margin-left", -margin.right + "px")
75  .append("g")
76  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
77 
78  svg.append("defs").append("clipPath")
79  .attr("id", "clip" + tag.replace('#', ""))
80  .append("rect")
81  .attr("width", width)
82  .attr("height", height);
83 
84  var xaxis = svg.append("g")
85  .attr("class", "x axis")
86  .attr("transform", "translate(0," + height + ")")
87  .call(x.axis = d3.svg.axis().scale(x).orient("bottom"));
88 
89  var formatTick = function (d) {
90  var prefix = d3.formatPrefix(d);
91  return prefix.scale(d) + " " + prefix.symbol + "B/s";
92  }
93  var yaxis = svg.append("g")
94  .attr("class", "y axis")
95  .attr("transform", "translate(0,0)")
96  .call(y.axis = d3.svg.axis().scale(y).tickFormat(formatTick).orient("left"));
97 
98  var paths = svg.append("g")
99  .attr("clip-path", "url(#clip" + tag.replace('#', "") + ")");
100 
101  for (var name in ids) {
102  ids[name].path = paths.append('path')
103  .data([ids[name].data])
104  .attr('class', 'line')
105  .style('stroke', ids[name].color);
106  }
107 
108  tick(paths, line, [xaxis, yaxis], x, y, ids, tag);
109 }