Published
Edited
Oct 26, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
raw_2017 = d3.csv("https://s3.eu-west-1.amazonaws.com/data.ixmaps.com/Crime/Italy/INT00062_Delitti_denunciati__-_dati_anno_2017_-_dati_Provinciali.csv")
Insert cell
Inputs.Table(raw_2017)
Insert cell
Insert cell
Insert cell
Insert cell
process_data = function(raw_data){

var data = Data.import({"source":raw_data,"type":"json"});

// aggiunge colonna con il l'incidenza (valore per 100.000)
var provinciaIndex = data.column("Provincia").index;
var valueIndex = data.column("TOT DELITTI COMMESSI").index;
data.addColumn({destination:"INCIDENCE"},function(row){
return (row[valueIndex]/pA[row[provinciaIndex]]*100000);
});

// crea tabella pivot con una colonna per reato
var pivot = data.pivot({
"lead":"Provincia",
"columns": "REATO",
"value": "INCIDENCE",
"keep": ["ANNO"]
});
// alcuni nomi delle Province devono essere cambiate per essere compatibile con i confini da ISTAT
corrNames(pivot);
return pivot;
}
Insert cell
delitti_2017 = process_data(raw_2017);
Insert cell
Inputs.Table(delitti_2017.json())
Insert cell
Insert cell
raw_2018 = d3.csv("https://s3.eu-west-1.amazonaws.com/data.ixmaps.com/Crime/Italy/INT00062_Delitti_denunciati__-_dati_anno_2018_-_dati_Provinciali.csv")
Insert cell
delitti_2018 = process_data(raw_2018);
Insert cell
raw_2019 = d3.dsv(',',"https://s3.eu-west-1.amazonaws.com/data.ixmaps.com/Crime/Italy/INT00062_Delitti_denunciati__-_dati_anno_2019_-_dati_Provinciali.csv")
Insert cell
delitti_2019 = process_data_2019(raw_2019);
Insert cell
merger = new Data.Merger();
Insert cell
Insert cell
final = merger
.addSource(delitti_2017, {
lookup: "Provincia",
columns: ["Provincia",filter.codice],
label: ["Provincia","TOTALE_DELITTI_2017"]
})
.addSource(delitti_2018, {
lookup: "Provincia",
columns: ["Provincia",filter.codice],
label: ["Provincia","TOTALE_DELITTI_2018"]
})
.addSource(delitti_2019, {
lookup: "Provincia",
columns: ["Provincia",filter.codice],
label: ["Provincia","TOTALE_DELITTI_2019"]
})
.realize();
Insert cell
Inputs.Table(final.json())
Insert cell
Insert cell
Insert cell
province = d3.json("https://s3.eu-central-1.amazonaws.com/maps.ixmaps.com/Istat/province_2019/Province_s.geojson.gz")
Insert cell
georef = () => {
return {
"layer": "Prov2019_name",
"field": "$item$",
"field100": "",
"style": {
"type": "FEATURES|NOLEGEND|LOCKED",
"colorscheme": [
"none"],
"linecolor":"none",
"dbtable": "PROVINCE",
"itemfield": "DEN_UTS",
"lookupfield": "geometry",
"title": "Italy Province 2019"
}
}
};
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
delitti = {
var reg = [];
var columnsA = delitti_2017.columnNames();
reg.push({name: columnsA[columnsA.length-2], codice: columnsA[columnsA.length-2]});
for( var c =2; c < columnsA.length-2; c++ ){
reg.push({name: columnsA[c], codice: columnsA[c]});
}
return reg;
}
Insert cell
Insert cell
p = d3.csv("https://s3.eu-west-1.amazonaws.com/data.ixmaps.com/ISTAT/DCIS_POPRES_Province_2019.csv")
Insert cell
Inputs.Table(p)
Insert cell
pA = Data.import({"source":p,"type":"json"
}).lookupArray("Value","Territorio");
Insert cell
Insert cell
pA["Aosta"] = pA["Valle d'Aosta / Vallée d'Aoste"]
Insert cell
pA["Bolzano"] = pA["Provincia Autonoma Bolzano / Bozen"]
Insert cell
pA["Trento"] = pA["Provincia Autonoma Trento"]
Insert cell
pA["Forli'-Cesena"] = pA["Forlì-Cesena"]
Insert cell
Insert cell
process_data_2019 = function(raw_data){

var data = Data.import({"source":raw_data,"type":"json"});
corrNames(data);
var provinciaIndex = data.column("Provincia").index;
var valueIndex = data.column("Totale Delitti commesi").index;
data.addColumn({destination:"INCIDENCE"},function(row){
return (row[valueIndex]/pA[row[provinciaIndex]]*100000);
});

var pivot = data.pivot({
"lead":"Provincia",
"columns": "Reato",
"value": "INCIDENCE",
"keep": ["ANNO"]
});
return pivot;
}
Insert cell
corrNames = function (data){
data.column("Provincia").map(function(value){
if (value == "Forli' - Cesena"){
return "Forli'-Cesena";
}
if (value == "Pesaro - Urbino"){
return "Pesaro e Urbino";
}
if (value == "Reggio Emilia"){
return "Reggio nell'Emilia";
}
if (value == "Reggio Calabria"){
return "Reggio di Calabria";
}
return value;
});
return data;
}
Insert cell
Insert cell
ixmaps = require('https://gjrichter.github.io/ixmaps/ui/js/htmlgui_api.js')
Insert cell
Data = require('https://gjrichter.github.io/data.js/data.js')
Insert cell
Inputs = require("@observablehq/inputs@0.7.21/dist/inputs.umd.min.js")
Insert cell
$ = require('https://code.jquery.com/jquery-3.3.1.min.js')
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