-
Notifications
You must be signed in to change notification settings - Fork 0
/
income_distr.html
335 lines (278 loc) · 14.6 KB
/
income_distr.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf8" />
<link rel="shortcut icon" type="image/png" href="img/favicon.png"/>
<title>Income disparities in Europe</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js" integrity="sha256-eGE6blurk5sHj+rmkfsGYeKyZx3M4bG+ZlFyA7Kns7E=" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css">
<script src="https://d3js.org/d3.v4.min.js"></script>
<!-- <script src="https://d3js.org/d3-scale-chromatic.v1.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/income_distr.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; }
h1 { font-size: 22px; }
#disrect rect {
fill: peru;
stroke-width: 0.5;
stroke: lightgray;
}
#disrect rect:hover {
fill: darkred;
}
#timeslider label {
position: absolute;
width: 20px;
margin-top: 12px;
margin-left: -10px;
text-align: center;
font-size: 10px;
}
.chartLabels { font-size: 14px; }
.arrow {
marker-end: url(#arrow);
stroke: gray;
stroke-width: 2.5px;
}
#averageLine {
stroke: black;
stroke-width: 2px;
opacity: 0.5;
}
.graticuleYline {
stroke: black;
stroke-width: 2px;
opacity: 0.15;
}
.ui-widget {
font-size: 12px !important;
}
#timeslider {
width: 200px;
}
.nodataTxt {
opacity:0;
font-size: 18px;
font-weight:bold;
text-anchor:middle
}
.bckRect { fill: #f5f5f5; }
</style>
</head>
<body>
<h1>
<span id="title1"></span> - <span id="geoTitle">-</span> <span
id="title2"></span> <span id="timeTitle">-</span>
</h1>
<div style="float: right;" id="lgDiv">
<select name="lg" id="lgList"></select>
</div>
<div style="float: left; width: 350px; margin-left: 10px; margin-right: 10px;">
<span id="countryText"></span>
<div id="geoListDiv">
<select name="geoN" id="geoList"></select>
</div>
<div style="margin-top: 8px;">
<span id="yearText"></span>
<div id="timeslider"></div>
</div>
<div style="margin-top: 16px;">
<span id="typeChart"></span>
<div id=typeListDiv>
<select name="typeN" id="typeList"></select>
</div>
</div>
<div id="info" style="width: 350px; margin-top: 25px;">
</div>
</div>
<div id="chart"></div>
<!-- <div>
<p><a id="linkdist2" href="income_distr_2.html"><span id="focusCntr"></span></a></p>
<p><span id="sourceTxt"></span></p>
</div>
-->
<script>
/**
*
* @author julien Gaffuri
*
*/
(function($, EstLib) {
$(function() {
//http://appsso.eurostat.ec.europa.eu/nui/show.do?dataset=ilc_di01&lang=fr
//TODO update on slider move
//get language
var lg = EstLib.getLang2Chars();
//build menu for the choice of language
var lgList = $("#lgList");
// build option group for the shares
$("<option>").attr("value","en").text("English").appendTo(lgList);
$("<option>").attr("value","fr").text("Français").appendTo(lgList);
$("<option>").attr("value","de").text("Deutsch").appendTo(lgList);
$('#lgList option[value="'+lg+'"]').attr('selected','selected');
//select dictionnary
var dict = EstLib.dictIncomeDistr;
dict = dict[lg] || dict.en;
//try to fill page with translated terms
EstLib.writeText(dict);
//write page title
document.title = dict.title;
//build svg element
var rnd = 8;
var margin = {top: 10+rnd, right: 10+rnd, bottom: 40+rnd, left: 30+rnd};
var width = 600 - margin.left - margin.right, height = 350 - margin.top - margin.bottom;
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
var chart = svg.append("g").attr("transform", "translate("+margin.left+","+margin.top+ ")");
//background rectangle
chart.append("rect").attr("class","bckRect")
.attr("width",width+2*rnd).attr("height",height+rnd)
.attr("x",-rnd).attr("y",-rnd)
.attr("rx",rnd).attr("ry",rnd);
var noData = chart.append("g").attr("id","nodata").attr("class","nodataTxt");
noData.append("text").attr("x",width*0.5).attr("y",height*0.5+9).text(dict.nodata);
var rects = chart.append("g").attr("id","disrect");
//chart axis scales
var xScale = d3.scaleLinear().domain([0,100]).range([0, width]);
var yMax = 75; //adapt max? min?
var yScale = d3.scaleLinear().domain([0,yMax]).range([0, height]);
//init chart
var addRect = function(quantileType,quantileNb,pos){
rects.append("rect").attr("x",xScale(pos)).attr("y",yScale(yMax))
.attr("width",xScale(EstLib.quantileDict[quantileType].percentage)).attr("height",yScale(0));
};
//deciles
for(i=1;i<=10;i++) addRect("D",i,10*(i-1));
//first 5 percentiles
for(i=0;i<=4;i++) addRect("P",i+1,i);
//last 5 percentiles
for(i=95;i<=100;i++) addRect("P",i,i-1);
//second twentile
addRect("T",2,5);
//19th twentyfifth
addRect("F",24,90);
//define arrow marker
svg.append("defs").append("marker").attr("id","arrow")
.attr("viewBox","0 -5 10 10").attr("refX",5).attr("refY",0)
.attr("markerWidth",3).attr("markerHeight",3).attr("orient","auto")
.append("path").attr("d", "M0,-5L10,0L0,5");
//draw axis labels
var labelsG = svg.append("g").attr("id","labels").attr("transform", "translate("+margin.left+","+margin.top+ ")");
labelsG.append("text").attr("class","chartLabels").attr("x",47).attr("y",height+17).text(dict.lowincome);
labelsG.append('line').attr("class","arrow").attr("x1",150).attr("y1",height+25).attr("x2",40).attr("y2",height+25);
labelsG.append("text").attr("class","chartLabels").attr("x",width-150).attr("y",height+17).text(dict.higincome);
labelsG.append('line').attr("class","arrow").attr("x1",width-150).attr("y1",height+25).attr("x2",width-45).attr("y2",height+25);
labelsG.append("text").attr("class","chartLabels").attr("transform", "translate(17,"+(height*0.5)+")rotate(-90)").attr("x",0).attr("y",0).text(dict.incomelev);
labelsG.append('line').attr("class","arrow").attr("x1",25).attr("y1",height*0.5+5).attr("x2",25).attr("y2",height*0.5-90);
//draw y graticule
for(var y=20; y<yMax; y+=10)
labelsG.append('line').attr("class","graticuleYline").attr("x1",0).attr("y1",height-yScale(y)).attr("x2",width).attr("y2",height-yScale(y));
//draw average line
labelsG.append('line').attr("id","averageLine")
.attr("x1",0).attr("y1",height-yScale(10)).attr("x2",width).attr("y2",height-yScale(10));
labelsG.append("text")
.attr("x",5).attr("y",height-yScale(10)-3).attr("font-size","11px").text(dict.avincome);
//info div
var infoDiv = d3.select("#info");
//the selection
var geoSel = EstLib.getParameterByName("geo") || "EU28";
var timeSel = EstLib.getParameterByName("time") || "2014";
var typeSel = "1";
//link to other visualisation
/* d3.select("#linkdist2")
.on("click", function() { window.open("income_distr_2.html?"+"time="+timeSel+"&lang="+lg, "_blank"); });
*/
$.when(
//get income distribution data
$.ajax({url:EstLib.getEstatDataURL("ilc_di01",{currency:"EUR",indic_il:"SHARE",
quantile:["P100","P99","P98","P97","P96","P95","D10","D9","D8","D7","D6","D5","D4","D3","D2","D1","P5","P4","P3","P2","P1"]
}, lg
)})
).then(function(data) {
var i;
//simplify geo label texts
EstLib.overrideCountryNames(data.dimension.geo.category.label, lg);
//decode data
data = JSONstat(data).Dataset(0);
//build geolist
var geoList = $("#geoList");
var geos = data.Dimension("geo").id;
geos.splice(geos.indexOf("EA12"),1);
geos.splice(geos.indexOf("EA17"),1);
geos.splice(geos.indexOf("EA18"),1);
geos.splice(geos.indexOf("EU15"),1);
geos.splice(geos.indexOf("EU27"),1);
EstLib.buildGeoList(geoList, geos, function(geo){
return data.Dimension("geo").Category(geo).label;
}, geoSel, function(){
geoSel=geoList.find(":selected").attr("value"); update(EstLib.getDataObj(data, geoSel, timeSel));
});
//build time slider
EstLib.buildTimeSlider($("#timeslider"), data.Dimension("time").id, timeSel, 5, function() {
timeSel= ""+$("#timeslider").slider("value");
update(EstLib.getDataObj(data, geoSel, timeSel));
});
//build menu for the choice of graphs
var typeList = $("#typeList");
// build option group for the shares
var optgroupSh = $("<optgroup>").attr("label","Shares of equivalized income held by...").appendTo(typeList);
$("<option>").attr("value","1").text("deciles/percentiles (as accurate as possible)").appendTo(optgroupSh);
$("<option>").attr("value","2").text("deciles of eq. income").appendTo(optgroupSh);
$("<option>").attr("value","3").text("quartiles of eq. income").appendTo(optgroupSh);
$("<option>").attr("value","4").text("quintiles of eq. income").appendTo(optgroupSh);
var optgroupLz = $("<optgroup>").attr("label","Approximation of Lorenz curve").appendTo(typeList);
$("<option>").attr("value","5").text("as accurate as possible").appendTo(optgroupLz);
$("<option>").attr("value","6").text("through deciles").appendTo(optgroupLz);
$('#typeList option[value="1"]').attr('selected','selected');
typeList
.selectmenu({change:function(){
typeSel=typeList.find(":selected").attr("value");
},width:"auto"})
.selectmenu("menuWidget").css("height","150px");
//update the chart
var update = function(dataObj){
if(!EstLib.dataPresence(dataObj)) {
noData.transition().style("opacity", 1);
labelsG.transition().style("opacity", 0);
} else {
//show
noData.transition().style("opacity", 0);
labelsG.transition().style("opacity", 1);
}
rects.selectAll("rect").data(d3.keys(dataObj))
.on("mouseover", function(qu) {
infoDiv.html( EstLib.getRectText(qu, dataObj[qu], dict, lg) );
})
.on("mouseout", function() {
infoDiv.text("");
})
.transition().attr("y", function(d,i){
var value = dataObj[d] || 0, factor = 10.0/EstLib.quantileDict[d.charAt(0)].percentage;
return yScale(value<0?yMax:yMax-factor*value);
})
.attr("height", function(d){
var value = dataObj[d] || 0, factor = 10.0/EstLib.quantileDict[d.charAt(0)].percentage;
return yScale(factor*Math.abs(value));
});
//select geoSel in list
$('#geoList option[value='+geoSel+']').attr('selected', 'selected');
//update title
d3.select("#geoTitle").text(data.Dimension("geo").Category(geoSel).label);
d3.select("#timeTitle").text(timeSel);
};
update(EstLib.getDataObj(data, geoSel, timeSel));
}, function() {
console.log("Could not load data");
}
);
});
}(jQuery, window.EstLib = window.EstLib || {} ));
</script>
</body>
</html>