-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
770 lines (701 loc) · 21.7 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="https://d3js.org/d3-tile.v0.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/leaflet.esri/1.0.4/esri-leaflet.js"></script>
<style>
@font-face {
font-family: "Arial MT Std";
src: url(https://gfw2-data.s3.amazonaws.com/fires/d3/Dashboard/ArialMTStd-LightCond.otf) format("opentype");
font-weight: normal;
}
@font-face {
font-family: "Arial MT Std";
src: url(https://gfw2-data.s3.amazonaws.com/fires/d3/Dashboard/ArialMTStd-BoldCond.otf) format("opentype");
font-weight: bold;
}
body {
font-family: "Arial MT Std", Helvetica, sans-serif, Arial;
position: relative;
}
h1{
color: #424242;
font-size: 18px;
margin: 5px;
text-align: center;
text-transform: uppercase;
letter-spacing: 1px;
}
h2{
color: #424242;
font-size: 16px;
margin: 0px;
text-align: center;
font-weight: bolder;
}
h3{
display: block;
font-size: 16px;
margin: 15px;
font-weight: normal;
}
h4{
display: block;
margin: 0px;
text-align: center;
font-weight: normal;
font-size: 14px;
}
.background {
fill: none;
pointer-events: all;
}
.mesh {
fill: none;
stroke: #fff;
stroke-linecap: round;
stroke-linejoin: round;
}
.axis path,
.axis line {
fill: none;
stroke: darkgray;
stroke-width: 1;
shape-rendering: crispEdges;
}
.axis text {
font-size: 11px;
}
#info{
z-index: 1;
position:relative;
width:200px;
height:80px;
padding:10px;
background-color:transparent;
border-radius:10px;
margin-left: 10px;
/*box-shadow: 4px 4px 10px rgba(0,0,0,0.1);*/
}
#info2 {
width: 80px;
overflow: hidden;
float: left;
}
#info2 h2 {
font-size: 24px;
text-align: right;
margin-top: 5px;
}
#info3 {
width: 110px;
overflow: hidden;
}
#info3 h2 {
}
#info.hidden{
display:none;
}
#info p{
margin:0px;
font-size:12px;
}
#chartDiv rect{
fill:#D68178;
}
#chartDiv rect.selected{
fill:darkred;
}
#chartDiv svg {
}
#barChart, #pieDiv1, #pieDiv2 {
}
#mapDiv {
width: 550px;
height: 250px;
border: 1px dashed darkgray;
}
#pieDiv1 path {
stroke: #fff;
}
#pieDiv1 text {
font-size: 10px;
fill:white;
}
#pieDiv2 path {
stroke: #fff;
}
#pieDiv2 text {
font-size: 10px;
fill:white;
}
#barChart text {
font-size: 10px;
fill:black;
}
#dashboard{
overflow: hidden;
margin: auto;
width: 800px;
}
#left{
width: 550px;
float: left;
}
#right{
overflow: hidden;
width: 250px;
}
#right svg {
display: block;
margin: auto;
}
</style>
</head>
<body>
<script>
// Copyright (c) 2013 Ryan Clark
// https://gist.github.com/rclark/5779673
L.TopoJSON = L.GeoJSON.extend({
addData: function(jsonData) {
if (jsonData.type === "Topology") {
for (key in jsonData.objects) {
geojson = topojson.feature(jsonData, jsonData.objects[key]);
L.GeoJSON.prototype.addData.call(this, geojson);
}
}
else {
L.GeoJSON.prototype.addData.call(this, jsonData);
}
}
});
</script>
<div id="dashboard">
<div id="left">
<h3>Click on a province to view the trend of fires since 2001</h3>
<div id="mapDiv"></div>
<h3>Hover over a year to analyze where fires happened</h3>
<div id="chartDiv"></div>
</div>
<div id="right">
<div id="info">
<h1 id="place">Kalimantan Tengah</h1>
<div id="info2">
<h2 id="number">30162<h2>
</div>
<div id="info3">
<h2 id="year">Fire Alerts in 2015</h2>
</div>
</div>
<div id="pieDiv1">
<h4>Fire Alerts in <span style="color:#1B5E48; font-weight:bold;">Protected Areas</span></h4>
</div>
<div id="pieDiv2">
<h4>Fire Alerts on <span style="color:#1B455E; font-weight:bold;">Peatland</span></h4>
</div>
<div id="barChart">
<h4>Fires Alerts by Land Use Area</h4>
</div>
</div>
</div>
<script>
var tables = [];
var loadTables = function(){
d3.csv("Tables/Adm1_2001.csv", function(error, data0) {
tables.push(data0);
d3.csv("Tables/Adm1_2002.csv", function(error, data1) {
tables.push(data1);
d3.csv("Tables/Adm1_2003.csv", function(error, data2) {
tables.push(data2);
d3.csv("Tables/Adm1_2004.csv", function(error, data3) {
tables.push(data3);
d3.csv("Tables/Adm1_2005.csv", function(error, data4) {
tables.push(data4);
d3.csv("Tables/Adm1_2006.csv", function(error, data5) {
tables.push(data5);
d3.csv("Tables/Adm1_2007.csv", function(error, data6) {
tables.push(data6);
d3.csv("Tables/Adm1_2008.csv", function(error, data7) {
tables.push(data7);
d3.csv("Tables/Adm1_2009.csv", function(error, data8) {
tables.push(data8);
d3.csv("Tables/Adm1_2010.csv", function(error, data9) {
tables.push(data9);
d3.csv("Tables/Adm1_2011.csv", function(error, data10) {
tables.push(data10);
d3.csv("Tables/Adm1_2012.csv", function(error, data11) {
tables.push(data11);
d3.csv("Tables/Adm1_2013.csv", function(error, data12) {
tables.push(data12);
d3.csv("Tables/Adm1_2014.csv", function(error, data13) {
tables.push(data13);
d3.csv("Tables/Adm1_2015.csv", function(error, data14) {
tables.push(data14);
d3.csv("Tables/Adm1_2016.csv", function(error, data15) {
tables.push(data15);
updatePie(svgPie1,selected_id,selected_year,"Protected","OutProtected");
updatePie(svgPie2,selected_id,selected_year,"Peat","OutPeat");
updateBarChart(svgBarChartArea,selected_id,selected_year);
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
}
var topoAdm1;
var init;
var createMap = function(){
//Variables
var active = d3.select("null");
mapLink = '<a href="https://52.200.102.67/osm_tiles"></a>';
//Layers
var gray = L.esri.basemapLayer("Gray",{opacity: 0.5});
var kd2015 = L.tileLayer(
'https://wri-tiles.s3.amazonaws.com/fires_idn/2015/{z}/{x}/{y}.png', {
attribution: '© ' + mapLink + ' Contributors',
maxZoom: 22,
});
var kdAllYears = L.tileLayer(
'https://wri-tiles.s3.amazonaws.com/fires_idn/all/{z}/{x}/{y}.png', {
attribution: '© ' + mapLink + ' Contributors',
maxZoom: 22,
});
map = new L.map('mapDiv', {
maxZoom:8,
minZoom:4,
center:[-3, 118],
zoom: 4,
layers: [gray, kd2015]
});
L.esri.basemapLayer("GrayLabels", {opacity: 0.5}).addTo(map).bringToFront();
var overlayHeatmap = {
"Heatmap: 2015": kd2015,
"Heatmap: Last 15 Years": kdAllYears
};
L.control.layers(overlayHeatmap).addTo(map);
var legend = L.control({position:'bottomleft'});
legend.onAdd = function (map){
var div = L.DomUtil.create('div', 'legend');
div.innerHTML += '<img src="legend.png" alt="legend" >';
return div;
};
legend.addTo(map);
d3.json("Tables/toposim-IndonesiaFires2.json", function(json){
function style(feature) {
return {
fillColor : 'transparent',
fillOpacity: 1,
color:'#555',
weight:0.3,
opacity:1
};
}
var topoAdm1Data = topojson.feature(json, json.objects.IndonesiaFires);
init = topoAdm1Data.features[14];
var topoAdm1 = new L.TopoJSON(topoAdm1Data, {style:style});
topoAdm1.addTo(map);
topoAdm1.eachLayer(handleLayer);
function handleLayer(layer){
// layer.setStyle(style());
layer.on({
click : clicked,
mouseover: hover,
//mouseout: reset
});
}
var old;
function clicked(e) {
//Change bar chart and also selected adm1 id
var layer = e.target
trendChart.updateTrendChart(layer.feature);
selected_id = layer.feature.properties.ID;
if (selected_year) {
updatePie(svgPie1,selected_id,selected_year,"Protected","OutProtected");
updatePie(svgPie2,selected_id,selected_year,"Peat","OutPeat");
updateBarChart(svgBarChartArea,selected_id, selected_year);
var idname = "F" + (selected_year+2001);
//update info
var info = d3.select("#info");
//info.select("#number").text(formatLong(layer.feature.properties[idname]));
info.select("#number").transition().duration(500)
.tween("text", function(){
var i = d3.interpolateRound(+this.textContent, layer.feature.properties[idname]);
return function(t) {
this.textContent = i(t);
}
});
}
if (old) {
topoAdm1.resetStyle(old);
}
layer.setStyle({
fillColor: 'rgba(145,145,145,0.2)',
weight: 0.5,
})
old = layer;
map.fitBounds(layer.getBounds());
//update info
var info = d3.select("#info");
info.select("#place").text(layer.feature.properties.NAME_1);
}
function hover(e) {
var layer = e.target;
layer.setStyle({
fillColor: 'rgba(145,145,145,0.2)',
});
}
function reset(e) {
// var layer = e.target;
// topoAdm1.resetStyle(layer);
}
});
}
var createTrendChart = function(){
//Trend chart variables
var width = 550, height = 250;
var margins = {top: 15, right: 20, bottom: 40, left: 50};
var chartwidth = width - margins.left - margins.right;
var chartheight = height - margins.top - margins.bottom;
var years = [2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016];
var svgChartArea = d3.select("#chartDiv")
.append("svg")
.attr("width", width)
.attr("height", height);
var chart = svgChartArea.append("g")
.attr("transform","translate("+margins.left + "," + margins.top + ")");
var dataset = [2400,23394,8431,14946,5719,30210,2941,1480,15453,443,6099,6076,3670,13401,30162,972];
//Scales
var xScale = d3.scale.ordinal()
.domain(d3.range(2001,2017))
.rangeRoundBands([0,chartwidth],0.3);
var yScale = d3.scale.linear()
.domain([0, 30000])
.range([chartheight, 0]);
//Define x,y axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(16);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(7);
//Create bar chart
var drawChart = function(){
chart.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i){
return xScale(2001+i);
})
.attr("y", function(d){
return yScale(d);
})
.attr("width", xScale.rangeBand())
.attr("height", function(d){
return chartheight - yScale(d);
})
.attr("class", function(d,i){
if (i == 14) {
return "selected";
}
})
.on("mouseover", function(d,i){
d3.selectAll("rect").classed("selected", false);
d3.select(this).attr("class", "selected");
selected_year = i;
updatePie(svgPie1,selected_id,selected_year,"Protected","OutProtected");
updatePie(svgPie2,selected_id,selected_year,"Peat","OutPeat");
updateBarChart(svgBarChartArea,selected_id, selected_year);
//update info
var info = d3.select("#info");
// info.select("#number").text(formatLong(d));
info.select("#number").transition().duration(500)
.tween("text", function(){
var i = d3.interpolateRound(+this.textContent, d);
return function(t) {
this.textContent = i(t);
}
});
info.select("#year").text("Fire Alerts in " + (i+2001));
});
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (chartheight) + ")")
.call(xAxis);
chart.append("g")
.attr("class", "y axis")
.call(yAxis);
}
drawChart.updateTrendChart = function(selected_data){
//var test = d3.values(selected_data); More efficient?
dataset = [selected_data.properties.F2001,selected_data.properties.F2002,selected_data.properties.F2003,selected_data.properties.F2004,selected_data.properties.F2005,selected_data.properties.F2006,selected_data.properties.F2007,selected_data.properties.F2008,selected_data.properties.F2009,selected_data.properties.F2010,selected_data.properties.F2011,selected_data.properties.F2012,selected_data.properties.F2013,selected_data.properties.F2014,selected_data.properties.F2015,selected_data.properties.F2016];
//yScale.domain([0, d3.max(dataset)])
chart.selectAll("rect")
.data(dataset)
.transition()
.duration(750)
.attr("x", function(d, i){
return xScale(2001+i);
})
.attr("y", function(d){
return yScale(d);
})
.attr("width", xScale.rangeBand())
.attr("height", function(d){
return chartheight - yScale(d);
});
//Update Y axis
chart.select(".y.axis")
.transition()
.duration(750)
.call(yAxis);
};
drawChart();
return drawChart;
}
var formatLong = d3.format(",d");
//Tables and variables for pie charts
var mockdataset = [0,50];
var selected_id = 15;
var selected_year = 14;
var pie_w = 150, pie_h = 150;
//var pie_margins = {top: 10, right: 10, bottom: 0, left: 10};
var pie = d3.layout.pie().sort(null);
var color1 = ["#1B5E48","#B6BFBC"];
var color2 = ["#1B455E","#B6BFBC"];
var color3 = ["#BFA2A7", "#ADBFA2", "#A2A3BF", "#B6BFBC"];
var outerRadius = pie_w / 2;
var innerRadius = 0;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius-10);
var svgPie1 = d3.select("#pieDiv1")
.append("svg")
.attr("width", pie_w)
.attr("height", pie_h);
var svgPie2 = d3.select("#pieDiv2")
.append("svg")
.attr("width", pie_w)
.attr("height", pie_h);
var updatePie = function(pie_object,adm_id,year_id,attr1,attr2) {
var selectedTableYear = tables[year_id];
var selectedRowAdm = selectedTableYear[adm_id-1];
var selectedAttributes = [+selectedRowAdm[attr1], +selectedRowAdm[attr2]];
var tot = d3.sum(selectedAttributes);
var percentages = [(selectedAttributes[0]/tot)*100, (selectedAttributes[1]/tot)*100];
function arcTween(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}
pie_object.selectAll("g.arc")
.data(pie(percentages)).select("text").transition().duration(750)
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.tween("text", function(d) {
var i = d3.interpolateRound(+this.textContent.replace('%', ''), d.value);
return function(t) {
this.textContent = i(t) + '%';
};
});
pie_object.selectAll("g.arc")
.data(pie(selectedAttributes)).select("path").transition().duration(750)
.attrTween("d", arcTween);
}
var createPies = function(){
var drawPie = function(pie_object, color){
var pieArcs = pie_object.selectAll("g.arc")
.data(pie(mockdataset))
.enter()
.append("g")
.attr("class","arc")
.attr("transform", "translate(" + outerRadius + ", " + outerRadius + ")");
pieArcs.append("path")
.each(function(d) { this._current = d; })
.attr("d",arc)
.attr("fill", function(d, i) {
return color[i];
});
pieArcs.append("text");
}
drawPie(svgPie1, color1);
drawPie(svgPie2, color2);
return drawPie;
}
var updateBarChart = function(bar_object,adm_id,year_id){
var selectedTableYear = tables[year_id];
var selectedRowAdm = selectedTableYear[adm_id-1];
var selected_dataset = [+selectedRowAdm.Logging, +selectedRowAdm.OilPalm, +selectedRowAdm.WoodFiber, +selectedRowAdm.OutCon]
var total = d3.sum(selected_dataset);
var perc = [(selected_dataset[0]/total)*100,(selected_dataset[1]/total)*100,(selected_dataset[2]/total)*100,(selected_dataset[3]/total)*100];
//console.log(perc);
//xScale.domain([0, d3.max(selected_dataset)]).ticks(8);
bar_object.selectAll("rect")
.data(perc)
.transition()
.duration(750)
.attr("x", function(d, i){
return xScale(0);
})
.attr("y", function(d,i){
var x = categories[i];
return yScale(x);
})
.attr("width", function(d){
return xScale(d);
})
.attr("height", yScale.rangeBand());
bar_object.selectAll("text")
.data(perc)
.transition().duration(750)
.attr("x", function(d, i){
return xScale(d);
})
.attr("y", function(d,i){
var x = categories[i];
return yScale(x) + 12;
})
.tween("text", function(d) {
var perc = d;
var i = d3.interpolateRound(+this.textContent.replace('%', ''), perc);
return function(t) {
this.textContent = i(t) + '%';
};
});
//Update X axis
bar_object.select(".x.axis")
.transition()
.duration(750)
.call(xAxis);
};
//Trend chart variables
var width = 260, height = 140;
var margins = {top: 10, right: 40, bottom: 20, left: 60};
var chartwidth = width - margins.left - margins.right;
var chartheight = height - margins.top - margins.bottom;
var svgBarChartArea = d3.select("#barChart")
.append("svg")
.attr("width", width)
.attr("height", height);
var barChart = svgBarChartArea.append("g")
.attr("transform","translate("+margins.left + "," + margins.top + ")");
var dataset = [0,0,0,0];
var categories = ["Logging","Oil Palm","Wood Fiber","Outside Concessions"]
//Scales
var yScale = d3.scale.ordinal()
.domain(categories)
.rangeRoundBands([0,chartheight],0.4);
var xScale = d3.scale.linear()
.domain([0, 70])
.range([0, chartwidth]);
//Define x,y axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(6);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
function wrap(text, width) {
text.each(function() {
var text = d3.select(this);
var string = text.text();
if (string.length > 10) {
var words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = -1.5,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", -10).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
}
});
}
var createBarChart = function(){
//Create bar chart
var drawChart = function(){
barChart.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.on("mouseover", function(d,i){
})
.attr("fill", function(d, i) {
return color3[i];
})
.attr("x", function(d, i){
return xScale(0);
})
.attr("y", function(d,i){
var x = categories[i];
return yScale(x);
})
.attr("width", function(d){
return xScale(d);
})
.attr("height", yScale.rangeBand());
barChart.selectAll("text")
.data(dataset)
.enter()
.append("text")
.attr("x", function(d, i){
return xScale(d);
})
.attr("y", function(d,i){
var x = categories[i];
return yScale(x) + 12;
});
barChart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (chartheight) + ")")
.call(xAxis);
barChart.append("g")
.attr("class", "y axis")
.call(yAxis)
.selectAll(".tick text")
.call(wrap, yScale.rangeBand());
};
drawChart();
return drawChart;
}
loadTables();
createMap();
var trendChart = createTrendChart();
var pies = createPies();
var barChart = createBarChart();
</script>
</body>
</html>