forked from eurostat/EurostatVisu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeser.html
581 lines (496 loc) · 18.6 KB
/
timeser.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="shortcut icon" type="image/png" href="img/favicon.png"/>
<title>Inflation 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="js/jquery.mousewheel.min.js"></script>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="js/colorbrewer.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.10.6/moment.min.js"></script>
<script src="js/json-stat.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/rickshaw/1.5.1/rickshaw.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/rickshaw/1.5.1/rickshaw.min.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: 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>
<div
style="float: left; border-top: solid; border-right: solid; border-width: 1px; border-color: #BBB">
<div id="graph" style="width: 1000px; height: 300px;"></div>
<div id="timerange" style="margin-top: 20px;"></div>
<div id="timerange" style="margin-top: 20px;"></div>
</div>
<div id="legend" style="margin-left: 30px; height: 300px;"></div>
<div id="indic" style="margin-top: 40px">
<input type="radio" name="indic" id="manr" value="manr" checked>
<label for="manr">Annual rate of change</label> <input type="radio"
name="indic" id="midx" value="midx"> <label for="midx">Index</label>
<input type="radio" name="indic" id="mmor" value="mmor"> <label
for="mmor">Monthly rate of change</label> <input type="radio"
name="indic" id="mv12r" value="mv12r"> <label for="mv12r">12-month
average rate of change</label>
</div>
<div id="refYearDiv" style="margin-top: 7px;">
Index reference year: <span id="refYearTxt"></span>
<div id="refYearSlider" style="width: 250px; margin-top: 5px"></div>
</div>
<div
style="width: 1000px; margin-top: 20px; padding: 5px; border: solid 1px gray; border-radius: 5px;">
<div>
<label for="geoN">Country</label><br> <select name="geoN"
id="geoList"></select>
</div>
<div style="margin-top: 10px;">
<label for="coicopN">COICOP</label><br> <select name="coicopN"
id="coicopList"></select>
</div>
<div style="margin-top: 10px;">
<button type="button" id="addRemB">Add to chart</button>
</div>
</div>
<div id="interp" style="margin-top: 20px">
Interpolation: <input type="radio" name="interp" id="linear"
value="linear" checked> <label for="linear">Linear</label> <input
type="radio" name="interp" id="cardinal" value="cardinal" checked>
<label for="cardinal">Cardinal</label> <input type="radio"
name="interp" id="basis" value="basis"> <label for="basis">Basis</label>
<input type="radio" name="interp" id="step-after" value="step-after">
<label for="step-after">Step after</label>
</div>
<div style="margin-top: 25px">
Share as URL<br> <input type="text" id="URLtext" name="URLtext"
size="100">
</div>
<div id="dialog" style="display: none;"></div>
<script>
/**
*
* Price statistics time series visualisation
*
* @author julien Gaffuri
*
*/
(function($, EstLib) {
$(function() {
//TODO bug
//file:///H:/psps/visualisation/WebContent/timeser.html?sel=EU,CP00,EA,CP00,US,CP00&indic=midx&iry=1997&dsl=2002M04,2013M08
//file:///H:/psps/visualisation/WebContent/timeser.html?sel=EU,CP00,EA,CP00,US,CP00,EA19,CP0111&indic=midx&iry=1997&dsl=2004M06,2011M06
//when midx and iry and dsl
//caused by re-referencing
//TODO default values: zoom on last figures?
//TODO autocompletion for coicops and countries?
//TODO pan on time range slide - move range
//TODO factorise code into a new pr-eurostat-lib.js file
//TODO on chart, show reference period
//TODO on click, pan with animation
//TODO on double click, zoom with animation
//TODO show data table for on-the-fly download
//TODO test new Rickshaw.Graph.RangeSlider.Preview
//https://raw.githubusercontent.com/shutterstock/rickshaw/master/rickshaw.js
//the dataset - [indic][geo_coicop]
var dsDict = {};
//dictionnary for geo names
var geoDict = {};
//the default reference year. Should be updated when reference year changes
var defaultIndexReferenceYear = 2015;
//the minimum temporal resolution, in seconds by pixel
var temporalResMin = 40000;
//GUI components
//dialog
var dialog = $("#dialog");
//graph
var gDiv = $("#graph");
var graph = new Rickshaw.Graph({
element: gDiv.get(0),
renderer: 'line',
min:"auto",
interpolation: $("input:radio[name=interp]:checked").val(),
series: []
});
graph.renderer.strokeWidth = 1.5;
//redraw graph
var redrawGraph = function(){
//erase previous drawing
gDiv.children("svg").empty();
var legDiv = $("#legend");
legDiv.empty();
//empty series data
graph.series.length = 0;
//fill graph data structure
var selNb = sel.length;
var colors = colorbrewer["Dark2"][Math.min(Math.max(selNb,3),8)];
var indic = $("input:radio[name=indic]:checked").val();
for(var j=0; j<selNb; j++){
var sel_ = sel[j];
var ds = dsDict[indic][sel_.geo+"_"+sel_.coicop];
if(!ds) continue;
serie = {};
serie.color = colors[j%8];
serie.name = ds.Dimension("geo").Category(sel_.geo).label + " - " + EstLib.coicopsDict[sel_.coicop].desc;
serie.data = [];
serie.ds = ds; //necessary to show the flags in tooltip
var months = ds.Dimension("time").Category();
for(var i=0; i<months.length; i++){
var month = months[i].label;
var d = ds.Data({time:month});
if(!d || d.length || (d.value!=0 && !d.value)) continue;
serie.data.push({x:EstLib.prMonthToRTime(month),y:d.value});
}
graph.series.push(serie);
}
graph.render();
//graph.update();
highlightBaseLine();
//build legend
var legend = new Rickshaw.Graph.Legend( {
graph: graph,
element: document.getElementById('legend'),
naturalOrder:true
} );
//change legend style
legDiv.css("background-color","white");
$(".label").css("color","black");
$(".action").css("color","black");
$(".rickshaw_legend").css("color","black");
//onhover event for legend elements
if(sel.length>1){
new Rickshaw.Graph.Behavior.Series.Toggle( {
graph: graph,
legend: legend
} );
new Rickshaw.Graph.Behavior.Series.Highlight({
graph: graph,
legend: legend
});
}
//add "remove button" to legend elements
legDiv.children(0).children().each(function(i,el){
var img = $("<img>")
.attr("src","img/ico-delete.png")
.attr("height","13").attr("width","13")
.attr("title","Remove")
.click(function(){ removeSelection(i); });
el = $(el);
//el.append(img);
el.children("a").after(img);
el.children("img").after( $("<span>").text(" ") );
});
//ensures URL filed is updated when time range slider changes
if(!sliR.ovrDone){
var slideFun = sliR.slider( "option", "slide" );
sliR.slider( "option", "slide", function(event, ui){
slideFun(event, ui);
updateURLTextField();
} );
sliR.ovrDone = true;
}
//ensures initial range values are loaded
if(sliR.initVals){
focusOn(sliR.initVals);
sliR.initVals = null;
}
};
//launch queries to get new data and redraw graph
var getGraphData = function(){
var ajaxs = [];
var indic = $("input:radio[name=indic]:checked").val();
if(!dsDict[indic]) dsDict[indic] = {};
for(var j=0; j<sel.length; j++) {
if(dsDict[indic][ sel[j].geo+"_"+sel[j].coicop ]) continue;
var params = { geo:sel[j].geo, coicop:sel[j].coicop };
if(indic==="midx") params.unit = "I15";
var ajax = $.ajax({url:EstLib.getEstatDataURL("prc_hicp_"+indic, params )});
ajax.prSel = sel[j];
ajaxs.push(ajax);
}
if(ajaxs.length>0)
$.when.apply($, ajaxs)
.then(function() {
if(!dsDict[indic]) dsDict[indic] = {};
for(var j=0; j<sel.length; j++) {
var key = sel[j].geo+"_"+sel[j].coicop;
if(dsDict[indic][key]) continue;
var data = arguments.length==3&&arguments[1]==="success" ? arguments[0] : arguments[j][0];
EstLib.overrideCountryNames(data.dimension.geo.category.label);
data = JSONstat(data).Dataset(0);
//if index and new index reference year, re-reference
if(indic==="midx" && indRefYearSlid.slider("value")!=defaultIndexReferenceYear)
EstLib.rereference(data, indRefYearSlid.slider("value"), key);
dsDict[indic][key] = data;
}
redrawGraph();
}, function() {
var sel__ = arguments[0].prSel;
dialog.html("No data available for:<br>Country: "+geoDict[sel__.geo]+"<br>COICOP: "+EstLib.coicopsDict[sel__.coicop].desc);
dialog.dialog({title: "No data"});
sel.splice(sel.indexOf(sel__),1);
//redrawGraph();
});
else
redrawGraph();
};
//zoom/pan graph
var focusOn = function(values){
sliR.slider("values", values);
graph.window.xMin = values[0];
graph.window.xMax = values[1];
graph.render(); //graph.update();
updateURLTextField();
highlightBaseLine();
};
var pan = false, xC=0;
gDiv.mousedown(function(e){
pan = true;
xC = e.offsetX;
});
gDiv.mouseup(function(){
pan = false;
xC = 0;
});
gDiv.mousemove(function(e){
var sv = sliR.slider("values");
if(!pan) return;
var dt = xC - e.offsetX; //in pixel screen
dt = dt / gDiv.width(); //in graph width ratio
dt = dt*(sv[1]-sv[0]); //in time
var dom = graph.dataDomain();
var newV0 = sv[0] + dt;
if(newV0 < dom[0]) return;
var newV1 = sv[1] + dt;
if(newV1 > dom[1]) return;
if(newV0>=newV1) return; //should not happen.
focusOn([newV0,newV1]);
xC = e.offsetX;
});
gDiv.mousewheel(function(e) {
var sv = sliR.slider("values");
var w = gDiv.width();
var dt = 0.2 * (sv[1]-sv[0]);
dt = e.deltaY>0?dt:-dt;
var r = e.offsetX / w;
var dom = graph.dataDomain();
var newV0 = Math.max(dom[0], sv[0] + r*dt);
var newV1 = Math.min(dom[1], sv[1] - (1-r)*dt);
//zoom-in threshold, based on temporal resolution
if( (newV1-newV0)/w < temporalResMin) {
var mid = 0.5*(sv[0]+sv[1]);
dt = 0.5*w*temporalResMin;
newV0 = mid - dt;
newV1 = mid + dt;
}
focusOn([newV0,newV1]);
});
//axis
new Rickshaw.Graph.Axis.Time({ graph: graph });
new Rickshaw.Graph.Axis.Y({ graph: graph });
var highlightBaseLine = function(){
var indic = $("input:radio[name=indic]:checked").val();
$("text:contains("+(indic==="midx"?100:0)+")").prev().attr("style","stroke:rgb(0,0,0);stroke-width:1;");
};
//graph tooltips
new Rickshaw.Graph.HoverDetail( {
graph: graph,
formatter: function(series, x, y) {
var date = moment(new Date(x * 1000)).format("MMM YYYY");
var flag = series.ds.Data({time:EstLib.rTimeToPrMonth(x)}).status;
if(flag === "e") flag = " (estimated)";
return series.name.replace(" - ","<br>") + "<br>" + date + "<br><b>" + y.toFixed(1) + "</b>" + (flag||"");
},
xFormatter : function() { return ""; }
} );
//timerange slider - see https://jqueryui.com/slider/#range
var sliR = $("#timerange");
new Rickshaw.Graph.RangeSlider( {
graph: graph,
element: sliR.get(0)
} );
var dsl = EstLib.getParameterByName("dsl");
if(dsl){
dsl = dsl.split(",");
if(dsl.length==2){
var d0 = +EstLib.prMonthToRTime(dsl[0]);
var d1 = +EstLib.prMonthToRTime(dsl[1]);
if(!isNaN(d0)&&!isNaN(d1)&&d0<d1) sliR.initVals = [d0,d1];
}
}
//indicator option
var indic = EstLib.getParameterByName("indic");
if(indic) $('input:radio[name=indic][value='+indic+']').attr('checked',true);
$("input[name='indic']").change(function () {
if( $("input:radio[name=indic]:checked").val() === "midx" ) indRefYearDiv.show(); else indRefYearDiv.hide();
getGraphData();
updateURLTextField();
});
$("#indic").buttonset();
//index reference year slider
var indRefYearDiv = $("#refYearDiv");
var indRefYearSlid = $("#refYearSlider");
var iry = EstLib.getParameterByName("iry");
indRefYearSlid.slider({
value: (iry?iry:defaultIndexReferenceYear),
step: 1,
slide: function(e,ui){ updateIndexReferenceYear(ui.value); }
});
var updateIndexReferenceYear = function(refYearFin){
$("#refYearTxt").text(refYearFin);
//compute re-referencing
for(var dsKey in dsDict.midx) EstLib.rereference(dsDict.midx[dsKey], refYearFin, dsKey);
//update graph
redrawGraph();
updateURLTextField(refYearFin);
};
if( $("input:radio[name=indic]:checked").val() === "midx" ) indRefYearDiv.show(); else indRefYearDiv.hide();
//geos list
var geoList = $("#geoList");
//coicops list
var coicopList = $("#coicopList");
EstLib.fillCoicopList(coicopList);
$('#coicopList option[value="CP00"]').attr('selected', 'selected');
coicopList.selectmenu({
change : function(){ updateAddRemButtonText(); },
width:800
}).selectmenu("menuWidget").css("height","300px");
//add/remove button
var sel = EstLib.getParameterByName("sel");
if(sel){
var sel_ = sel.split(",");
sel = [];
for(var i=0; i<sel_.length; i=i+2) sel.push({geo:sel_[i],coicop:sel_[i+1]});
} else
sel = [
{geo:"EU",coicop:"CP00"},
{geo:"EA",coicop:"CP00"},
{geo:"US",coicop:"CP00"}
];
var removeSelection = function(i){
sel.splice(i,1);
updateAddRemButtonText();
updateURLTextField();
redrawGraph();
};
var addRemButton = $("#addRemB"); //addRemButton.button(); $("button").button();
addRemButton.click(function(){
var geoSel = geoList.val();
var coicopSel = coicopList.val();
//check if already selected, remove. Otherwise, add.
for(var j=0; j<sel.length; j++)
if(geoSel === sel[j].geo && coicopSel === sel[j].coicop) {
removeSelection(j);
return;
}
sel.push( {geo:geoSel,coicop:coicopSel} );
updateAddRemButtonText();
getGraphData();
updateURLTextField();
});
var updateAddRemButtonText = function(){
//check if already selected
for(var j=0; j<sel.length; j++)
if(geoList.val() === sel[j].geo && coicopList.val() === sel[j].coicop) {
addRemButton.button("option", "label", "Remove from chart");
return;
}
addRemButton.button("option", "label", "Add to chart");
};
addRemButton.button();
//interpolation option
var interp = EstLib.getParameterByName("interp");
if(interp) $('input:radio[name=interp][value='+interp+']').attr('checked',true);
$("input[name='interp']").change(function () {
graph.interpolation = $("input:radio[name=interp]:checked").val();
graph.render();
updateURLTextField();
});
$("#interp").buttonset();
//sharing URL
var updateURLTextField = function(refYearFin){
var params = [];
//sel
var buf = [];
for(var i=0; i<sel.length; i++) buf.push(sel[i].geo,sel[i].coicop);
params.push({p:"sel",v:buf.join(",")});
//indic
var indic = $("input:radio[name=indic]:checked").val();
if(indic != "manr") params.push({p:"indic",v:indic});
//index reference year
var iry = refYearFin || indRefYearSlid.slider("value");
if(iry != defaultIndexReferenceYear) params.push({p:"iry",v:iry});
//interp
var interp = $("input:radio[name=interp]:checked").val();
if(interp != "cardinal") params.push({p:"interp",v:interp});
//dsl
var sv = sliR.slider("values");
if(!isNaN(sv[0]) && !isNaN(sv[1])){
var dom = graph.dataDomain();
if(dom[0]!=sv[0] || dom[1]!=sv[1]){
params.push({p:"dsl",v:EstLib.rTimeToPrMonth(+sv[0])+","+EstLib.rTimeToPrMonth(+sv[1])});
}
}
buf = [];
for(i=0; i<params.length; i++) buf.push(params[i].p+"="+params[i].v);
$("#URLtext").val( window.location.origin + window.location.pathname + (buf.length>0?"?"+buf.join("&"):"") );
};
//get initial information
$.when(
$.ajax({url:EstLib.getEstatDataURL("prc_hicp_manr",{coicop:["CP00","CP01","CP02"],time:["2010M01","2015M01","2016M01","2017M01","2020M01"]})}),
$.ajax({url:EstLib.getEstatDataURL("prc_hicp_midx",{coicop:["CP00","CP01","CP02"],geo:["EU","EA"]})})
).then(function(geos, months) {
//get geo info
geos = geos[0];
geoDict = geos.dimension.geo.category.label;
EstLib.overrideCountryNames(geoDict);
geos = JSONstat(geos).Dataset(0);
//build geo list
EstLib.buildGeoList(geoList, geos.Dimension("geo").id, function(geo){return geoDict[geo];}, null, function(){
updateAddRemButtonText();
}, 800, 200);
//get time info
months = months[0];
months = JSONstat(months).Dataset(0).Dimension("time").id.sort();
indRefYearSlid.slider("option", "min", +months[0].substring(0,4));
indRefYearSlid.slider("option", "max", +months[months.length-1].substring(0,4));
//various updates
updateAddRemButtonText();
$("#refYearTxt").text( indRefYearSlid.slider("value") );
var indic = $("input:radio[name=indic]:checked").val();
var iry = indRefYearSlid.slider("value");
if(indic === "midx" && iry != defaultIndexReferenceYear) updateIndexReferenceYear(iry);
updateURLTextField();
//first graph refresh
getGraphData();
}, function() {
console.log("Could not retrieve initial information");
});
});
}(jQuery, window.EstLib = window.EstLib || {} ));
</script>
</body>
</html>