Published
Edited
May 13, 2021
Importers
Insert cell
Insert cell
uri = `https://www.harp.lg.jp/opendata/dataset/1369/resource/3132/010006_hokkaido_covid19_patients.csv`
//`https://www.harp.lg.jp/opendata/dataset/1369/resource/2828/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
d3.group(cases, (d) => d.患者_居住地)
Insert cell
cases = hokkaidoCases
Insert cell
cases.slice(-10)
Insert cell
hokkaidoCases = {
const csvString = await getSourceStr(uri);
return (
d3
.csvParse(csvString, d3.autoType)
// return (await d3.csv(uri, d3.autoType))
.map((d) => {
// fix typo
if (d.患者_居住地 === "胆振振興局管内") {
d.患者_居住地 = "胆振総合振興局管内";
}
if (d.患者_居住地 === "宗谷総合誌振興局管内") {
d.患者_居住地 = "宗谷総合振興局管内";
}
if (d.患者_居住地 === "オホーツク振興局管内") {
d.患者_居住地 = "オホーツク総合振興局管内";
}
if (d.居住地 === "オホーツク総合振興局管内") {
d.患者_居住地 = "オホーツク総合振興局管内";
}
return {
date: d.公表_年月日,
sex: d.患者_性別,
age: ageGroup(d.患者_年代),
// d.年代 === "非公表" ? NaN : d.年代 === "10歳未満" ? 0 : d.age_group, //+d.年代.replace(/(\d+).+/, '$1'),
...d
};
})
.filter((d) => d.sex === "男性" || d.sex === "女性")
);
}
// 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
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 range = startAge < 80 ? 10 : 30;
const endAge = startAge + range;

return d3.sum(
population.filter((d) => +d.age >= startAge && +d.age < endAge),
(d) => +d[sex]
);
}
Insert cell
population = hokkaidoPopulation
Insert cell
hokkaidoPopulation = d3.tsvParse(popData, d3.autoType)
Insert cell
Insert cell
getSourceStr = async (uri) => {
const arrayData = new Uint8Array(await d3.buffer(uri));
const csvString = iconv.decode(arrayData, "Shift_JIS");
return csvString;
}
Insert cell
iconv = require("https://bundle.run/iconv-lite@0.6.2")
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