Skip to content

Instantly share code, notes, and snippets.

@angelialau
Created June 17, 2020 16:42
Show Gist options
  • Save angelialau/eeaab5bca0faefe096a50a3682b6ca58 to your computer and use it in GitHub Desktop.
Save angelialau/eeaab5bca0faefe096a50a3682b6ca58 to your computer and use it in GitHub Desktop.
DV - HW2// source https://jsbin.com/mukiciw
<!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',
nticks: 5,
mirror: true,
linewidth: 1,
},
yaxis: {
title: 'Number of Cases',
ticks: 'outside',
mirror: true,
linewidth: 1,
}
}
Plotly.newPlot('chart', traces, layout);
});
</script>
</body>
</html>
.bar {
fill: SteelBlue;
stroke: Black;
}
#title {
font-size:20;
font-family:Sans-Serif;
}
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',
nticks: 5,
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