Published
Edited
May 3, 2020
1 fork
Insert cell
md`# MTA + COVID-19 map`
Insert cell
chart2 = {
var svg = d3.create("svg")
.attr("width", 1000)
.attr("height", 700)

var g = svg.append("g")
g.selectAll("path")
.data(nyc2.features)
.enter().append("path")
.attr("d", path)
.attr("fill","#C0C0C0");
var h = svg.append("g")
var circles = h.selectAll("circle")
.data(data.series)
.enter().append("circle")
.style("opacity", 0.7)
.attr("cx",function(d,i) {
var coords = proj([d.longitude,d.latitude]);
return coords[0];
})
.attr("cy",function(d,i) {
var coords = proj([d.longitude,d.latitude]);
return coords[1];
})
.attr("r",function(d,i) {
var proportion = d.values[3]/30000;
return 20*proportion;
})
.attr("fill",function(d) {
return d3.interpolateMagma(d.values[3]/30000);
});
g.attr("transform", "translate(200, 0)");
h.attr("transform", "translate(200, 0)");
var datetext = svg.append("text")
.text("Wednesday, Febrary 19th")
.attr("x", 50)
.attr("y", 100)
.style("font-size","30px")
.style("font-weight","bold");
svg.append("text")
.text("Total riders: ")
.attr("x",50)
.attr("y",130)
.style("font-size","20px")
.style("font-weight",20);
var valtext = svg.append("text")
.attr("x", 50)
.attr("y", 150)
.style("font-size","20px")
.style("font-weight",500)
.text("2868501");
const slider = d3.sliderBottom()
.min(d3.min(data.dates))
.max(d3.max(data.dates))
.marks(data.dates)
.width(300)
.tickFormat(d3.utcFormat("%b %d"))
.ticks(5)
.on("onchange", function(i) {
var index
for(var n = 0; n < data.dates.length; n++) {
//console.log(data.dates[n]);
if (data.dates[n] == i) {
index = n;
}
}
circles.transition().attr("r",function(d,i) {
var proportion = d.values[index]/30000;
return 20*proportion;
})
.attr("fill", function(d,i) {
return d3.interpolateMagma(d.values[index]/30000);
});
datetext.transition().text(d3.utcFormat("%A, %B %d")(i));
valtext.transition().text(function() {
console.log(index);
var total = 0;
for (var n = 0; n < data.series.length; n++) {
total += data.series[n].values[index]
}
return total;
});
});

svg.call(slider);

return svg.node()
}
Insert cell
proj = d3.geoConicConformal()
.parallels([33, 45])
.rotate([96, -39])
.fitSize([1000, 700], nyc2)
Insert cell
path = d3.geoPath()
.projection(d3.geoConicConformal()
.parallels([33, 45])
.rotate([96, -39])
.fitSize([1000, 700], nyc2))
Insert cell
nyc2 = {
var betterfeatures = nyc.features.filter(function(d) {
return d.properties.borough != "Staten Island";
})
return {
type: nyc.type,
features: betterfeatures
};
}
Insert cell
nyc = d3.json("https://gist.githubusercontent.com/pstuffa/928a2a31f352e59edef5ef56fa767e20/raw/7ba0230c627237c12cc1b3809f85d99486621756/nyc.json")
Insert cell
data = {
var dateRange = d3.timeDays(new Date(2020, 1, 16), new Date(2020, 3, 4));
var format = d3.timeFormat("%a %d")
return {
y: "Daily Station Entries",
series: data_raw,
dates: dateRange
};
}
Insert cell
data_raw.splice(0,1)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data.series, d => d3.max(d.values))]).nice()
//.domain([0, 12000])
.range([height - margin.bottom, margin.top])
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data.dates))
.range([margin.left, width - margin.right])
Insert cell
margin = ({top: 20, right: 20, bottom: 30, left: 30})
Insert cell
height = 600
Insert cell
d3 = require.alias({
"d3-array": "d3@5",
"d3-axis": "d3@5",
"d3-dispatch": "d3@5",
"d3-drag": "d3@5",
"d3-ease": "d3@5",
"d3-scale": "d3@5",
"d3-selection": "d3@5",
"d3-transition": "d3@5"
})("d3@5", "d3-simple-slider@1")
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