// 各国の人口データから分級境界を計算する
const geojson = await d3.json('./geojson/world.geojson');
const values = geojson.features.map(f => f.properties.POP_EST);
// 自然分類(Jenks)で5階級に分級
// method: 'jenks' | 'equalInterval' | 'quantile' | 'stdDev'
const result = Thematika.classify(values, 5, 'jenks');
// result.thresholds → scaleThreshold の domain 用の境界値(4個)
// result.breaks → 最小値・最大値を含む全境界値(6個)
// D3のscaleThresholdにそのまま渡す
const colorScale = d3.scaleThreshold()
.domain(result.thresholds)
.range(d3.schemeYlOrRd[5]);
// コロプレスレイヤー
const choroplethLayer = new Thematika.GeojsonLayer({
data: geojson,
attr: {
fill: d => colorScale(d.properties.POP_EST),
stroke: '#fff',
'stroke-width': 0.5
}
});
map.addLayer('choropleth', choroplethLayer);
// 凡例(labelFormatで数値を整形)
const legend = new Thematika.LegendLayer({
scale: colorScale,
position: { top: 20, left: 20 },
title: '人口',
labelFormat: d3.format('.2s')
});
map.addLayer('legend', legend);