blob: 8dc501bb26ff6e81d7bb26f30d644bc6b4a0fa77 [file] [log] [blame]
Thomas Vachuska95aadff2015-03-26 11:45:41 -07001<!DOCTYPE html>
2<meta charset="utf-8">
3<style>
4
5 svg {
6 font: 10px sans-serif;
7 }
8
9 .line {
10 fill: none;
11 stroke: darkgreen;
12 stroke-width: 2px;
13 }
14
15 .axis path,
16 .axis line {
17 fill: none;
18 stroke: #999;
19 stroke-width: 2px;
20 shape-rendering: crispEdges;
21 }
22
23</style>
24<body>
25<script src="http://d3js.org/d3.v3.min.js"></script>
26<script>
27 (function () {
28 var cs = 0,
29 samples = [
30 89.53,
31 37515.81,
32 104609.6,
33 113105.11,
34 103194.74,
35 122151.63,
36 128623.9,
37 137325.33,
38 154897.31,
39 161235.07,
40 162025.4,
41 164902.64,
42 158196.26,
43 161072.44,
44 160792.54,
45 164692.44,
46 161979.74,
47 162137.4,
48 159325.19,
49 170465.44,
50 168186.46,
51 171152.34,
52 168221.02,
53 167440.73,
54 165003.39,
55 166855.18,
56 157268.79,
57 164087.54,
58 162265.21,
59 165990.16,
60 176364.01,
61 172064.07,
62 184872.24,
63 183249.8,
64 182282.47,
65 171475.11,
66 158880.58,
67 166016.69,
68 168233.16,
69 177759.92,
70 179742.87,
71 170819.44,
72 167577.73,
73 169479.9,
74 175544.89,
75 183792.01,
76 184689.52,
77 178503.87,
78 173219.27,
79 179085.49,
80 179700.54,
81 174281.17,
82 181353.08,
83 180173.14,
84 184093.16,
85 186011.5,
86 176952.79,
87 175319.2,
88 169001.05,
89 174545.12,
90 169156.29,
91 171804.3,
92 159155.54,
93 154709.96,
94 157263.97
95 ],
96 theSample,
97 headers = [ "Whole", "Half", "Third" ];
98
99 var n = 243,
100 duration = 750,
101 now = new Date(Date.now() - duration),
102 data = [];
103
104 headers.forEach(function (d, li) {
105 data[li] = d3.range(n).map(function () { return 0; });
106 });
107
108 var margin = {top: 20, right: 100, bottom: 20, left: 100},
109 width = 960 - margin.right,
110 height = 512 - margin.top - margin.bottom;
111
112 var x = d3.time.scale()
113 .domain([now - (n - 2) * duration, now - duration])
114 .range([0, width]);
115
116 var y = d3.scale.linear()
117 .domain([0, 200000])
118 .range([height, 0]);
119
120 var svg = d3.select("body").append("p").append("svg")
121 .attr("width", width + margin.left + margin.right)
122 .attr("height", height + margin.top + margin.bottom)
123 .append("g")
124 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
125
126 svg.append("defs").append("clipPath")
127 .attr("id", "clip")
128 .append("rect")
129 .attr("width", width)
130 .attr("height", height);
131
132 var axis = svg.append("g")
133 .attr("class", "x axis")
134 .attr("transform", "translate(0," + height + ")")
135 .call(x.axis = d3.svg.axis().scale(x).orient("bottom"));
136
137 svg.append("g")
138 .attr("class", "y axis")
139 .call(d3.svg.axis().scale(y).orient("left"));
140
141 svg.append("g")
142 .attr("class", "y axis")
143 .attr("transform", "translate(" + width + " ,0)")
144 .call(d3.svg.axis().scale(y).orient("right"));
145
146 var lines = [], paths = [];
147 data.forEach(function (p, li) {
148 lines[li]= d3.svg.line()
149 .interpolate("basis")
150 .x(function (d, i) {
151 return x(now - (n - 1 - i) * duration);
152 })
153 .y(function (d, i) {
154 return y(d);
155 });
156
157 paths[li] = svg.append("g")
158 .attr("clip-path", "url(#clip)")
159 .append("path")
160 .datum(function () { return data[li]; })
161 .attr("id", "line" + li)
162 .attr("class", "line");
163 });
164
165 var transition = d3.select({}).transition()
166 .duration(750)
167 .ease("linear");
168
169 function tick() {
170 transition = transition.each(function () {
171 // update the domains
172 now = new Date();
173 x.domain([now - (n - 2) * duration, now - duration]);
174
175 data.forEach(function (d, li) {
176 // push the new most recent sample onto the back
177 d.push(theSample[li]);
178
179 // redraw the line and slide it left
180 paths[li].attr("d", lines[li]).attr("transform", null);
181 paths[li].transition().attr("transform", "translate(" + x(now - (n - 1) * duration) + ")");
182
183 // pop the old data point off the front
184 d.shift();
185 });
186
187 // slide the x-axis left
188 axis.call(x.axis);
189
190 }).transition().each("start", tick);
191 }
192
193 function setSample() {
194 var v = samples[cs++];
195 theSample = [ v, v/2, v/3 ];
196 }
197
198 setSample();
199 setInterval(setSample, 1000);
200 tick();
201
202 })()
203</script>
204</body>
205</html>