-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.html
252 lines (196 loc) · 7.3 KB
/
index2.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Cost of damage of Hurricane Harvey (New York Times)</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Exo|Red+Hat+Display">
<style>
body{
font-family: Arial, Helvetica, sans-serif;
font-family: "Exo", "Red Hat Display", Segoe, "sans-serif";*/;
font-size: 14px;
text-align: justified;
/* text-align: center; */
}
div.tooltip {
padding: 2px;
font: 14px;
/* background: white; */
border: 0px;
border-radius: 8px;
pointer-events: none;
}
</style>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://cdnjs.com/libraries/d3-legend"></script>
<!-- Create a div where the graph will take place -->
<div id="plot"></div>
<div id="plot_bar"></div>
<body>
<script>
// https://d3-graph-gallery.com/graph/heatmap_style.html
let url_data = "https://raw.githubusercontent.com/okiane/HASS-assignment5/gh-pages/data/events-US-1980-2017.csv"
// set the dimensions and margins of the graph
const margin = {top: 50, right: 50, bottom: 100, left: 50},
width = 800 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#plot_bar")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
//Read the data
d3.csv(url_data, function(data) {
// console.log(data)
// console.log(data)
// Labels of row and columns -> unique identifier of the column called 'group' and 'variable'
const col_x_name = "Event"
const col_y_name = "CPIAdjustedEstimatedCost(inBillions)"
const val = "CPIAdjustedEstimatedCost(inBillions)"
const label_x = "Disaster"
const label_y = "Damage Cost ($ in Billions)"
// Filter data
var data = data.sort(function(a, b) {
return d3.descending(+a[val], +b[val]);
}).slice(0, 5);
const col_x = d3.map(data, function(d){return d[col_x_name];}).keys()
const col_y = d3.map(data, function(d){return d[col_y_name];}).keys()
const list_x = d3.set(data, function(d) {return d[col_x_name]}).values().sort((a, b) => a - b);
const list_y = d3.set(data, function(d) {return d[col_y_name]}).values().sort((a, b) => a - b);
const max_y = d3.max(data, function(d) {
d[val] = parseInt(d[val])
return d[val]})
// list_x_name = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
// 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
// Add X axis:
var x = d3.scaleBand()
.range([ 0, width ])
.domain(data.map(function(d) { return d["Event"]; }))
.padding(0.05);
svg.append("g")
.style("font-size", 15)
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).tickSizeOuter(0))
.selectAll("text")
.attr("transform", "translate(-10,0) rotate(-30)")
.style("text-anchor", "end")
// .select(".domain")
;
// Add Y axis:
var y = d3.scaleLinear()
.domain([0, max_y / 100 * 110])
.range([ height, 0]);
svg.append("g")
.style("font-size", 15)
.call(d3.axisLeft(y).tickSizeOuter(0))
.select(".domain");
// Add X axis label:
svg.append("text")
.attr("text-anchor", "end")
.attr("x", width)
.attr("y", height + margin.top - 10)
.text(label_x);
// Add Y axis label:
svg.append("text")
.attr("text-anchor", "end")
.attr("transform", "rotate(-90)")
.attr("y", -margin.left + 10)
.attr("x", -margin.top + 50)
.text(label_y);
// Create a tooltip
var tooltip = d3.select("#plot_bar")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "white")
.style("border", "solid")
.style("border-width", "2px")
.style("border-radius", "5px")
.style("padding", "5px");
// Three functions that change the tooltip when user hovers / moves / leaves a cell
var mouseover = function(d) {
tooltip
.style("opacity", 1)
d3.select(this)
.style("stroke", "grey")
.style("stroke-width", 4)
.style("opacity", 1)
}
var mousemove = function(d) {
tooltip
.html("<b>[" + d["DisasterType"] + "] " + d["Event"]
+ "</b> that lasted <b>" + d["DurationDays"] + "</b> days"
+ "(from " + d["DateStart"] + d["DateEnd"] + ")"
+ " costing <b> USD$" + d["CPIAdjustedEstimatedCost(inBillions)"] + " billion</b>"
+ " (<b>" + (d["CPIAdjustedEstimatedCost(inBillions)"] / max_y * 100).toFixed(2) + " % </b> of Hurricane Katrina's cost of damage)"
+ " and causing <b>" + d["Deaths"] + "</b> death(s)" + ".<br>"
+ "Summary: " + d["Summary"] + "<br>"
)
.style("left", (d3.mouse(this)[0]+70) + "px")
.style("top", (d3.mouse(this)[1]) + "px")
}
var mouseleave = function(d) {
tooltip
.style("opacity", 0)
d3.select(this)
.style("stroke", "none")
// .style("opacity", 0.8)
.filter(function(d) {return d["Event"]=="Hurricane Harvey"; })
.style("stroke", "yellow")
.style("stroke-width", 4)
.style("opacity", 1)
}
// Color scale
var ColourScale = d3.scaleLinear()
.range(['maroon', 'black'])
.domain([d3.min(data, function(d) {
return d[val];
}),
d3.max(data, function(d) {
return d[val];
})
])
// Bars
svg.selectAll("plot_bar")
.data(data)
.enter()
.append("rect")
.attr("x", function(d) { return x(d[col_x_name]); })
.attr("y", function(d) { return y(d[col_y_name]); })
.attr("width", x.bandwidth())
.attr("height", function(d) { return height - y(d[val]); })
.style("fill", function(d) { return ColourScale(d[val])} )
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
.filter(function(d) {return d["Event"]=="Hurricane Harvey"; })
.style("stroke", "yellow")
.style("stroke-width", 4)
.style("opacity", 1);
// Add title to graph
svg.append("text")
.attr("x", 0)
.attr("y", -20)
.attr("text-anchor", "left")
.style("font-size", "22px")
.text("Cost of damage of Hurricane Harvey");
// Add subtitle to graph
svg.append("text")
.attr("x", 0)
.attr("y", -5)
.attr("text-anchor", "left")
.style("font-size", "14px")
.style("fill", "grey")
.style("max-width", 400)
.html("<a href='https://www.nytimes.com/interactive/2017/09/01/upshot/cost-of-hurricane-harvey-only-one-storm-comes-close.html#:~:text=It%20estimated%20the%20cost%20at,%2490%20billion%20as%20of%20Thursday.&text=Moody's%20Analytics%2C%20a%20research%20firm,and%20%24108%20billion%20on%20Friday.&text=Chuck%20Watson%2C%20a%20disaster%20modeler%20with%20Enki%20Research.'>New York Times</a>");
// Add legend
svg.append("g")
.attr("class", "legendScale")
.attr("transform", "translate(200,20)");
});
</script>
</body>
</html>