Published
Edited
Jun 17, 2020
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

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

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

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

svg.append("g")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("fill", "none")
.selectAll("circle")
.data(coronaData)
.join("circle")
.attr("cx", d => x(d.total_cases))
.attr("cy", d => y(d.total_deaths))
.attr("r", 3)
.attr("title", d => d.title);

svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 12)
.selectAll("text")
.data(coronaData)
.join("text")
.attr("dy", "0.35em")
.attr("x", d => x(d.total_cases) + 7)
.attr("y", d => y(d.total_deaths))
.text(d => { const diff = Math.round(100*d.total_deaths/regressor.predict(d.total_cases)); return `${d.code} (%${diff})`});

svg.append("path")
.attr("class", "regression")
.datum(regressor)
.attr("d", lineGenerator);
return svg.node();
}
Insert cell
coronaData = {
const response = await fetch("https://api.thevirustracker.com/free-api?countryTotals=ALL");
const raw = await response.json();
// return Object.values(raw.countryitems[0]).filter(d => d.total_deaths >= 2000 && d.total_cases <= 500000 );
return Object.values(raw.countryitems[0]).filter(d => d.total_cases >= 60000 && d.total_cases <= 500000 /* && d.code != "IN"*/ );
}
Insert cell
lineGenerator = d3.line()
.x(d => x(d[0]))
.y(d => y(d[1]));
Insert cell
x = d3.scaleLinear()
// .base(10)
.domain(d3.extent(coronaData, d => d.total_cases)).nice()
.range([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
// .base(10)
.domain(d3.extent(coronaData, d => d.total_deaths)).nice()
.range([height - margin.bottom, margin.top])
Insert cell
regressor = regression(coronaData.filter(d => d.code!="TR"))
Insert cell
regression = d3.regressionPoly()
.x(d => d.total_cases)
.y(d => d.total_deaths)
.order(2)
// .domain(d3.extent(coronaData, d => d.total_cases));

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())
.call(g => g.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text("Total cases"))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.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("Total death"))
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
margin = ({top: 25, right: 70, bottom: 35, left: 70})
Insert cell
height = 800
Insert cell
html`
<style>
circle {
fill-opacity: .5;
fill: red;
stroke: #000;
stroke-opacity: .8;
}
.regression {
fill: none;
stroke: #000;
stroke-width: 1px;
}
.axis line {
stroke: #ddd;
}
.axis line.baseline {
stroke: #888;
}
.axis .domain {
display: none;
}
</style>
`
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more