forked from devsar/d3talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.html
162 lines (138 loc) · 4.46 KB
/
calendar.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script type="text/javascript" src="d3/d3.js"></script>
<script type="text/javascript" src="d3/d3.csv.js"></script>
<script type="text/javascript" src="d3/d3.time.js"></script>
<link type="text/css" rel="stylesheet" href="style.css"/>
<link type="text/css" rel="stylesheet" href="colorbrewer/colorbrewer.css"/>
<style type="text/css">
#body {
padding-top: 20px;
text-align: center;
}
svg {
font-size: 10px;
shape-rendering: crispEdges;
}
.day {
fill: #fff;
stroke: #000;
stroke-opacity: .1;
}
.month {
fill: none;
stroke: #000;
stroke-width: 2px;
}
</style>
</head>
<body>
<div id="body">
<div id="footer">
<span>…</span>, 1995-2008
<div class="hint">use the menu to change the dataset</div>
<div><select>
<option value="flights">U.S. Commercial Flights</option>
<option value="stocks">Dow Jones</option>
</select></div>
</div>
</div>
<script type="text/javascript">
var m = [29, 20, 20, 19], // top right bottom left margin
w = 620 - m[1] - m[3], // width
h = 90 - m[0] - m[2], // height
z = 11; // cell size
var day = d3.time.format("%w"),
week = d3.time.format("%U"),
percent = d3.format(".1%"),
flightData,
stockData,
formatDate = d3.time.format("%Y-%m-%d"),
formatNumber = d3.format(",d"),
formatPercent = d3.format("+.1%");
var svg = d3.select("#body").selectAll(".year")
.data(d3.range(1995, 2009))
.enter().append("div")
.attr("class", "year")
.style("width", w + m[1] + m[3] + "px")
.style("height", h + m[0] + m[2] + "px")
.style("display", "inline-block")
.append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.attr("class", "RdYlGn")
.append("svg:g")
.attr("transform", "translate(" + (m[3] + (w - z * 53) / 2) + "," + (m[0] + (h - z * 7) / 2) + ")");
svg.append("svg:text")
.attr("transform", "translate(-6," + z * 3.5 + ")rotate(-90)")
.attr("text-anchor", "middle")
.text(String);
var rect = svg.selectAll("rect.day")
.data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("svg:rect")
.attr("class", "day")
.attr("width", z)
.attr("height", z)
.attr("x", function(d) { return week(d) * z; })
.attr("y", function(d) { return day(d) * z; });
rect.append("svg:title");
svg.selectAll("path.month")
.data(function(d) { return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter().append("svg:path")
.attr("class", "month")
.attr("d", monthPath);
d3.csv("dji.csv", function(csv) {
stockData = d3.nest()
.key(function(d) { return (d.date = formatDate.parse(d.Date)).getFullYear(); })
.key(function(d) { return d.date; })
.rollup(function(d) { return d.value = (d[0].Close - d[0].Open) / d[0].Open; })
.map(csv);
d3.select("select").on("change", function() {
display(this.value === "flights" ? flightData : stockData);
});
});
d3.csv("flights-departed.csv", function(csv) {
flightData = d3.nest()
.key(function(d) { return (d.date = formatDate.parse(d.date)).getFullYear(); })
.key(function(d) { return d.date; })
.rollup(function(d) { return +d[0].value; })
.map(csv);
display(flightData);
});
function display(data, title) {
if (data === flightData) {
var formatValue = formatNumber,
title = "U.S. Commercial Flights",
color = d3.scale.quantile();
} else {
var formatValue = formatPercent,
title = "Dow Jones",
color = d3.scale.quantize();
}
d3.select("#footer span")
.text(title);
svg.each(function(year) {
color
.domain(data === flightData ? d3.values(data[year]) : [-.05, .05])
.range(d3.range(9));
d3.select(this).selectAll("rect.day")
.attr("class", function(d) { return "day q" + color(data[year][d]) + "-9"; })
.select("title")
.text(function(d) { return formatDate(d) + ": " + formatValue(data[year][d]); });
});
}
function monthPath(t0) {
var t1 = new Date(t0.getFullYear(), t0.getMonth() + 1, 0),
d0 = +day(t0), w0 = +week(t0),
d1 = +day(t1), w1 = +week(t1);
return "M" + (w0 + 1) * z + "," + d0 * z
+ "H" + w0 * z + "V" + 7 * z
+ "H" + w1 * z + "V" + (d1 + 1) * z
+ "H" + (w1 + 1) * z + "V" + 0
+ "H" + (w0 + 1) * z + "Z";
}
</script>
</body>
</html>