forked from Adam-Toth/supply_dashboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
567 lines (463 loc) · 21.6 KB
/
index.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
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" name="viewport">
<meta name="author" content="Adam Toth">
<meta property="og:title" content="Iraq NFI Supply Pipeline">
<meta property="og:type" content="article">
<meta property="og:site_name" content="Iraq Supply Pipeline">
<meta property="og:description" content="Iraq Supply Pipeline">
<title>Iraq Supply Pipeline</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- UNHCR Logo -->
<div style="position: relative;float: right; top: -18px" >
</div>
<!-- Styles -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/dc.css">
<link rel="stylesheet" type="text/css" href="css/leaflet.css" />
<link rel="stylesheet" type="text/css" href="css/introjs.css">
<link rel="stylesheet" type="text/css" href="css/introjs-nazanin.css">
<link rel="stylesheet" type="text/css" href="css/iraq_dashboard.css">
<!-- JS -->
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/intro.js"></script>
<script type="text/javascript" src="js/d3_002.js"></script>
<script type="text/javascript" src="js/crossfilter.js"></script>
<script type="text/javascript" src="js/dc.js"></script>
<script type="text/javascript" src="js/d3.js"></script>
<script type="text/javascript" src="js/d3.min.js"></script>
<script type="text/javascript" src="js/d3.tip.min.js"></script>
<script type="text/javascript" src="js/leaflet.js"></script>
<script type="text/javascript" src="js/jquery.csv-0.71.min.js"></script>
<script type="text/javascript" src="js/miso.ds.deps.min.js"></script>
<!-- Favicon -->
</head>
<body>
<!-- Loading screen -->
<div class="loader"></div>
<!-- design page layout with bootsrap -->
<div class="container" id="dashboard">
<div class="row">
<div class="col-md-8">
<h1><strong>Shelter / NFI Cluster Dashboard - Iraq. </strong></h1>
<h5>Click on any of the graphs below to filter and slice the data. Note that agreggated quantities might be misleading, start filtering on item first.</h5>
</div>
<div style="text-align:center" class="col-md-4">
<img src="images/logo.png" >
</div>
<div class="row">
<div style="text-align:left;float: left" class="col-md-8" data-position="right"><a class="reset btn btn-primary btn-sm" data-step="8" data-intro="Click this button to reset all values or click 'reset' next to the title of a filtered chart." data-position="right" id="reset" href="javascript:dc.filterAll();dc.redrawAll();">Reset All</a></div>
<div style="text-align:right;float: right" class="col-md-4"><a class="btn btn btn-success" href="javascript:void(0);" onclick="javascript:introJs().start();">Tutorial</a>
</div>
</div>
<div class="row">
<div style="height: 30px">
<!--
<div id="item_value" class="col-md-4 dc-chart" data-step="1" data-intro="Click on this chart to filter values by item group. The chart scale changes dynamically." data-position="right" style="text-align:left"><h5>NFI Values by Item Group (USD) <a class="reset" href="javascript:value_chart.filterAll();dc.redrawAll();"style="display: none;">reset</a></h5></div>
-->
<div id="itm_qty" class="col-md-4 dc-chart" style="text-align:left" data-step="2" data-intro="Click on this chart to filter item quantities by group. The chart scale changes dynamically." data-position="right" >
<h5>NFI Quantities by item type
<a class="reset" href="javascript:itm_qty_chart.filterAll();dc.redrawAll();"style="display: none;">reset</a></h5></div>
<div id="proc_total_status" class="col-md-4 dc-chart" data-step="3" data-intro="Click on this pie chart to filter items by status." data-position="left" style="text-align:left" >
<h5>NFI quantity by organisation<a class="reset" href="javascript:proc_total_status_chart.filterAll();dc.redrawAll();"style="display: none;">reset</a></h5>
</div>
</div>
<div class="row">
<div id="proc_total_target" class="col-md-4 dc-chart" data-step="4" data-intro="Click on this chart to filter items/activities by the targeted population." data-position="left" style="text-align:left" >
<h5>NFI type by Category (number of Item)
<a class="reset" href="javascript:proc_total_target_chart.filterAll();dc.redrawAll();"style="display: none;">reset</a></h5></div>
<div id="proc_total_op" class="col-md-4 dc-chart" data-step="5" data-intro="Click on this chart to filter items/activities by the operation." data-position="left" style="text-align:left" >
<h5>NFI Quantity by Operational area
<a class="reset" href="javascript:proc_total_op.filterAll();dc.redrawAll();"style="display: none;">reset</a></h5></div>
<div id="proc_trend" class="col-md-6 dc-chart" data-step="6" data-intro="Select a timeframe to restrict activities to that interval only." data-position="top" style="text-align:center" >
<h5>Supply Timeline <a class="reset" href="javascript:proc_trend_chart.filterAll();dc.redrawAll();"style="display: none;">reset</a></h5></div>
</div>
<!--
<div id="map" class="col-md-6 dc-chart" data-step="7" data-intro="Hover the mouse over a governorate to see the value of items released." data-position="left" style="text-align:left; margin: 20px" >
<h5></h5>
</div>
-->
<div class="row">
<div></div>
<div></div>
</div>
<script>
// loading screen code
$(window).load(function() {
$(".loader").fadeOut("slow");
});
//declaration of chart names and types
// var value_chart = dc.rowChart("#item_value");
var proc_total_status_chart = dc.pieChart("#proc_total_status");
var proc_total_target_chart = dc.rowChart("#proc_total_target");
var proc_total_op = dc.rowChart("#proc_total_op");
var itm_qty_chart = dc.rowChart("#itm_qty");
var proc_trend_chart = dc.barChart("#proc_trend");
// Miso dataset importing google spreadsheet by spreadsheet ID
var ds = new Miso.Dataset({
importer : Miso.Dataset.Importers.GoogleSpreadsheet,
parser : Miso.Dataset.Parsers.GoogleSpreadsheet,
key : "1dqTTsaSTST8LgYGShZ4ZtOKx8-fCZqQI5SW1jBRbH6Q",
worksheet : "1"
});
// declare array to be filled with data from Google spreadsheet
ds.fetch({
success : function() {
var spreadsheetdata = [];
this.each(function(row){ spreadsheetdata.push(row);});
//create a goupped view of only location codes and values
var groupped = this.groupBy("A1Code",["Value"]);
groupped = groupped.where({columns: ["A1Code","Value"],
rows: function(row){
return row.A1Code != null;
}
});
//create separate json from groupped view of locations and values
var Location_json = groupped.toJSON();
//formatting of numbers, dates
var numberFormat = d3.format(",f");
var dateFormat = d3.time.format("%Y-%d-%m");
var monthNameFormat = d3.time.format("%B");
//crossfilter data
var cf = crossfilter(spreadsheetdata);
//declare crossfilter dimensions to be used in charts
cf.targetpop = cf.dimension(function (d) { return d.Target_Population; });
cf.op = cf.dimension(function (d) { return d.Operation; });
cf.item = cf.dimension(function (d) { return d.Item;});
cf.status = cf.dimension(function (d) { return d.Status; });
cf.location = cf.dimension(function (d) {return d.Location; });
//group by dimensions
var targetpop = cf.targetpop.group();
var op = cf.op.group();
// var date = cf.date.group();
var item = cf.item.group();
var status = cf.status.group();
var location = cf.location.group();
var all = cf.groupAll();
//sum values by dimension
var op_by_total = cf.op.group().reduceSum(function (d) {
return d.Value;
});
var item_total_quantity = cf.item.group().reduceSum(function (d) {
return d.Quantity;
});
var status_by_total = cf.status.group().reduceSum(function (d) {
return d.Value;
});
var item_total_value = cf.item.group().reduceSum(function (d) {
return d.Value;
});
var targetpop_by_total = cf.targetpop.group().reduceSum(function (d) {
return d.Value;
});
var location_by_value = cf.location.group().reduceSum(function (d) {
return d.Value;
});
//parse date from data array
var parseDate = d3.time.format("%Y-%d-%m").parse;
spreadsheetdata.forEach(function(d) {
d.date = parseDate(d.Date);
d.month = d3.time.month(d.date);
});
//create date dimension by month
var datedimension = cf.dimension(function(d) {return (d.month);});
//remove values where date is empty
datedimension.filter("");
cf.remove();
datedimension.filterAll();
//sum values by date and calculate minimum and maximum dates in the dataset
var value_by_date = datedimension.group().reduceSum(function(d) {return(d.Value);});
var minDate = datedimension.bottom(1)[0].date;
var maxDate = datedimension.top(1)[0].date;
// Implement bookmarking chart filters status
// Serializing filters values in URL
function getFiltersValues() {
var filters = [
// { name: 'value', value: value_chart.filters()},
{ name: 'proc', value: proc_total_status_chart.filters()},
{ name: 'proc2', value: proc_total_target_chart.filters()},
{ name: 'itm_qty', value: itm_qty_chart.filters()},
{ name: 'op', value: proc_total_op.filters()},
{ name: 'proc_trend', value: proc_trend_chart.filters()}
];
var recursiveEncoded = $.param(filters);
location.hash = recursiveEncoded;
}
// Init chart filters
function initFilters() {
// Get hash values
var parseHash = /^#sector=([A-Za-z0-9,_\-\/\s]*)&status=([A-Za-z0-9,_\-\/\s]*)&org=([A-Za-z0-9,_\-\/\s]*)&map=([A-Za-z0-9,_\-\/\s]*)$/;
var parsed = parseHash.exec(decodeURIComponent(location.hash));
function filter(chart, rank) { // for instance chart = sector_chart and rank in URL hash = 1
// sector chart
if (parsed[rank] == "") {
chart.filter(null);
}
else {
var filterValues = parsed[rank].split(",");
for (var i = 0; i < filterValues.length; i++) {
chart.filter(filterValues[i]);
}
}
}
if (parsed) {
filter(value_chart, 1);
filter(proc_chart, 2);
filter(itm_qty_chart, 3);
filter(proc_per_itm_chart, 4);
filter(proc_trend_chart, 5);
filter(proc_total_op, 6);
}
}
// tooltips for row chart
var rowTip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function (d) {
return "<span style='color: #fff'>" + d.key + "</span> : " + numberFormat(d.value);
});
// tooltips for piechart
var pieTip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function (d) {
return "<span style='color: #fff'>" + d.data.key + "</span> : " + numberFormat(d.value);
});
// tooltip for bar chart (not used)
var barTip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function (d) {
return "<span style='color: #fff'>" + monthNameFormat(d.key) + "</span> : " + numberFormat(d.value) + " USD";
});
// Item value chart
/*
value_chart
.width(400)
.height(485)
.dimension(cf.item)
.group(item_total_value = item_total_value || 0)
.elasticX(true)
.renderLabel(true)
.renderTitle(false)
.on("filtered", getFiltersValues)
.xAxis().ticks(6).tickFormat(d3.format("$,s"));
*/
// Item quantity chart
itm_qty_chart
.width(400)
.height(550)
.dimension(cf.item)
.on("filtered", getFiltersValues)
.group(item_total_quantity = item_total_quantity || 0)
.elasticX(true)
.renderTitle(false)
.renderLabel(true)
.xAxis().ticks(6).tickFormat(d3.format("s"));
// Status chart
proc_total_status_chart
.width(300)
.height(200)
.dimension(cf.status)
.group(status_by_total = status_by_total || 0)
.radius(100)
.innerRadius(25)
.slicesCap(15)
//.legend(dc.legend().gap(3))
.colors(d3.scale.category20())
.renderLabel(true)
.renderTitle(false)
.on("filtered", getFiltersValues);
// Target population chart
proc_total_target_chart
.width(300)
.height(100)
.dimension(cf.targetpop)
.group(targetpop_by_total = targetpop_by_total || 0)
.elasticX(false)
.renderLabel(true)
.renderTitle(false)
.on("filtered", getFiltersValues)
.xAxis().ticks(4).tickFormat(d3.format(",s"));
// Operation chart
proc_total_op
.width(300)
.height(120)
.dimension(cf.op)
.group(op_by_total = op_by_total || 0)
.elasticX(false)
.renderLabel(true)
.renderTitle(false)
.on("filtered", getFiltersValues)
.xAxis().ticks(4).tickFormat(d3.format(",s"));
// Timeline bar chart
proc_trend_chart
.height(200)
.width(750)
.renderHorizontalGridLines(true)
.dimension(datedimension)
.group(value_by_date)
// .renderTitle(true)
.title(function (d) { return monthNameFormat(d.key) + ": " + numberFormat(d.value); })
// define scale based on minimum and maximum dates
.x(d3.time.scale().domain([minDate,maxDate]))
.xUnits(d3.time.months)
.on("filtered", getFiltersValues)
.brushOn(true)
// .gap(10)
.elasticY(false)
// rotate and move X axis labels
.renderlet(function (chart) {
chart.selectAll("g.x text")
.attr('dx', '-30')
.attr('transform', "rotate(-45)");
})
.margins({left: 70 ,top: 10, bottom: 50, right: 50})
.yAxis().ticks(7).tickFormat(d3.format(",s"));
//Map
//Import admin level 1 geojson
/*
d3.json("data/admin1.geojson", function(json) {
// extend admin level 1 map geojson features with values,
//paired by location code that appears on both the map and the dataset
for (var i = 0; i < Location_json.length; i++) {
var dist_Location = Location_json[i].A1Code;
var dist_Value = parseFloat(Location_json[i].Value);
for (var j = 0; j < json.features.length; j++) {
var jsonGov = json.features[j].properties.A1Code;
if (dist_Location == jsonGov) {
json.features[j].properties.Value = dist_Value;
break;
}
}
}
var geojson;
// declare leaflet map with an empty tile layer
var map = L.map('map', { zoomControl: false }).setView([33.15, 43.8], 6);
L.tileLayer('', {
attribution: "This map does not imply official endorsement or acceptance by the United Nations"
})
.addTo(map);
// disable map user interaction including dragging, zoom, clicking, keyboard, etc.
map.dragging.disable();
map.touchZoom.disable();
map.doubleClickZoom.disable();
map.scrollWheelZoom.disable();
map.boxZoom.disable();
map.keyboard.disable();
// control that shows Governorate info on hover
var info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
// declare what to show in information control
info.update = function (props) {
this._div.innerHTML = '<h4>Released Value</h4>' + (props ?
'<b>' + props.A1NameEn + '</b><br />' + numberFormat(props.Value = props.Value || 0) + ' USD'
: 'Hover over a Governorate');
};
info.addTo(map);
// get color depending on released value
function getColor(d) {
return d > 5000000 ? '#084594' :
d > 2500000 ? '#2171b5' :
d > 1000000 ? '#4292c6' :
d > 500000 ? '#6baed6' :
d > 250000 ? '#9ecae1' :
d > 100000 ? '#c6dbef' :
// d > 50000 ? '#deebf7' :
'#f7fbff';
}
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '3',
fillOpacity: 0.7,
fillColor: getColor(feature.properties.Value)
};
}
// highlight selected governorate
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
});
if (!L.Browser.ie && !L.Browser.opera) {
layer.bringToFront();
}
info.update(layer.feature.properties);
}
// reset highlight back to normal
function resetHighlight(e) {
geojson.resetStyle(e.target);
info.update();
}
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight
});
}
// add style to geojson feature and add to map
geojson = L.geoJson(json, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
// legend control (not used)
var legend = L.control({position: 'bottomleft'});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 50000, 100000, 250000, 500000, 1000000, 2000000, 2500000],
labels = [],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + getColor(from + 1) + '"></i> ' +
from + (to ? '–' + to : '+'));
}
div.innerHTML = labels.join('<br>');
return div;
};
//legend.addTo(map);
});
*/
initFilters();
dc.renderAll();
// select and call d3-tip for charts
d3.selectAll("g.row").call(rowTip);
d3.selectAll("g.row").on('mouseover', rowTip.show)
.on('mouseout', rowTip.hide);
d3.selectAll(".pie-slice").call(pieTip);
d3.selectAll(".pie-slice").on('mouseover', pieTip.show)
.on('mouseout', pieTip.hide);
d3.selectAll("g.stack").call(barTip);
d3.selectAll("g.stack").on('mouseover', barTip.show)
.on('mouseout', barTip.hide);
// });
}
});
</script>
<script src="js/bootstrap.js"></script>
<div class="d3-tip n" style="position: absolute; display: none; box-sizing: border-box; top: 186.9px; left: 727.5px;"></div><div class="d3-tip" style="position: absolute; display: none; box-sizing: border-box;">
<div></div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-55986212-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>