Last active
June 17, 2020 18:08
-
-
Save angelialau/9411b74c090a4122c80b44dd8a75b8af to your computer and use it in GitHub Desktop.
DV - HW2// source https://jsbin.com/mukiciw
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>DV - HW2</title> | |
<script src="https://d3js.org/d3.v5.js"></script> | |
<script src="https://serv.cusp.nyu.edu/~hvo/files/us-refugees.js" type="text/javascript"></script> | |
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> | |
<style id="jsbin-css"> | |
.bar { | |
fill: SteelBlue; | |
stroke: Black; | |
} | |
#title { | |
font-size:20; | |
font-family:Sans-Serif; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="chart"></div> | |
<script id="jsbin-javascript"> | |
Promise.all([ | |
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv"), | |
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv"), | |
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv") | |
]).then(function(data) { | |
var us = data[0]; | |
var nys = data[1].filter(record => record.state=='New York'); | |
var nyc = data[2].filter(record => record.county=='New York City'); | |
let datasets = [us, nys, nyc], | |
titles = ['US', 'NYS', 'NYC'], | |
colors= ['#1f77b4', '#ff7f0e', '#2ca02c']; | |
let traces = []; | |
datasets.forEach((dataset, i)=>{ | |
// for cases | |
traces.push({ | |
x: dataset.map(d => d.date), | |
y: dataset.map(d => d.cases), | |
type:'line', | |
name: titles[i] + ' Cases', | |
marker: {color:colors[i]} | |
}); | |
// for deaths | |
traces.push({ | |
x: dataset.map(d => d.date), | |
y: dataset.map(d => d.deaths), | |
mode:'lines', | |
name: titles[i] + ' Deaths', | |
line: {dash: 'dot'}, | |
marker:{color:colors[i]} | |
}); | |
}); | |
let layout = { | |
title: 'Covid-19 Cases in NYC vs. NYS and US', | |
width: 700, | |
height: 400, | |
xaxis:{ | |
title: 'Date', | |
ticks: 'outside', | |
mirror: true, | |
linewidth: 1, | |
}, | |
yaxis: { | |
title: 'Number of Cases', | |
ticks: 'outside', | |
mirror: true, | |
linewidth: 1, | |
} | |
} | |
Plotly.newPlot('chart', traces, layout); | |
}); | |
</script> | |
<script id="jsbin-source-css" type="text/css">.bar { | |
fill: SteelBlue; | |
stroke: Black; | |
} | |
#title { | |
font-size:20; | |
font-family:Sans-Serif; | |
}</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.bar { | |
fill: SteelBlue; | |
stroke: Black; | |
} | |
#title { | |
font-size:20; | |
font-family:Sans-Serif; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise.all([ | |
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv"), | |
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv"), | |
d3.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv") | |
]).then(function(data) { | |
var us = data[0]; | |
var nys = data[1].filter(record => record.state=='New York'); | |
var nyc = data[2].filter(record => record.county=='New York City'); | |
let datasets = [us, nys, nyc], | |
titles = ['US', 'NYS', 'NYC'], | |
colors= ['#1f77b4', '#ff7f0e', '#2ca02c']; | |
let traces = []; | |
datasets.forEach((dataset, i)=>{ | |
// for cases | |
traces.push({ | |
x: dataset.map(d => d.date), | |
y: dataset.map(d => d.cases), | |
type:'line', | |
name: titles[i] + ' Cases', | |
marker: {color:colors[i]} | |
}); | |
// for deaths | |
traces.push({ | |
x: dataset.map(d => d.date), | |
y: dataset.map(d => d.deaths), | |
mode:'lines', | |
name: titles[i] + ' Deaths', | |
line: {dash: 'dot'}, | |
marker:{color:colors[i]} | |
}); | |
}); | |
let layout = { | |
title: 'Covid-19 Cases in NYC vs. NYS and US', | |
width: 700, | |
height: 400, | |
xaxis:{ | |
title: 'Date', | |
ticks: 'outside', | |
mirror: true, | |
linewidth: 1, | |
}, | |
yaxis: { | |
title: 'Number of Cases', | |
ticks: 'outside', | |
mirror: true, | |
linewidth: 1, | |
} | |
} | |
Plotly.newPlot('chart', traces, layout); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment