-
Notifications
You must be signed in to change notification settings - Fork 0
/
us.js
114 lines (90 loc) · 3.17 KB
/
us.js
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
let svg = d3.select("body").append("svg")
.attr("width", 1440)
.attr("height", 750);
//store the width and height for later
let width = +svg.attr("width");
let height = +svg.attr("height");
//render the data
function render(data) {
let xVal = d => d.date; //gets the date for a value d
let yVal = d => d.cases; //gets the cases amount for a value d
//set the margins
let margin = {
top: 100,
right: 150,
bottom: 100,
left: 325
};
//these set the width and height of the inner chart
let innerWidth = width - margin.left - margin.right;
let innerHeight = height - margin.top - margin.bottom;
/*
SET UP SCALES
*/
let scaleX = d3.scaleTime() //sets up how dates will scale
.domain([d3.min(data, xVal), d3.max(data, xVal)]).nice() //the data space, but nice() rounds it cleanly
.range([0, innerWidth]); //the pixel space
let xAxis = d3.axisBottom(scaleX) //the bottom axis is connected to the scaleX now
.tickSize(-innerHeight)
.tickPadding(20)
.ticks(12)
.tickFormat(d3.timeFormat("%b - %e"));
let scaleY = d3.scaleLinear() //sets up how cases, y axis values will scale
.domain([0, d3.max(data, yVal)]).nice()
.range([innerHeight, 0])
let yAxis = d3.axisLeft(scaleY) //left axis is connected to the scaleY now
.tickPadding(20)
.tickSize(-innerWidth);
let g = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`); //moves the chart out into clear space
let yG = g.append("g") //adds a grouping for the cases, y axis
.call(yAxis)
.attr("font-size", 16); //size of number of cases labels
yG.append("text") //add the y axis label
.attr("font-size", 22)
.attr("fill", "black")
.text("New Cases Per Day")
.attr("y", innerHeight / 2)
.attr("x", -90);
let xG = g.append("g") //adds another axis grouping for the x axis, sets up labels and ticks
.call(xAxis)
.attr("transform", `translate(0, ${innerHeight})`) //moves the dates to bottom
.attr("font-size", 16);
xG.append("text") //add the label below x axis
.attr("font-size", 22)
.attr("fill", "black")
.text("Month - Day")
.attr("y", 70)
.attr("x", innerWidth / 2);
g.append("text") //adds another grouping for the name of the line chart
.attr("font-family", "sans-serif")
.text(desiredCountry + " COVID-19 New Cases Per Day, 2020")
.attr("font-size", 26)
.attr("y", -20)
.attr("x", innerWidth / 4);
/*
DRAW THE LINE
*/
let line = d3.line() //defines the x and y of the line
.x(d => scaleX(xVal(d)))
.y(d => scaleY(yVal(d)));
g.append("path") //add the line or path
.datum(data)
.attr("fill", "none")
.attr("stroke", "#00A15F")
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round") //smooths line a bit
.attr("stroke-linecap", "round") //smooths line a bit
.attr("d", line); //call line to draw line
}
let desiredCountry = "Ireland";
d3.csv("covid.csv", function(d) { //for each entry
if (d["Location.Country"] == desiredCountry && +d["Date.Year"] == 2020) {
return { //parse date from multiple entries
date: d3.timeParse("%Y-%m-%d")(d["Date.Year"] + "-" + d["Date.Month"] + "-" + d["Date.Day"]),
cases: +d["Data.Cases"]
};
}
}).then(function(data) {
render(data); //then render the data as it has been fully processed
});