Published
Edited
Jun 26, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
margin = ({top: 20, right: 20, bottom: 35, left: 40})
Insert cell
Insert cell
y = d3.scaleLinear([1000, 11000], [height - margin.bottom, margin.top])
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 confirmed cases of COVID-19 per 100000 people→"))
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("↑ Tests per 100000 people"))
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
radius = d3.scaleSqrt([0, 5e8], [0, width / 6])
Insert cell
color = d3.scaleOrdinal(jsondata.map(d => d.region), d3.schemeCategory10).unknown("black")
Insert cell
bisectYear = d3.bisector(([year]) => year).left
Insert cell
function valueAt(values, year) {
const i = bisectYear(values, year, 0, values.length - 1);
const a = values[i];
if (i > 0) {
const b = values[i - 1];
const t = (year - a[0]) / (b[0] - a[0]);
return a[1] * (1 - t) + b[1] * t;
}
return a[1];
}
Insert cell
import {Scrubber} from "@mbostock/scrubber"
Insert cell
Insert cell
formatDate = d3.utcFormat("%Y")
Insert cell
function dataAt(year) {
return jsondata.map(d => ({
name: d.name,
region: d.region,
totpop:d.totpop,
pos_pop_100k: valueAt(d.pos_pop_100k, year),
population: valueAt(d.population, year),
ct_pop_100k: valueAt(d.ct_pop_100k, year),
tamp_pop_100k: valueAt(d.tamp_pop_100k, year)
}));
}
Insert cell
names = new Set(jsondata.map(d => d.name))
Insert cell
jdates = new Set(jsondata.map(d => d.jdate))
Insert cell
getdates=function(csvdoc){
var nested = d3.nest()
.key(function(d){ return d.justdata; })
.entries(csvdoc)
var dates = nested.map(function(d){
var dates_record = new Date(d.key);
// this is the variable that we grouped by
// dates_record.jdate = d.key;
//dates_record.justdata = d.values.map(function(c){ return [c.justdata]; });
return dates_record;
});
return dates;
}
Insert cell
mydates=getdates(data2)
Insert cell
Insert cell
csvtojson=function(csvdoc){
var nested = d3.nest()
.key(function(d){ return d.denominazione_regione; })
.entries(csvdoc)
var json = nested.map(function(d){
var namerecord = {};
// this is the variable that we grouped by
namerecord.name = d.key;
// this is another variable that I happen to know is constant for all
// years within a given "name", so I'll just grab the first value
namerecord.region = d.values[0].denominazione_regione;
namerecord.totpop = d.values[0]['Maschi + Femmine'];
// these are the time-series variables
// use a "map" to grab the year and amount from each of the child "values"
//namerecord.totale_casi = d.values.map(function(c){ var x=d3.autoType({date: c.justdata}); return [new Date(c.justdata), +c.totale_casi]; });
namerecord.totale_casi = d.values.map(function(c){ return [new Date(c.justdata), +c.totale_casi]; });
namerecord.tamponi = d.values.map(function(c){ return [new Date(c.justdata), +c.tamponi]; });
namerecord.population = d.values.map(function(c){ return [new Date(c.justdata), +c['Maschi + Femmine']]; });
namerecord.pos_pop_100k = d.values.map(function(c){ return [new Date(c.justdata), +c.pos_pop_100k]; });
namerecord.tamp_pop_100k = d.values.map(function(c){ return [new Date(c.justdata), +c.tamp_pop_100k]; });
namerecord.ct_pop_100k= d.values.map(function(c){ return [new Date(c.justdata), +c.ct_pop_100k]; });
return namerecord;
});
return json;
}
Insert cell
jsondata=csvtojson(data2)
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