Published
Edited
Jan 14, 2022
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ordinanze = d3.csv("https://raw.githubusercontent.com/opendatasicilia/ordinanze-covid/main/data.csv")
Insert cell
Inputs.Table(ordinanze)
Insert cell
Insert cell
__ordinanze = Data.import({"source":ordinanze,"type":"json"}).sort("nro_ordinanza").select(filter.filter)
Insert cell
Insert cell
__ordinanze_expanded = __expand_data(__ordinanze);
Insert cell
__expand_data = function (data) {

var table = [];
var lastColumn = data.columnNames().length - 1;
var found = 0
var records = data.records;
var len = records.length;
// go through the records and create for every record (Comune/Ordinanza) as many new table rows
// as days are between data_inizio e data_fine with the appropriate date
for ( var r=0; r<records.length; r++ ){
var data_inizio = records[r][3];
var data_fine = records[r][4];
var giorno = 1;
var giorni = (new Date(data_fine).getTime() - new Date(data_inizio).getTime()) / 1000 / 60 / 60 / 24;
while ( data_inizio != data_fine ) {
table.push({pro_com:records[r][0],comune:records[r][1],pro:com_pro_A["0"+records[r][0]],date:data_inizio,ord:(giorno==1)?1:0,giorni:(giorno==1)?giorni:0,pop:pop_21_A["0"+records[r][0]],pop_g:pop_21_A["0"+records[r][0]]});
data_inizio = new Date(new Date(data_inizio).getTime()+1000*60*60*24).toISOString().split("T")[0];
giorno++;
};
}

// create and return a new Data object from the table
return Data.import({"source":table,"type":"json"}).sort("date");
};
Insert cell
Insert cell
Inputs.Table(__ordinanze_expanded.json())
Insert cell
Insert cell
pivot = __ordinanze_expanded.pivot({
lead: "pro_com",
columns: "date",
keep: ["comune","pro"],
value: "pop"
}).sort("pro")
Insert cell
Inputs.Table(pivot.json())
Insert cell
Insert cell
Insert cell
condensed = Data.import({"source":__ordinanze_expanded.json(),"type":"json"}).condense({
lead: "pro_com",
keep: ["comune","pro_com","pro","pop"]
}).sort("pro")
Insert cell
Inputs.Table(condensed.json())
Insert cell
Insert cell
condensed_pro = {
var tab = Data.import({"source":condensed.json(),"type":"json"})
.condense({
lead: "pro",
keep: ["pro"],
calc: "sum"
})
.addColumn({source:"pro",destination:"pop_pro"},(value)=>{
return pop_pro_A[value];
})
.addColumn({destination:"pop_percent"},(row)=>{
return row[6]/row[8]*100;
})
return tab;
}
Insert cell
Inputs.Table(condensed_pro.json())
Insert cell
Insert cell
condensed_pro_max = {
var tab = Data.import({"source":condensed.json(),"type":"json"})
.condense({
lead: "pro",
keep: ["pro"],
calc: "max"
})
.addColumn({source:"pro",destination:"pop_pro"},(value)=>{
return pop_pro_A[value];
})
return tab;
}
Insert cell
Inputs.Table(condensed_pro_max.json())
Insert cell
Insert cell
columnsA = {
var x = pivot.columnNames().slice(3);
x.pop();
return x;
}
Insert cell
xaxisA = {
var x = pivot.columnNames().slice(3);
for ( var i in x ){
x[i] = new Date(x[i]).toLocaleDateString().replace(/\//g,"-");
}
x.pop();
return x
}
Insert cell
pop_21 = d3.csv("https://raw.githubusercontent.com/opendatasicilia/comuni-italiani/main/dati/popolazione_2021.csv");
Insert cell
pop_21_A = Data.import({"source":pop_21,"type":"json"}).lookupArray({key:"pro_com_t",value:"pop_res_21"})
Insert cell
com = d3.csv("https://raw.githubusercontent.com/opendatasicilia/comuni-italiani/main/dati/comuni.csv")
Insert cell
com_pro_A = Data.import({"source":com,"type":"json"}).lookupArray({key:"pro_com_t",value:"den_prov"})
Insert cell
pop_prov = {
var __pop_prov = Data.import({"source":pop_21,"type":"json"})
__pop_prov.addColumn({source:"pro_com_t",destination:"pro"},(value) => {
return (com_pro_A[value]);
})
__pop_prov.condense({lead:"pro"})
return __pop_prov;
}
Insert cell
pop_pro_A = pop_prov.lookupArray({key:"pro",value:"pop_res_21"})
Insert cell
Insert cell
comuni = d3.json("https://s3.eu-central-1.amazonaws.com/maps.ixmaps.com/Istat/comuni_2020/italia_comuni_2020_g_sicilia.geojson")
Insert cell
Insert cell
ixmaps = require('https://gjrichter.github.io/ixmaps/ui/js/htmlgui_api.js')
Insert cell
Inputs = require("@observablehq/inputs@0.7.21/dist/inputs.umd.min.js")
Insert cell
Data = require('https://gjrichter.github.io/data.js/data.js')
Insert cell
$ = require('https://code.jquery.com/jquery-3.3.1.min.js')
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