-
Notifications
You must be signed in to change notification settings - Fork 1
/
berlin-moves-delta.html
174 lines (140 loc) · 4.05 KB
/
berlin-moves-delta.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
<!doctype html>
<head>
<meta charset="utf-8">
<title>D3 deltachart</title>
<link rel="stylesheet" type="text/css" href="base.css">
<script src="http://d3js.org/d3.v3.min.js"></script>
<style type="text/css">
.axis path, .axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-size: 0.650em;
}
.year {
font-size: 0.850em;
font-weight: 600;
fill: #ffffff;
}
.bgbar.even {
fill: #7f7f5f;
}
.bgbar.odd {
fill: #a8a376;
}
.bar.positive {
fill: #006b55;
}
.bar.negative {
fill: #ab003c;
}
</style>
</head>
<body>
<h2>People moving from and to Berlin</h2>
<p>data source:
<a href="http://daten.berlin.de/datensaetze/zu-und-fortz%C3%BCge-berlin">Senatsverwaltung für Wirtschaft, Technologie und Frauen, Senat of Berlin</a>
</p>
<p>
<a href="https://github.com/Su-Shee/opendata-wrestling">code is available on Su-Shee's github</a>
</p>
<div id="svgcontainer">
<script type="text/javascript">
"use strict";
// source of our data: http://daten.berlin.de
// "Senatsverwaltung für Wirtschaft, Technologie und Frauen"
// http://daten.berlin.de/datensaetze/zu-und-fortz%C3%BCge-berlin
// GET json file with data
var req = new XMLHttpRequest();
req.open('GET', 'berlin-moves-data.json', false);
req.send(null);
var berlinmoves = JSON.parse(req.responseText);
var dataset = [];
var years = [];
for (var year in berlinmoves ) {
var moved = berlinmoves[year];
var diff = moved[0] - moved[1];
years.push(year);
dataset.push(diff.toFixed(1));
}
var width = 800;
var height = 600;
var xpad = 20;
var ypad = 20;
var tpad = 20;
var bpad = 20;
var xlabel = "numbers in thousands";
var ylabel = "why did you leave?!";
var xScale = d3.scale.linear()
.domain( [-45.0, 45.0] )
.range( [xpad, width] );
var yScale = d3.scale.linear()
.domain([1996, 2011])
.range( [-10, height - ypad] );
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(5);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(12)
.tickFormat(d3.format("d"));
var chart = d3.select("#svgcontainer").append("svg")
.attr("width", width + xpad + ypad)
.attr("height", height + tpad + bpad)
.append("g");
chart.selectAll(".bgbar").data(dataset)
.enter().append("rect")
.attr("class", function(d, i) { return i % 2 ? "bgbar even" : "bgbar odd"; })
.attr("x", xpad * 2)
.attr("y", function (d, i) { return i * 35; })
.attr("width", width - xpad)
.attr("height", 35);
chart.selectAll(".bar").data(dataset)
.enter().append("rect")
.attr("class", function(d) { return d < 0 ? "bar negative" : "bar positive"; })
.attr("x", function (d, i) { return xScale( Math.min(0, d) ) + xpad; })
.attr("y", function (d, i) { return i * 35; })
.attr("width", function (d, i) { return Math.abs(xScale(d) - xScale(0)); })
.attr("height", 35);
chart.selectAll(".year").data(dataset)
.enter().append("text")
.attr("class", "year")
.attr("x", xpad * 3)
.attr("y", function (d, i) { return i == 0 ? 23 : (i * 35) + 23; })
.attr("height", 35)
.text(function (d, i) { return years[i]; });
chart.selectAll(".amount").data(dataset)
.enter().append("text")
.attr("class", function(d) { return d < 0 ? "amount negative" : "amount positive"; })
.attr("x", function (d, i) { return d < 0 ? xScale(Math.min(0, d)) - xpad * 1.5 : xScale(Math.max(0, d)) + xpad * 1.5; })
.attr("y", function (d, i) { return i == 0 ? 23 : (i * 35) + 23; })
.attr("height", 35)
.text(function (d, i) { return d; });
chart.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + xpad + "," + height + ")")
.call(xAxis);
chart.append("text")
.attr("class", "axis")
.attr("x", width/2)
.attr("y", height + ypad * 2)
.attr("text-anchor", "middle")
.attr("fill", "#7f7f5f")
.text(xlabel);
chart.append("text")
.attr("class", "axis")
.attr("x", -120)
.attr("y", ypad)
.attr("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.attr("fill", "#7f7f5f")
.text(ylabel);
</script>
</div>
<p></p>
</body>
</html>