-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcofog_sunburst.html
292 lines (250 loc) · 14.3 KB
/
cofog_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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf8" />
<link rel="shortcut icon" type="image/png" href="https://raw.githubusercontent.com/eurostat/eurostat.js/master/img/favicon.png"/>
<title>Government expenditure by function in Europe</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://cdn.jsdelivr.net/gh/eurostat/[email protected]/d3-sunburst.js"></script>
<script src="https://cdn.jsdelivr.net/gh/eurostat/[email protected]/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/[email protected]/json-stat.js"></script>
<script src="https://cdn.jsdelivr.net/gh/eurostat/[email protected]/js/lib.js"></script>
<script src="https://cdn.jsdelivr.net/gh/eurostat/[email protected]/js/eurostat-lib.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">Government expenditure for <span id="geoTXT">?</span> in <span id="yearTXT">?</span> by function</h1>
<div id="sunburst" style="float: left; margin-right: 20px;"></div>
<div id="legend"></div>
<div style="margin-top: 10px;">
<div>
<div id="modeR" style="margin-top: 10px;">
<input type="radio" name="modeR" id="size" value="size" checked>
<label for="size">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>
/**
*
* @author julien Gaffuri
*
*/
(function($, EstLib) {
$(function() {
//get cofog hierarchy
d3.csv("https://raw.githubusercontent.com/eurostat/eurostat.js/master/data/cofog99.csv", function(data) {
//build cofog dictionnary
var cofogsDict={};
var cofogCodes=[];
for(var i=0; i<data.length; i++){
var d=data[i];
d.children=[];
cofogsDict[d.code]=d;
cofogCodes.push(d.code);
}
data=null;
//build cofog hierarchy
for(i=0; i<cofogCodes.length; i++){
var cofogCode=cofogCodes[i];
if(cofogCode.length == 4) continue;
var parentCode = cofogCode.substring(0,4);
cofogsDict[parentCode].children.push(cofogsDict[cofogCode]);
}
//build root element of the hierarchy
{
cofogsDict.TOTAL={code:"TOTAL",desc:"All",children:[]};
var ch = ["GF01","GF02","GF03","GF04","GF05","GF06","GF07","GF08","GF09","GF10"];
for(i=0;i<ch.length;i++) cofogsDict.TOTAL.children.push(cofogsDict[ch[i]]);
}
//console.log(cofogsDict);
//console.log(cofogsDict["TOTAL"]);
//colors
var color = colorbrewer.Set3[10];
var cofogToColor = function(code){
var fam = code.substring(0,4);
if(fam==="TOTAL") return;
return color[+(fam.replace("GF",""))-1];
};
//build initial sunburst object
var r=230;
var sb = d3.sunburst()
.codesHierarchy(cofogsDict["TOTAL"])
.radius(r)
.codeToColor(cofogToColor)
.strokeWidth(0.5)
.strokeColor("#fff")
.setmouseover( function(cofog){
$("#arc"+cofog).attr("fill","red");
$("#lgdEltRect"+cofog.substring(0,4)).css("fill","red");
var v = dataIndex[sli.slider("value")][geoList.find(":selected").attr("value")][cofog];
var html = cofogsDict[cofog].desc + " (" + cofog + ")";
if($("input:radio[name=modeR]:checked").val() === "size") html += "<br>" + d3.round(v,1).toFixed(1) + "% of GDP";
infoDiv.html(html);
})
.setmouseout( function(cofog){
$("#arc"+cofog).attr("fill",cofogToColor(cofog));
$("#lgdEltRect"+cofog.substring(0,4)).css("fill",cofogToColor(cofog));
infoDiv.html("");
})
.fontSize( function(depth){ return depth==1?12:10;} )
.fontFill( function(depth){ return depth==1?"#111":"#444";} )
.labelRotationParameter( function(depth){ return depth==1?1:2;} )
.labelRemovalParameter( function(depth){ return depth==1?0.5:0.5;} )
.set({},0)
;
//title
var title = $("#title");
var titleContentSave = null;
//legend
var lgd = $("#legend");
lgd.css("height", 2*r);
var infoDiv = null;
var refreshLegend = function(){
lgd.empty();
//var mouseoverFun = function() { highlightCofog($(this).attr("id").replace("lgdElt","")); };
//var mouseoutFun = function() { unHighlightCofog($(this).attr("id").replace("lgdElt","")); };
//TODO build legend EstLib.buildCOICOPLegend(lgd, cofogToColor, mouseoverFun, mouseoutFun);
//info
infoDiv = $("<div>").appendTo(lgd).css("font-size","1.5em").css("margin-top","10px")/*.css("border","solid 1px").css("width","auto")*/;
};
refreshLegend();
var geoList = $("#geoList");
var sli = $("#timeslider");
sli.css("width",2*r);
//the data store: a dictionary of values by year/geo
var dataIndex = {};
//get base information on years and geos
$.when($.ajax({url:EstLib.getEstatDataURL("gov_10a_exp",{cofog99:"TOTAL",na_item:"TE",sector:"S13",unit:"PC_GDP"})}))
.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
$("input[name='modeR']").change(function () {
if($(this).attr("value") === "count") {
//disable
$("#geoListDiv").hide();
sli.hide();
titleContentSave = title.html();
title.html("COFOG 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;}, "EU28", 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-2], 2, 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(){
if($("input:radio[name=modeR]:checked").val() === "count"){
sb.set(null,500);
return;
}
var geoSel = geoList.find(":selected").attr("value");
var yearSel = sli.slider("value");
//get data and update chart
if(geoSel && dataIndex[yearSel] && dataIndex[yearSel][geoSel])
sb.set(dataIndex[yearSel][geoSel],500)
else if(!geoSel && dataIndex[yearSel])
sb.set(dataIndex[yearSel][geoSel],500)
else {
//no data: get it!
//build query objects - by year/cofog
var ajaxs = [];
//get cofog groups (due to limitation in eurostat web service)
EstLib.getSlicedCOICOPList = function(nb){
nb = nb || 50;
var cofogs_ = [];
for(var i=0; i<(cofogCodes.length/nb); i++)
cofogs_[i] = cofogCodes.slice(i*nb, Math.min((i+1)*nb,cofogCodes.length));
return cofogs_;
};
var cofogs_ = EstLib.getSlicedCOICOPList();
for(var i=0; i<cofogs_.length; i++)
if(geoSel) ajaxs.push( $.ajax({url:EstLib.getEstatDataURL("gov_10a_exp",{geo:geoSel,time:yearSel,na_item:"TE",sector:"S13",unit:"PC_GDP",cofog99:cofogs_[i]})}) );
else ajaxs.push( $.ajax({url:EstLib.getEstatDataURL("gov_10a_exp",{time:yearSel,na_item:"TE",sector:"S13",unit:"PC_GDP",cofog99:cofogs_[i]})}) );
//get data
$.when.apply($, ajaxs)
.then(function() {
//extract data
for(var i=0; i<arguments.length; i++){
var data = arguments[i][0];
var ds = JSONstat(data).Dataset(0);
var geo = ds.Dimension("geo").id[0];
var year = ds.Dimension("time").id[0];
var cofogs = ds.Dimension("cofog99").id;
for(var j=0; j<cofogs.length; j++){
var cofog = cofogs[j];
var d = ds.Data({time:year,geo:geo,cofog99:cofog});
var v = 0;
if(d && d.value) v = d.value;
if(!dataIndex[year]) dataIndex[year]={};
if(!dataIndex[year][geo]) dataIndex[year][geo]={};
dataIndex[year][geo][cofog] = v;
}
}
sb.set(dataIndex[yearSel][geoSel],500);
}, function() {
console.log("Could not load data for " + geoSel + " " + yearSel); //TODO better
});
}
};
//
updateChart();
}, function() {
console.log("Could not load initialisation data");
});
});
});
}(jQuery, window.EstLib = window.EstLib || {} ));
</script>
</body>
</html>