Published
Edited
Oct 30, 2020
Fork of Fan Chart
1 star
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("stroke-miterlimit", 1);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

svg.append("g")
.call(grid);

svg.append("path")
.attr("fill", "steelblue")
.attr("fill-opacity", 0.2)
.attr("d", area(data.slice(0,projectedDataEnd)));

svg.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("d", line(data.slice(0, observedIndex + 1)));

svg.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-dasharray", "3,3")
.attr("d", line(data.slice(observedIndex, projectedDataEnd)));
svg.append("path")
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 1.5)
.attr("d", lineReal(data.slice(observedIndex)));

svg.append("circle")
.attr("cx", x(observed.date))
.attr("cy", y(observed.mean))
.attr("r", 2.5);

svg.append("text")
.attr("x", x(observed.date))
.attr("y", y(observed.mean))
.attr("dx", 6)
.attr("dy", "0.35em")
.text(observed.mean.toLocaleString("en"));

svg.append("text")
.attr("x", x(observed.date))
.attr("y", y(observed.mean))
.attr("dx", 6)
.attr("dy", "1.35em")
.text(d3.utcFormat("%B %-d")(observed.date));

return svg.node();
}
Insert cell
data[34]
Insert cell
//data = Object.assign(d3.csv(url, d3.autoType),{y: "↑ Deaths per day"})
data = Object.assign(d3.csvParse(await FileAttachment("imheNew@1.csv").text(), function(d) {
console.log('idem rijesiti', d)
return {
'date': d3.timeParse("%m/%d/%Y")(d.date),
'projected': +d.projected,
'mean': +d.mean,
'upper': +d.upper,
'lower': +d.lower,
'real': +d.real
}
}), {y: "↑ Deaths per day"})
Insert cell
// url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQjGzPCuuvxgjBb46kSYU4yL4K7KOYI9ETs301768A2LvY8Ev74tP3cCCaR99Wi4Pafo3fxoriHluZM/pub?gid=1642428545&single=true&output=csv'
Insert cell
//data = Object.assign(d3.csvParse(await FileAttachment("covid-ihme-projected-deaths-2020-04-01 - covid-ihme-projected-deaths-2020-04-01(1)(2).csv").text(), d3.autoType), {y: "↑ Deaths per day"})
Insert cell
projectedDataEnd = data.findIndex(d=>d.date > new Date(2020, 6, 15))
Insert cell
observedIndex = data.findIndex(d => d.projected) - 1
Insert cell
observed = data[observedIndex]
Insert cell
lineReal = d3.line()
.defined(d => d.real !== null)
.x(d => x(d.date))
.y(d => y(d.real))
Insert cell
line = d3.line()
.x(d => x(d.date))
.y(d => y(d.mean))
Insert cell
area = d3.area()
.x(d => x(d.date))
.y0(d => y(d.lower))
.y1(d => y(d.upper))
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(data, d => d.date))
.rangeRound([margin.left, width - margin.right])
Insert cell
y = d3.scaleLog()//.scaleLinear()//
.domain([1, d3.max(data, d => d.upper)])
.rangeRound([height - margin.bottom, margin.top])
.clamp(true)
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80))
.call(g => g.select(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, ",d"))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(data.y))
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
height = 600
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
d3 = require("d3@5")
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