forked from eurostat/EurostatVisu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincome_distr_2.html
268 lines (218 loc) · 10.8 KB
/
income_distr_2.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
<!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.11.4/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;
}
.disrect rect:hover { fill: darkred; }
#timeslider label {
position: absolute;
width: 20px;
margin-top: 12px;
margin-left: -10px;
text-align: center;
font-size: 10px;
}
.svgCntr { cursor: pointer; }
.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: 14px;
font-weight:bold;
text-anchor:middle
}
.cntrLbl { font-size: 14px; }
.bckRect { fill: #eee; }
</style>
</head>
<body>
<h1>
<span id="title1"></span> <span id="title2"></span> <span
id="timeTitle">-</span>
</h1>
<div style="padding-bottom: 25px; padding-left: 10px;">
<span id="yearText"></span>
<div id="timeslider"></div>
</div>
<div id="charts"></div>
<div>
<p>
<span id="sourceTxt"></span>
</p>
</div>
<div id="tooltip"></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();
//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;
//dimensions
var rnd = 8;
var margin = {top: 3+rnd, right: 3+rnd, bottom: 3+rnd, left: 3+rnd},
width = 200 - margin.left - margin.right,
height = 130 - margin.top - margin.bottom;
//chart axis scales
var xScale = d3.scaleLinear().domain([0,100]).range([0, width]);
var yMax = 75;
var yScale = d3.scaleLinear().domain([0,yMax]).range([0, height]);
//the selection
var timeSel = EstLib.getParameterByName("time") || "2014";
//info
var tooltip = EstLib.tooltip({width: "300px",font: "16px sans-serif",padding: "10px"});
$.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) {
//simplify geo label texts
EstLib.overrideCountryNames(data.dimension.geo.category.label, lg);
//decode data
data = JSONstat(data).Dataset(0);
//build time slider
EstLib.buildTimeSlider($("#timeslider"), data.Dimension("time").id, timeSel, 5, function() {
timeSel= ""+$("#timeslider").slider("value");
update();
});
//get geos
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);
geos.splice(geos.indexOf("NMS12"),1);
geos.sort(EstLib.geoComparisonEstatPublications);
//build initial charts
for(var j=0; j<geos.length; j++) {
var geo = geos[j];
//build svg element
var svg = d3.select("#charts")
.append("svg")
.attr("id", "svg"+geo)
.attr("class", "svgCntr")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("geo", geo).on("click", function() { window.open("income_distr.html?geo="+d3.select(this).attr("geo")+"&time="+timeSel+"&lang="+lg, "_blank"); });
var chart = svg.append("g").attr("transform", "translate("+(margin.left)+","+(margin.top)+ ")");
//background rectangle
chart.append("rect")
.attr("width",width+2*rnd).attr("height",height+2*rnd)
.attr("x",-rnd).attr("y",-rnd)
.attr("rx",rnd).attr("ry",rnd)
.attr("class","bckRect");
//no data
chart.append("g").attr("id","nodata"+geo).attr("class","nodataTxt")
.append("text").attr("x",width*0.5).attr("y",height*0.5+7).text(dict.nodata);
//rectangles
var rects = chart.append("g").attr("id","disrect"+geo).attr("class","disrect");
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
var i;
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);
//country name title
chart.append("text").attr("x",22).attr("y",16).attr("class","cntrLbl").text( data.Dimension("geo").Category(geo).label );
//flag
chart.append("svg:image")
.attr("x",0).attr("y",0).attr('width', 20).attr('height', 20)
.attr("xlink:href","img/flags/1/"+(geo.substring(0,2))+".png")
}
//update the chart
var update = function(){
for(var i=0; i<geos.length; i++){
var geo = geos[i];
var dataObj = EstLib.getDataObj(data, geo, timeSel);
var rects = d3.select("#disrect"+geo);
var noData = d3.select("#nodata"+geo);
//check if "no data" message should be shown
if(!EstLib.dataPresence(dataObj)) noData.transition().style("opacity", 1);
else noData.transition().style("opacity", 0);
//update rectangles with dataObj (within a closure to preserve dataObj value)
(function(dataObj){
rects.selectAll("rect").data(d3.keys(dataObj))
.on("mouseover", function(qu) { tooltip.mouseover(EstLib.getRectText(qu, dataObj[qu], dict, lg)); })
.on("mousemove", function() { tooltip.mousemove(); })
.on("mouseout", function() { tooltip.mouseout(); })
.transition()
.attr("y", function(qu){
var value = dataObj[qu] || 0, factor = 10.0/EstLib.quantileDict[qu.charAt(0)].percentage;
return yScale(value<0?yMax:yMax-factor*value);
})
.attr("height", function(qu){
var value = dataObj[qu] || 0, factor = 10.0/EstLib.quantileDict[qu.charAt(0)].percentage;
return yScale(factor*Math.abs(value));
});
})(dataObj);
}
//update title
d3.select("#timeTitle").text(timeSel);
};
update();
}, function() {
console.log("Could not load data");
}
);
});
}(jQuery, window.EstLib = window.EstLib || {} ));
</script>
</body>
</html>