-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcoicop_time_stack.html
273 lines (229 loc) · 9.26 KB
/
coicop_time_stack.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
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="shortcut icon" type="image/png"
href="https://raw.githubusercontent.com/eurostat/eurostat.js/master/img/favicon.png" />
<title>COICOP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://d3js.org/d3.v3.min.js"></script>
<!-- <script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script> -->
<script src="https://cdn.jsdelivr.net/gh/eurostat/eurostat.js@0.1/js/colorbrewer.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jsonstat@0.13.3/json-stat.js"></script>
<script src="https://cdn.jsdelivr.net/gh/eurostat/eurostat.js@0.1/js/lib.js"></script>
<script src="https://cdn.jsdelivr.net/gh/eurostat/eurostat.js@0.1/js/eurostat-lib.js"></script>
<script src="https://cdn.jsdelivr.net/gh/eurostat/eurostat.js@0.1/js/pr-eurostat-lib.js"></script>
<script src="https://cdn.jsdelivr.net/gh/eurostat/eurostat.js@0.1/js/pr-coicop-dict.js"></script>
<style>
body {
font-family: Arial;
font-size: 80%;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
text-align: center;
}
#timeslider label {
position: absolute;
width: 20px;
margin-top: 20px;
margin-left: -10px;
text-align: center;
}
.ui-widget {
font-size: 90% !important;
}
</style>
</head>
<body>
<h1 id="title">
Evolution of household expenditures for <span id="geoTXT">?</span>
</h1>
<div id="stack" style="float: left; margin-right: 20px;"></div>
<div id="legend"></div>
<div style="margin-top: 10px;">
<div id="geoListDiv" style="float: left; margin-right: 20px;">
<select name="geoN" id="geoList"></select>
</div>
<div id="ctype" style="margin-top: 10px;">
<input type="radio" name="ctype" id="CP00" value="CP00" checked>
<label for="CP00">COICOP classification</label> <input type="radio" name="ctype" id="SA" value="SA"> <label
for="SA">ECB
classification</label>
</div>
</div>
<script>
/**
*
* Visualisation of coicop information as time stack
*
* @author julien Gaffuri
*
*/
(function ($, EstLib) {
$(function () {
//http://bl.ocks.org/mbostock/4060954
//http://bl.ocks.org/mbostock/3943967
EstLib.getEstatRestDataURLBase = "https://ec.europa.eu/eurostat/api/dissemination/statistics/"
EstLib.getEstatDataURL = function (datasetCode, filters, lang, format, version, baseURL) {
lang = lang || "en";
format = format || "json";
version = version || "1.0";
baseURL = baseURL || EstLib.getEstatRestDataURLBase;
var url = [];
url.push(baseURL, version, "/data/", datasetCode, "?format=", format, "&lang=", lang);
if (filters)
for (var param in filters) {
var o = filters[param];
if (Array.isArray(o))
for (var i = 0; i < o.length; i++)
url.push("&", param, "=", o[i]);
else url.push("&", param, "=", o);
}
return url.join("");
};
//
//EstLib.modifyCoicopHierarchy();
//TODO add more coicop levels?
//TODO add ECB
var coicops = ["CP01", "CP02", "CP03", "CP04", "CP05", "CP06", "CP07", "CP08", "CP09", "CP10", "CP11", "CP12"];
//colors
var color = colorbrewer.Set3[12];
var colorSA = { "SERV": colorbrewer.Set3[4][0], "NRG": colorbrewer.Set3[4][1], "FOOD": colorbrewer.Set3[4][2], "IGD_NNRG": colorbrewer.Set3[4][3] };
var coicopToColor = function (coicop) {
if ($("input:radio[name=ctype]:checked").val() === "CP00") {
var fam = coicop.substring(0, 4);
if (fam === "CP00") return;
return color[+(fam.replace("CP", "")) - 1];
} else {
return colorSA[getFamCode(coicop)];
}
};
var getFamCode = function (coicop) {
if ($.inArray(coicop, EstLib.coicopsDict["SA"].children) >= 0) return coicop;
var parents = EstLib.coicopsDict[coicop].parents;
if (parents.length == 0) return;
for (var i = 0; i < parents.length; i++) {
var out = getFamCode(parents[i]);
if (out) return out;
}
};
//title
var title = $("#title");
//svg elements
var width = 800, height = 400, marginBottom = 30, marginSide = 20;
var svg = d3.select("#stack").append("svg").attr("width", width).attr("height", height);
var chart = svg.append("g").attr("transform", "translate(" + marginSide + ",0)");
var grat = svg.append("g").attr("class", "xaxis").attr("transform", "translate(" + marginSide + "," + (height - marginBottom) + ")");
grat.style({ fill: "none", stroke: "#777", font: "10px sans-serif" });
//legend
var lgd = $("#legend");
lgd.css("height", height);
var refreshLegend = function () {
lgd.empty();
if ($("input:radio[name=ctype]:checked").val() === "CP00") {
var mouseoverFun = function () { highlightCoicop($(this).attr("id").replace("lgdElt", "")); };
var mouseoutFun = function () { unHighlightCoicop($(this).attr("id").replace("lgdElt", "")); };
EstLib.buildCOICOPLegend(lgd, coicopToColor, mouseoverFun, mouseoutFun);
} else { }
};
refreshLegend();
var highlightCoicop = function (coicop) {
$("#area" + coicop).css("fill", "red");
$("#lgdEltRect" + coicop.substring(0, 4)).css("fill", "red");
};
var unHighlightCoicop = function (coicop) {
$("#area" + coicop).css("fill", coicopToColor(coicop));
$("#lgdEltRect" + coicop.substring(0, 4)).css("fill", coicopToColor(coicop));
};
var geoList = $("#geoList");
//get base information on geos
$.when($.ajax({ url: EstLib.getEstatDataURL("prc_hicp_inw", { coicop: "CP00" }) }))
.then(function (geoDim) {
EstLib.overrideCountryNames(geoDim.dimension.geo.category.label);
geoDim = JSONstat(geoDim).Dataset(0).Dimension("geo");
//option
var cType = $("#ctype");
$("input[name='ctype']").change(function () {
updateChart();
refreshLegend();
});
cType.buttonset();
//build geolist
var geoURL = EstLib.getParameterByName("geo") || "EA";
EstLib.buildGeoList(geoList, geoDim.id, function (geo) { return geoDim.Category(geo).label; }, geoURL, function () {
$("#geoTXT").text(geoDim.Category(geoList.find(":selected").attr("value")).label);
updateChart();
});
//title geo text
$("#geoTXT").text(geoDim.Category(geoList.find(":selected").attr("value")).label);
var updateChart = function () {
var geoSel = geoList.find(":selected").attr("value");
//get data and update chart
$.when(
$.ajax({ url: EstLib.getEstatDataURL("prc_hicp_inw", { geo: geoSel, coicop: coicops }) })
).then(function (ds) {
EstLib.overrideCountryNames(ds.dimension.geo.category.label);
ds = JSONstat(ds).Dataset(0);
//clear previous
chart.selectAll("*").remove();
//get years interval
var years = ds.Dimension("time").id,
yearMin = +years[0], yearMax = +years[years.length - 1];
//structure dataset
data = [];
for (var c = coicops.length - 1; c >= 0; c--) {
var coicop = coicops[c];
var data_ = [];
for (var year = yearMin; year <= yearMax; year++) {
var s = ds.Data({ coicop: coicop, time: "" + year, geo: geoSel });
data_.push({ year: year, y0: 0, y: s.value });
data_.coicop = coicop;
}
data.push(data_);
}
var stack = d3.layout.stack().offset("zero");
data = stack(data);
//scales
var xScale = d3.scale.linear().domain([yearMin, yearMax]).range([0, width - 2 * marginSide]);
var yScale = d3.scale.linear().domain([0, 1000]).range([height - marginBottom, 0]);
//area construction function
var area = d3.svg.area()
.x(function (d) { return xScale(d.year); })
.y0(function (d) { return yScale(d.y0); })
.y1(function (d) { return yScale(d.y0 + d.y); });
chart.selectAll("path").data(data).enter().append("path").attr("d", area)
.style("fill", function (d) { return coicopToColor(d.coicop); })
.attr("id", function (d) { return "area" + d.coicop; })
.on("mouseover", function (d) { highlightCoicop(d.coicop); })
.on("mouseout", function (d) { unHighlightCoicop(d.coicop); })
//.interpolate("monotone") TODO test that
;
//year labels
grat.selectAll("*").remove();
var xAxis = d3.svg.axis().scale(xScale).tickSize(-height).tickValues(years).tickFormat(function (d) { return d; }).orient("bottom");
grat.call(xAxis);
//rotate labels
grat.selectAll(".xaxis text").attr("transform", function () {
return "translate(" + (10 + this.getBBox().height * -2) + "," + this.getBBox().height + ")rotate(-45)";
});
}, function () {
console.log("Could not load product weight data for " + geoSel);
});
};
//
updateChart();
}, function () {
console.log("Could not load initialisation data");
});
});
}(jQuery, window.EstLib = window.EstLib || {}));
</script>
</body>
</html>