-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
310 lines (253 loc) · 8.48 KB
/
index.html
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
.line {
stroke-width: 1;
fill: none;
clip-path: url(#clip);
}
.area {
clip-path: url(#clip);
stroke-width: 0;
opacity: 0.7;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.brush .extent {
stroke: #fff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
/* Statistic lines */
.meanline {
stroke: blue;
fill: none;
stroke-width: 2;
clip-path: url(#clip);
}
.meanline-label {
fill: blue;
stroke-width: 1;
font-size: 14px;
}
.sdline {
stroke: red;
fill: none;
stroke-width: 2;
stroke-dasharray: 10,10;
clip-path: url(#clip);
}
.sdline-label {
fill: red;
stroke-width: 1;
font-size: 14px;
}
.label {
fill: black;
stroke-width: 1;
font-size: 14px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 10, right: 10, bottom: 100, left: 40},
margin2 = {top: 230, right: 10, bottom: 20, left: 40},
width = 1080 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom,
height2 = 300 - margin2.top - margin2.bottom;
var parseDate = d3.time.format("%b %Y").parse;
var color = "#27ae60";
var x = d3.time.scale().range([0, width]),
x2 = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
y2 = d3.scale.linear().range([height2, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left");
var brush = d3.svg.brush()
.x(x2)
.on("brush", brushed)
.on("brushend", brushend);
var area = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.price); });
var line = d3.svg.line()
.interpolate("monotone")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.price); });
var area2 = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x2(d.date); })
.y0(height2)
.y1(function(d) { return y2(d.price); });
var line2 = d3.svg.line()
.interpolate("monotone")
.x(function(d) { return x2(d.date); })
.y(function(d) { return y2(d.price); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
var data = [{date: 'Jan 2000', price: 45.3},
{date: 'Feb 2000', price: 35.6},
{date: 'Mar 2000', price: 28.4},
{date: 'Apr 2000', price: 23.3},
{date: 'May 2000', price: 29.7},
{date: 'Jun 2000', price: 32.4},
{date: 'Jul 2000', price: 37.9},
{date: 'Aug 2000', price: 35.2},
{date: 'Sep 2000', price: 34.3},
{date: 'Oct 2000', price: 32.5},
{date: 'Nov 2000', price: 36.7},
{date: 'Dec 2000', price: 33.6},
{date: 'Jan 2001', price: 38.3},
{date: 'Feb 2001', price: 45.6},
{date: 'Mar 2001', price: 52.4},
{date: 'Apr 2001', price: 49.3},
{date: 'May 2001', price: 52.7},
{date: 'Jun 2001', price: 45.4},
{date: 'Jul 2001', price: 37.9},
{date: 'Aug 2001', price: 33.2},
{date: 'Sep 2001', price: 29.3},
{date: 'Oct 2001', price: 35.5},
{date: 'Nov 2001', price: 40.7},
{date: 'Dec 2001', price: 42.6}];
data.forEach(function(d) {
d.date = parseDate(d.date);
d.price = +d.price;
});
x.domain(d3.extent(data.map(function(d) { return d.date; })));
y.domain([0, d3.max(data.map(function(d) { return d.price; }))]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(data)
.attr("class", "area")
.attr("fill", d3.rgb(color).brighter(2))
.attr("d", area);
focus.append("path")
.datum(data)
.attr("class", "line")
.attr("stroke", color)
.attr("d", line);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "y axis")
.call(yAxis);
context.append("path")
.datum(data)
.attr("class", "area")
.attr("fill", d3.rgb(color).brighter(2))
.attr("d", area2);
context.append("path")
.datum(data)
.attr("class", "line")
.attr("stroke", color)
.attr("d", line2);
context.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "x brush")
.call(brush)
.selectAll("rect")
.attr("y", -6)
.attr("height", height2 + 7);
// Statistic lines and labels
var statisticData = calcMeanSdVar(data);
var meanData = [{date: data[0].date, price: statisticData.mean},
{date: data[data.length - 1].date, price: statisticData.mean}];
var lineStatistic = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.price); });
focus.append('path')
.datum(meanData)
.attr("class", "meanline")
.attr("d", lineStatistic );
focus.append("text")
.attr("x", 10)
.attr("y", height - margin.top)
.attr("dy", ".35em")
.attr("class", "meanline-label")
.text("Mean: " + statisticData.mean.toFixed(2) );
var sdMinData = [{date: data[0].date, price: statisticData.mean - statisticData.deviation},
{date: data[data.length - 1].date, price: statisticData.mean - statisticData.deviation}];
focus.append('path')
.datum(sdMinData)
.attr("class", "sdline min")
.attr("d", lineStatistic );
var sdMaxData = [{date: data[0].date, price: statisticData.mean + statisticData.deviation},
{date: data[data.length - 1].date, price: statisticData.mean + statisticData.deviation}];
focus.append('path')
.datum(sdMaxData)
.attr("class", "sdline max")
.attr("d", lineStatistic );
focus.append("text")
.attr("x", 110)
.attr("y", height - margin.top)
.attr("dy", ".35em")
.attr("class", "sdline-label")
.text("Standard Deviation: " + statisticData.deviation.toFixed(2) );
// More statistic labels
focus.append("text")
.attr("x", 310)
.attr("y", height - margin.top)
.attr("dy", ".35em")
.attr("class", "label variance")
.text("Variance: " + statisticData.variance.toFixed(2) );
function brushed() {
x.domain(brush.empty() ? x2.domain() : brush.extent());
focus.select(".area").attr("d", area);
focus.select(".line").attr("d", line);
focus.select(".x.axis").call(xAxis);
}
function brushend() {
var extent = brush.extent();
// Retrieve brushed data
var extentData = data.filter(function(d) { return extent[0] <= d.date && d.date <= extent[1] });
statisticData = calcMeanSdVar(extentData);
meanData = [{date: extentData[0].date, price: statisticData.mean},
{date: extentData[extentData.length - 1].date, price: statisticData.mean}];
focus.select(".meanline").data([meanData]).attr("d", lineStatistic);
focus.select(".meanline-label").text("Mean: " + statisticData.mean.toFixed(2) );
var sdMaxData = [{date: extentData[0].date, price: statisticData.mean + statisticData.deviation},
{date: extentData[extentData.length - 1].date, price: statisticData.mean + statisticData.deviation}];
focus.select(".max").data([sdMaxData]).attr("d", lineStatistic);
var sdMinData = [{date: extentData[0].date, price: statisticData.mean - statisticData.deviation},
{date: extentData[extentData.length - 1].date, price: statisticData.mean - statisticData.deviation}];
focus.select(".min").data([sdMinData]).attr("d", lineStatistic);
focus.select(".sdline-label").text("Standard Deviation: " + statisticData.deviation.toFixed(2) );
focus.select(".variance").text("Variance: " + statisticData.variance.toFixed(2) );
}
// Mean, standard deviation, variance
function calcMeanSdVar(values) {
var r = {mean: 0, variance: 0, deviation: 0}, t = values.length;
for(var m, s = 0, l = t; l--; s += parseInt(values[l].price));
for(m = r.mean = s / t, l = t, s = 0; l--; s += Math.pow(parseInt(values[l].price) - m, 2));
return r.deviation = Math.sqrt(r.variance = s / t), r;
}
</script>