Published
Edited
May 5, 2020
Insert cell
md`# Cases, Usage and Timeline`
Insert cell
chart = {
var stage = 0
var turnstile_data_1 = turnstile_data.slice(0, 7);
var turnstile_data_2 = turnstile_data.slice(6,14);
var turnstile_data_3 = turnstile_data.slice(13,20);
var turnstile_data_4 = turnstile_data.slice(19,);
function transition(path) {
path.transition()
.duration(2500)
.attrTween("stroke-dasharray", tweenDash)
.on("end", () => {
d3.select(this).call(transition);
svg.selectAll('.text-box')
.text(setText(stage))
});
}

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("overflow", "visisble");
svg.append("g")
.attr("transform", "translate(0, " + (height - margin.top -10) + ")")
.call(d3.axisBottom(x_covid));
svg.append("g")
.attr("transform", "translate(" + (margin.left) + ",0)")
.attr('style', 'font-size: 7px')
.call(d3.axisLeft(y));
svg.append("image")
.attr("xlink:href", "https://cdn.onlinewebfonts.com/svg/img_70339.png")
.attr("width", 30)
.attr("height", 30)
.attr('x', height-350)
.attr('y', margin.top)
.on("click", function(d) {
stage += 1
var next_data;
if (stage == 1){
next_data = turnstile_data_1
}
if (stage == 2){
next_data = turnstile_data_2
}
if (stage == 3){
next_data = turnstile_data_3
}
if (stage == 4){
next_data = turnstile_data_4
}
svg.append('path')
.datum(next_data)
.attr("fill", "none")
.attr("stroke", "#7c1e84")
.attr("stroke-width", 1.5)
.attr("d", line)
.call(transition);

})

svg.append("text")
.attr('class', 'text-box')
.attr("x", height-310)
.attr("y", margin.top + 10)
.attr('style', 'font-size: 10px')
.append("tspan")
.text(setText(stage)[0])
.append("tspan")
.text(setText(stage)[1])
.attr("x", height-310)
.attr("dy", 22)
.append("tspan")
.text(setText(stage)[2])
.attr("x", height-310)
.attr("dy", 22)

return svg.node();
}
Insert cell
function setText(stage){
if (stage == 0){
return ["Over the course of March, as COVID-19 becomes a greater threat.", "Watch how the MTA usage changes.", "Normally, MTA usage is lower during the weekends."]
}
if (stage == 1){
return "On Sunday March 7th, Governor Cuomo declared a State Emergency"
}
if (stage == 2) {
return "On March 14th, first NY COVID deaths occur"
}
if (stage == 3) {
return "On March 20th, Governor Cuomo starts NY Pause."
}
}
Insert cell
function tweenDash() {
const l = this.getTotalLength(),
i = d3.interpolateString("0," + l, l + "," + l);
return function(t) { return i(t) };
}
Insert cell
line_covid = d3.line()
.defined(d => !isNaN(d.value))
.x(d => x_covid(d.date))
.y(d => y_covid(d.value))
Insert cell
line = d3.line()
.defined(d => !isNaN(d.value))
.x(d => x(d.date))
.y(d => y(d.value))
Insert cell
x_covid = d3.scaleTime()
.domain(d3.extent(covid_data, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
y_covid = d3.scaleLinear()
.domain([0, d3.max(covid_data, d=> d.value)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(turnstile_data, d=> d.value)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
x = d3.scaleTime()
.domain(d3.extent(turnstile_data, d => d.date))
.range([margin.left + 10, width - margin.right])
Insert cell
Insert cell
d3.csv('https://raw.githubusercontent.com/avivaweinbaum/nyc-mta-covid/master/turnstile.csv')
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more