Published
Edited
Apr 25, 2021
2 forks
Importers
Insert cell
Insert cell
uri = `https://stopcovid19.metro.tokyo.lg.jp/data/130001_tokyo_covid19_patients.csv`
Insert cell
populationMap
Insert cell
weekBeforeLastData.get('')
Insert cell
getLength(weekBeforeLastData, 90, '男性')
Insert cell
getLength = (data, age, sex) => {
if (data.get(age)) {
if (data.get(age).get(sex)) {
return data.get(age).get(sex).length;
} else {
return 0;
}
} else {
return 0;
}
}
Insert cell
ages = d3.range(80, -10, -10)
Insert cell
sexes = ['男性', '女性']
Insert cell
weekBeforeLastData = d3.group(
weekBeforeLastCases.filter(d => d.age >= 0),
d => d.age,
d => d.sex
)
Insert cell
lastData = d3.group(
lastWeekCases.filter(d => d.age >= 0),
d => d.age,
d => d.sex
)
Insert cell
weekBeforeLastCases = filterDates(cases, new Date(2021, 3 - 1, 13))
Insert cell
lastWeekCases = filterDates(cases, new Date(2021, 3 - 1, 20))
Insert cell
filterDates = (data, date, duration = 7) => {
return data.filter(
d =>
d.date.getTime() >= date.getTime() &&
d.date.getTime() <= date.getTime() + (duration - 1) * 1000 * 60 * 60 * 24
);
}
Insert cell
latestDate = d3.max(cases, d => d.date)
Insert cell
d3.group(cases, d => d.age)
Insert cell
d3.group(cases, d => d.sex)
Insert cell
cases = tokyoCases
Insert cell
tokyoCases = (await d3.csv(
'https://sugi2000cors.herokuapp.com/' + uri,
d3.autoType
))
.map(d => ({
date: d.公表_年月日,
sex: d.患者_性別,
age: ageGroup(d.患者_年代),
...d
}))
.filter(d => d.age >= 0)
.filter(d => d.sex === '男性' || d.sex === '女性')
Insert cell
ageGroup = rawAge => {
let ageGroup;
switch (rawAge) {
case '100歳以上':
case '90代':
ageGroup = 80;
break;
case '10歳未満':
ageGroup = 0;
break;
case '不明':
ageGroup = -1;
break;
case '-':
ageGroup = -1;
break;
default:
ageGroup = +rawAge.substr(0, 2);
break;
}
return ageGroup;
}
Insert cell
function ec(r, c) {
return XLSX.utils.encode_cell({ r: r, c: c });
}
Insert cell
function delete_row(ws, row_index) {
var variable = XLSX.utils.decode_range(ws["!ref"]);
for (var R = row_index; R < variable.e.r; ++R) {
for (var C = variable.s.c; C <= variable.e.c; ++C) {
ws[ec(R, C)] = ws[ec(R + 1, C)];
}
}
variable.e.r--;
ws['!ref'] = XLSX.utils.encode_range(variable.s, variable.e);
}
Insert cell
ws = {
const wb = await loadExcelWorkbook(
'https://sugi2000cors.herokuapp.com/' + uri,
{
type: 'array',
cellDates: true
}
);
const ws = wb.Sheets[wb.SheetNames[0]];
delete_row(ws, 0);
delete_row(ws, 0);
delete_row(ws, 0);
delete_row(ws, 1);
delete_row(ws, 1);
return ws;
}
Insert cell
populationMap = new Map(
ages.map(age => [
age,
new Map(sexes.map(sex => [sex, getPopulation(age, sex)]))
])
)
Insert cell
getPopulation(0, '男性')
Insert cell
getPopulation = (age, sex) => {
const startAge = age;
const endAge = startAge + 10;

return (
d3.sum(
population.filter(d => +d.age >= startAge && +d.age < endAge),
d => +d[sex]
) * 1000
);
}
Insert cell
population = tokyoPopulation
Insert cell
tokyoPopulation = (await FileAttachment("tokyo-population-2018-10.csv").csv())
.map((d) => d3.autoType(d))
.map((d) => ({
...d,
男性: d.男性 * 1000,
女性: d.女性 * 1000
}))
Insert cell
XLSX = xlsx
Insert cell
import { loadExcel, loadExcelWorkbook } from "@sugi2000/load-excel-file"
Insert cell
xlsx = require('xlsx@0.16.9/dist/xlsx.full.min.js')
Insert cell
import { Table } from "@observablehq/inputs"
Insert cell
d3 = require('d3@6')
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