forked from eurostat/EurostatVisu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoicop_sunburst.html
342 lines (296 loc) · 11.8 KB
/
coicop_sunburst.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="shortcut icon" type="image/png" href="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="js/colorbrewer.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="js/json-stat.js"></script>
<script src="js/lib.js"></script>
<script src="js/eurostat-lib.js"></script>
<script src="js/pr-eurostat-lib.js"></script>
<script src="js/pr-coicop-dict.js"></script>
<style>
body {
font-family: "Myriad Pro", Myriad, MyriadPro-Regular, 'Myriad Pro Regular', MyriadPro, 'Myriad Pro', "Liberation Sans", "Nimbus Sans L", "Helvetica Neue", vegur, Vegur, Helvetica, Arial, sans-serif;
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">
Household expenditure for <span id="geoTXT">?</span> in <span
id="yearTXT">?</span> by product
</h1>
<div id="sb" style="float: left; margin-right: 20px;"></div>
<div id="legend"></div>
<div style="margin-top: 10px;">
<div>
<div id="ctype" style="float: left; margin-right: 20px; ">
<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 id="modeR" style="margin-top: 10px;">
<input type="radio" name="modeR" id="size" value="size" checked>
<label for="size">Product weights</label> <input type="radio" name="modeR" id="count" value="count"> <label for="count">Equalised hierarchy</label>
</div>
</div>
<div id="geoListDiv" style="margin-top: 10px;">
<select name="geoN" id="geoList"></select>
</div>
<div style="margin-top: 10px;">
<div id="timeslider"></div>
</div>
</div>
<script>
/**
*
* Visualisation of coicop information as sunburst
*
* @author julien Gaffuri
*
*/
(function($, EstLib) {
$(function() {
//TODO remove radio circles
//TODO show flags
//TODO do something when no data
//TODO fill empty spaces ?
//TODO coicop 5
//
EstLib.modifyCoicopHierarchy();
//size
var r = 350;
//year/geo
var dataIndex = {};
//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");
var titleContentSave = null;
//svg element
var svg = d3.select("#sb").append("svg")
.attr("width", 2*r).attr("height", 2*r)
.append("g").attr("transform", "translate(" + r + "," + r + ")");
var partition = d3.layout.partition().sort(null).size([2 * Math.PI, r * r]);
var arc = d3.svg.arc()
.startAngle(function(d) { return d.x; })
.endAngle(function(d) { return d.x + d.dx; })
.innerRadius(function(d) { return Math.sqrt(d.y); })
.outerRadius(function(d) { return Math.sqrt(d.y + d.dy); });
//legend
var lgd = $("#legend");
lgd.css("height", 2*r);
var infoDiv = null;
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 {}
//info
infoDiv = $("<div>").appendTo(lgd).css("font-size","1.5em").css("margin-top","10px")/*.css("border","solid 1px").css("width","auto")*/;
};
refreshLegend();
var highlightCoicop = function(coicop){
$("#arc"+coicop).attr("fill","red");
if($("input:radio[name=ctype]:checked").val() === "CP00") $("#lgdEltRect"+coicop.substring(0,4)).css("fill","red");
var v = dataIndex[sli.slider("value")][geoList.find(":selected").attr("value")][coicop];
var html = EstLib.coicopsDict[coicop].desc + " (" + coicop + ")";
if($("input:radio[name=modeR]:checked").val() === "size") html += "<br>" + d3.round(v,1).toFixed(1) + "‰";
infoDiv.html(html);
};
var unHighlightCoicop = function(coicop){
$("#arc"+coicop).attr("fill",coicopToColor(coicop));
if($("input:radio[name=ctype]:checked").val() === "CP00") $("#lgdEltRect"+coicop.substring(0,4)).css("fill",coicopToColor(coicop));
infoDiv.html("");
};
var geoList = $("#geoList");
var sli = $("#timeslider");
sli.css("width",2*r);
//get base information on years and geos
$.when($.ajax({url:EstLib.getEstatDataURL("prc_hicp_inw",{coicop:"CP00"})}))
.then(function(ds) {
EstLib.overrideCountryNames(ds.dimension.geo.category.label);
ds = JSONstat(ds).Dataset(0);
var years = ds.Dimension("time").id.sort();
var geos = ds.Dimension("geo").id;
//option
var cType = $("#ctype");
$("input[name='ctype']").change(function () {
updateChart();
refreshLegend();
});
cType.buttonset();
//option
$("input[name='modeR']").change(function () {
if($(this).attr("value") === "count") {
//disable
$("#geoListDiv").hide();
sli.hide();
titleContentSave = title.html();
title.html("Products hierarchy");
} else {
//enable
$("#geoListDiv").show();
sli.show();
title.html(titleContentSave);
}
updateChart();
});
$("#modeR").buttonset();
//build geolist
EstLib.buildGeoList(geoList, geos, function(geo){return ds.Dimension("geo").Category(geo).label;}, "EA", function(){
$("#geoTXT").text( ds.Dimension("geo").Category(geoList.find(":selected").attr("value")).label );
updateChart();
}, 300);
//build years slider
EstLib.buildTimeSlider(sli, years, years[years.length-1], 1, function( event, ui ) {
$("#yearTXT").text(sli.slider("value"));
updateChart();
});
//fill text
$("#geoTXT").text( ds.Dimension("geo").Category(geoList.find(":selected").attr("value")).label );
$("#yearTXT").text(sli.slider("value"));
var updateChart_ = function(data,year,geo){
//build data structure
var root = {};
var buildDatasetRec = function(code, obj){
obj.name = code;
var children = EstLib.coicopsDict[code].children.sort();
if(children.length>0){
obj.children = [];
for(var i=0; i<children.length; i++){
var childCode = children[i];
var childObj = {};
obj.children.push(childObj);
buildDatasetRec(childCode, childObj);
}
}
else
obj.size = data?data[code]:1;
};
var rootLabel = $("input:radio[name=ctype]:checked").val();
var rootLabelC = rootLabel === "CP00";
buildDatasetRec(rootLabel,root);
//clear previous
svg.selectAll("*").remove();
//draw shapes
svg.datum(root).selectAll("path")
.data(partition.value($("input:radio[name=modeR]:checked").val() === "count"? function() { return 1; } : function(d) { return d.size; }).nodes)
.enter().append("path")
.attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring
.attr("d", arc)
.attr("id", function(d) { return "arc"+d.name; })
.attr("stroke-width", 0.5)
.attr("stroke", "gray")
.attr("fill", function(d) {
//var lev = 2 - Math.min(d.name.length-4,2)
return coicopToColor(d.name);
})
.on("mouseover", function(d) { highlightCoicop(d.name); })
.on("mouseout", function(d) { unHighlightCoicop(d.name); });
//draw labels
svg.datum(root).selectAll("text")
.data(partition.value($("input:radio[name=modeR]:checked").val() === "count"? function() { return 1; } : function(d) { return d.size; }).nodes)
.enter().append("text")
.attr("transform", function(d) {
var angle = 0;
var lev = rootLabelC? Math.min(d.name.length-4,2) >= 1 : $.inArray(d.name, EstLib.coicopsDict["SA"].children)<0;
if(lev){
var v = data? data[d.name] : 0;
angle = (d.x + d.dx*0.5) * 180/Math.PI;
if(v<30){ angle -= 90; if(angle<0) angle+=360; }
if(angle>90 && angle<270) angle-=180;
if(angle<0) angle+=360;
}
return "translate(" + arc.centroid(d) + ")"+ (angle==0?"":"rotate("+angle+")");
})
.attr("dy", ".35em")
.style("text-anchor", "middle")
.style("font-weight", function(d) {
var lev = rootLabelC? Math.min(d.name.length-4,2) == 0 : $.inArray(d.name, EstLib.coicopsDict["SA"].children)>=0;
if(lev) return "bold";
})
.style("fill", function(d) {
var lev = rootLabelC? Math.min(d.name.length-4,2) >= 1 : $.inArray(d.name, EstLib.coicopsDict["SA"].children)<0;
if(lev) return "#555";
})
.style("font-size", function(d) {
var lev = rootLabelC? Math.min(d.name.length-4,2) >= 1 : $.inArray(d.name, EstLib.coicopsDict["SA"].children)<0;
if(lev) return "10";
})
.html(function(d) {
if(d.name==="CP00" || d.name==="SA") return;
var lev = rootLabelC? Math.min(d.name.length-4,2) == 0 : $.inArray(d.name, EstLib.coicopsDict["SA"].children)>=0;
if(lev && !data) return d.name;
if(!data) {
return d.name;
}
var v = data[d.name];
if(lev) return (rootLabelC? d.name : EstLib.coicopsDict[d.name].desc) + "<tspan x=\"0\" dy=\"1.2em\">" + d3.round(v,1).toFixed(1) + " ‰</tspan>";
if(v>5) return d.name;
})
.on("mouseover", function(d) { highlightCoicop(d.name); })
.on("mouseout", function(d) { unHighlightCoicop(d.name); });
};
var updateChart = function(){
if($("input:radio[name=modeR]:checked").val() === "count"){
updateChart_();
return;
}
var geoSel = geoList.find(":selected").attr("value");
var yearSel = sli.slider("value");
//get data and update chart
EstLib.getProductWeightData(geoSel, yearSel, dataIndex, function(){ updateChart_(dataIndex[yearSel][geoSel],yearSel,geoSel); });
};
//
updateChart();
}, function() {
console.log("Could not load initialisation data");
});
});
}(jQuery, window.EstLib = window.EstLib || {} ));
</script>
</body>
</html>