-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
172 lines (148 loc) · 5.02 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
<!DOCTYPE html>
<meta charset="utf-8">
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<style>
.toolTip {
position: absolute;
display: none;
min-width: 80px;
height: auto;
background: none repeat scroll 0 0 #ffffff;
border: 1px solid #6F257F;
padding: 14px;
text-align: center;
}
</style>
<body>
<h1>United States Covid 19 Data - 2021</h1>
<div>
<label for="state"><strong>Select State:</strong></label>
<select id="state">
</select>
</div>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<script>
// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 90, left: 60},
width = 460 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.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 + ")");
var tooltip = d3.select("body").append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.style("background", "#ffffff")
.text("a simple tooltip");
// Parse the Data
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv", function(data) {
var select = d3.select("#state")
.attr('class','select')
.on('change',onchange)
var options = select
.selectAll('option')
.data(d3.map(data, function(d){return d.state;}).keys())
.enter()
.append('option')
.text(function (d) { return d; })
.attr("value",function(d){return d;});
var selectState = "Washington";
function onchange() {
selectState = d3.select('select').property('value')
d3.selectAll('svg').remove();
svg = d3.select("#my_dataviz")
.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 + ")");
displayChart(data, selectState);
};
function filterData(data, s) {
var fData = data.filter(function(d) {
var year = d.date.substring(0,4);
var month = d.date.substring(5,7);
var day = d.date.substring(8,10);
return d.state == s && year == "2021" && month <= "07" && day == "01"
});
return fData;
};
displayChart(data, selectState);
function displayChart(data, selectState) {
var stateData = filterData(data, selectState);
console.log(stateData);
stateData.forEach(function(d) {
d.cases = d.cases/1000;
var year = d.date.substring(0,4);
var month = d.date.substring(5,7);
var day = d.date.substring(8,10);
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var monthValue = months[parseInt(month) - 1];
d.date = monthValue;
});
// X axis
var x = d3.scaleBand()
.range([ 0, width ])
.domain(d3.map(stateData, function(d){return d.date;}).keys())
.padding(0.2);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "translate(-10,0)rotate(-45)")
.style("text-anchor", "end");
var casesMin = d3.min(stateData, function(d) { return d.cases; } );
var casesMax = d3.max(stateData, function(d) { return d.cases; } );
console.log(casesMin, casesMax);
// Add Y axis
var y = d3.scaleLinear()
.domain([0, casesMax])
.range([ height, 0])
svg.append("g")
.call(d3.axisLeft(y).ticks(5));
// Add the text label for the Y axis
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Cases(in 1000s)");
// Bars
svg.selectAll("mybar")
.data(stateData)
.enter()
.append("rect")
.attr("x", function(d) {
return x(d.date);
})
.attr("width", x.bandwidth())
.attr("fill", "#69b3a2")
// no bar at the beginning thus:
.attr("height", function(d) { return height - y(0); }) // always equal to 0
.attr("y", function(d) { return y(0); })
.on("mouseover", function(d){tooltip.text("Deaths:\n" + d.deaths); return tooltip.style("visibility", "visible");})
.on("mousemove", function(){return tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");})
.on("mouseout", function(){return tooltip.style("visibility", "hidden");});
// Animation
svg.selectAll("rect")
.transition()
.duration(800)
.attr("y", function(d) { return y(d.cases); })
.attr("height", function(d) {
return Math.max(height - y(d.cases), 0);
})
.delay(function(d,i){console.log(i) ; return(i*100)})
}
});
</script>
</body>