-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
311 lines (297 loc) · 12.7 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
311
<!DOCTYPE html>
<html>
<head>
<title>Euro crisis</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="../d3.min.js"></script>
<script type="text/javascript" src="../d3.layout.min.js"></script>
<script type="text/javascript" src="../d3.time.min.js"></script>
<script type="text/javascript" src="../jquery-1.6.2.min.js"></script>
<style type="text/css">
body {
font: 12px Verdana, Palatino, Helvetica, sans;
background: #ffdd73;
}
h1 {
font-weight: normal;
font-size: 4em;
margin: 0 0 0;
}
line {
stroke: #bf8330;
stroke-width: 3;
stroke-linecap: round;
}
path {
stroke: #bf8330;
stroke-width: 1;
stroke-linecap: round;
}
.tick {
stroke: #000;
stroke-width: 1;
fill: none;
}
.domain {
stroke-width: 2;
fill: none;
stroke: #000;
fill: none;
}
.title {
font-weight: bold;
}
</style>
</head>
<body>
<div style="position: fixed; top: 10px; left: 10px">
<h1>Euro crisis</h1>
Width of lines represents interest rates, colour ratings. Hover over
line to see country name. The dashed line shows the 3% deficit limit
of the Eurozone.
</div>
<div id="graph" style="position: fixed; top: 15%;"></div>
<div style="position: fixed; bottom: 10px; left: 10px">
Deficit and interest rates courtesy of the
<a href="http://www.ecb.int/">European Central Bank</a>,
ratings are those of <a href="http://www.fitchratings.com">Fitch</a>.
</div>
<div style="position: fixed; bottom: 10px; right: 10px">
Lars Kotthoff 2012
</div>
<script type="text/javascript">
(function () {
var margin = 50,
width = window.innerWidth - 30,
height = window.innerHeight*0.8,
svg = d3.select("#graph")
.append("svg:svg")
.attr("height", height)
.attr("width", width),
clab = svg.append("svg:text")
.classed("title", true)
.attr("transform", "translate(" + (width/2) + "," +
(height/5) + ")");
d3.json("debt.json", function(json) {
var dmax = d3.max($.map(d3.values(json.deficit), function(d) {
return d3.max($.map(d, function(e) { return e[1]; }));
})),
dmin = d3.min($.map(d3.values(json.deficit), function(d) {
return d3.min($.map(d, function(e) { return e[1]; }));
})),
imax = d3.max($.map(d3.values(json.interest), function(d) {
return d3.max($.map(d, function(e) { return e[1]; }));
})),
imin = d3.min($.map(d3.values(json.interest), function(d) {
return d3.min($.map(d, function(e) { return e[1]; }));
})),
iscale = d3.scale.linear().domain([imin, imax]).range([2, 16]),
dformat = d3.time.format("%d/%m/%Y")
tmin = d3.min($.map(d3.values(json.deficit), function(d) {
return d3.min($.map(d, function(e) {
return dformat.parse(e[0]);
}));
})),
tmax = d3.max($.map(d3.values(json.deficit), function(d) {
return d3.max($.map(d, function(e) {
return dformat.parse(e[0]);
}));
})),
rscale = d3.scale.ordinal()
.domain(["AAA", "AAA-", "AA+", "AA", "AA-", "A+", "A", "A-",
"BBB+", "BBB", "BBB-", "BB+", "BB", "BB-", "B+",
"B", "B-", "CCC+", "CCC"])
.rangePoints([0, 1]),
crscale = d3.scale.linear().domain([0, 0.5, 1])
.range(["green", "yellow", "red"]),
x = d3.time.scale().domain([tmin, tmax])
.range([margin, width - 30]),
y = d3.scale.linear().domain([dmin, dmax]).nice()
.range([height - margin, 15]),
xlabels = d3.svg.axis().scale(x).orient("bottom"),
ylabels = d3.svg.axis().scale(y).orient("left").ticks(20);
function grad(d) {
var xp = d3.time.scale().domain([tmin, tmax]);
d3.select(this).append("svg:stop")
.attr("offset", 0)
.attr("stop-color", crscale(rscale(d.value[0][1])));
for(var i = 1; i < d.value.length &&
dformat.parse(d.value[i][0]) <= tmax; i++) {
d3.select(this).append("svg:stop")
.attr("offset", xp(dformat.parse(d.value[i][0])))
.attr("stop-color", crscale(rscale(d.value[i-1][1])));
d3.select(this).append("svg:stop")
.attr("offset", xp(dformat.parse(d.value[i][0])))
.attr("stop-color", crscale(rscale(d.value[i][1])));
}
d3.select(this).append("svg:stop")
.attr("offset", 1)
.attr("stop-color",
crscale(rscale(d.value[d.value.length-1][1])));
}
var iscales = [];
$.each(d3.entries(json.interest), function(i, d) {
iscales[d.key] = d3.scale.linear()
.domain($.map(d.value, function(e) {
return dformat.parse(e[0]);
}))
.range($.map(d.value, function(e) {
return iscale(e[1]);
}));
});
svg.append("svg:line")
.attr("y1", y(0))
.attr("y2", y(0))
.attr("x1", x(tmin))
.attr("x2", x(tmax));
svg.append("svg:line")
.attr("stroke-dasharray", "10 10")
.attr("y1", y(-3))
.attr("y2", y(-3))
.attr("x1", x(tmin))
.attr("x2", x(tmax));
svg.append("svg:defs")
.selectAll("linearGradient")
.data(d3.entries(json.ratings)).enter()
.append("svg:linearGradient")
.attr("id", function(d) { return d.key.substr(0, 3); })
.each(grad);
function mkPs(p, aleft, aright, o) {
var gamma = aleft - aright,
d = o / Math.sin(gamma/2),
aup = aright + gamma/2,
adown = aup - Math.PI,
dxup = d * Math.cos(aup),
dyup = d * Math.sin(aup),
dxdown = d * Math.cos(adown),
dydown = d * Math.sin(adown),
t = [[p[0] + dxup, p[1] - dyup]],
b = [[p[0] + dxdown, p[1] - dydown]];
return {"t": t, "b": b};
}
function varLine(points) {
var ps = $.map(points.value, function(p, i) {
var thisx = x(dformat.parse(p[0])),
thisy = y(p[1]),
width = iscales[points.key](dformat.parse(p[0]));
aleft = Math.PI, aright = 0;
if(i > 0) {
var prevx = x(dformat.parse(points.value[i-1][0])),
prevy = y(points.value[i-1][1]),
dx = thisx - prevx,
dy = thisy - prevy;
aleft = Math.PI - Math.atan(dy/dx);
}
if(i < points.value.length-1) {
var nextx = x(dformat.parse(points.value[i+1][0])),
nexty = y(points.value[i+1][1]),
dx = nextx - thisx,
dy = thisy - nexty;
aright = Math.atan(dy/dx);
}
return mkPs([thisx, thisy], aleft, aright, width/2);
}),
tops = $.map(ps, function(p) { return p.t; }),
bottoms = $.map(ps, function(p) { return p.b; }).reverse();
return "M" + tops.join(" ") + " " + bottoms.join(" ") + "Z";
}
svg.selectAll("path")
.data(d3.entries(json.deficit)).enter()
.append("svg:path")
.attr("fill", function(d) {
return "url(#" + d.key.substr(0, 3) + ")";
})
.attr("d", varLine)
.on("mouseover", function(d) { clab.text(d.key); })
.on("mouseout", function(d) { clab.text(""); });
// debug start
/*
var line = d3.svg.line()
.y(function(d) { return y(d[1]); })
.x(function(d) { return x(dformat.parse(d[0])); });
svg.selectAll("path.foo")
.data(d3.entries({"Ireland": json.deficit.Ireland})).enter()
.append("svg:path")
.attr("fill", "none")
.attr("stroke-dasharray", "10 10")
.attr("d", function(d) { return line(d.value); });
svg.selectAll("circle")
.data(json.deficit.Ireland).enter()
.append("svg:circle")
.attr("fill", "none")
.attr("stroke", "black")
.attr("r", 10)
.attr("cx", function(d) { return x(dformat.parse(d[0])); })
.attr("cy", function(d) { return y(d[1]); });
*/
// debug end
svg.append("svg:g")
.attr("transform", "translate(0," + (height - margin + 5) + ")")
.call(xlabels);
svg.append("svg:g")
.classed("yaxis", true)
.attr("transform", "translate(" + (margin - 5) + ",0)")
.call(ylabels);
svg.append("svg:text")
.classed("title", true)
.attr("transform", "translate(" + (margin + 5) + ",15)")
.attr("text-anchor", "start")
.text("surplus [% GDP]");
svg.append("svg:text")
.classed("title", true)
.attr("transform", "translate(" + (5*width/6-10) + ",10)")
.attr("text-anchor", "end")
.text("interest (long-term) [%]");
svg.append("svg:text")
.attr("transform", "translate(" + (5*width/6-10) + ",28)")
.attr("text-anchor", "end")
.text(imin);
svg.append("svg:text")
.attr("transform", "translate(" + (5*width/6-10) + "," +
(20 + 2*margin) + ")")
.attr("text-anchor", "end")
.text(imax);
svg.append("svg:text")
.classed("title", true)
.attr("transform", "translate(" + (5*width/6+10+iscale(imax)) +
",10)")
.attr("text-anchor", "start")
.text("rating");
svg.append("svg:text")
.attr("transform", "translate(" + (5*width/6+10+iscale(imax)) +
",28)")
.attr("text-anchor", "start")
.style("stroke", crscale(rscale("AAA")))
.text("AAA");
svg.append("svg:text")
.attr("transform", "translate(" + (5*width/6+10+iscale(imax)) +
"," + (20 + 2*margin) + ")")
.attr("text-anchor", "start")
.attr("stroke", crscale(rscale("CCC")))
.text("CCC");
var legfill = svg.select("defs")
.append("svg:linearGradient")
.attr("x1", "0%")
.attr("x2", "0%")
.attr("y1", "0%")
.attr("y2", "100%")
.attr("id", "legend");
legfill.append("svg:stop")
.attr("offset", 0)
.attr("stop-color", crscale(rscale("AAA")));
legfill.append("svg:stop")
.attr("offset", 0.5)
.attr("stop-color", crscale(rscale("BBB")));
legfill.append("svg:stop")
.attr("offset", 1)
.attr("stop-color", crscale(rscale("CCC")));
svg.append("svg:path")
.attr("transform", "translate(" + (5*width/6) + ",20)")
.attr("fill", "url(#legend)")
.attr("d", "M0,0 " + iscale(imin) + ",0 " + iscale(imax) +
"," + 2*margin + " " + "0," + 2*margin + "Z");
});
})();
</script>
</body>
</html>