-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathsliding_window.html
More file actions
89 lines (73 loc) · 2.04 KB
/
sliding_window.html
File metadata and controls
89 lines (73 loc) · 2.04 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<html>
<head>
<title>Sliding Windows Example</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="../dist/Bacon.js"></script>
<style>
.chart rect {
fill: steelblue;
stroke: white;
}
</style>
</head>
<body>
<script>
// The d3 setup. Skip further down for the bacon-powered bit
// http://mbostock.github.com/d3/tutorial/bar-2.html
var d_len = 33,
w = 20,
h = 80;
var x = d3.scale.linear()
.domain([0, 1])
.range([0, w]);
var y = d3.scale.linear()
.domain([0, 100])
.rangeRound([0, h]);
var chart = d3.select("body").append("svg")
.attr("class", "chart")
.attr("width", w * d_len - 1)
.attr("height", h);
chart.append("line")
.attr("x1", 0)
.attr("x2", w * d_len)
.attr("y1", h - 0.5)
.attr("y2", h - 0.5)
.style("stroke", "#000");
function redraw(data) {
var rect = chart.selectAll("rect")
.data(data, function(d) { return d.time; });
rect.enter().insert("rect", "line")
.attr("x", function(d, i) { return x(i + 1) - .5; })
.attr("y", function(d) { return h - y(d.value) - .5; })
.attr("width", w)
.attr("height", function(d) { return y(d.value); })
.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; });
rect.transition()
.duration(1000)
.attr("x", function(d, i) { return x(i) - .5; });
rect.exit().transition()
.duration(1000)
.attr("x", function(d, i) { return x(i - 1) - .5; })
.remove();
}
// The Bacon awesomeness
var initial = {
time: 1297110663,
value: 70
}
function walk(prev, rand) {
return {
time: prev.time + 1,
value: ~~Math.max(10, Math.min(90, prev.value + 10 * (rand - .5)))
};
}
function rand() {
return new Bacon.Next(Math.random());
}
Bacon.interval(1500).map(Math.random).scan(initial, walk).slidingWindow(33).onValue(redraw);
</script>
</body>
</html>