Published
Edited
Aug 25, 2020
1 star
Also listed in…
Power & Energy
Insert cell
Insert cell
drawDorlingCartogramPowerStations({ width: 540, height: 540 })
Insert cell
await drawTablePowerStations()
Insert cell
md`### Appendix: Code`
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
Insert cell
import {
drawDorlingCartogram,
drawSimpleColorLegend,
drawSimpleCircleLegend
} from '@nuuuwan/dorling-cartogram'
Insert cell
import { addDefaults } from '@nuuuwan/option-utils'
Insert cell
import { arrayToIndex } from '@nuuuwan/list-utils'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
async function drawDorlingCartogramPowerStations(options = {}) {
const stationDataOriginal = await getStationData();
const stationData = stationDataOriginal.filter(x => x.end_year === '');

const maxCapacity = Math.max(...stationData.map(x => x.capacity_mw));
const stationIndex = arrayToIndex(stationData, x => x.station);

options = addDefaults(options, {
width: 800,
height: 800,
legendWidth: 200,
maxRadius: 64,
fRegionIDToColor: function(regionID) {
const info = stationIndex[regionID];
return TYPE_TO_COLOR[info['type']];
}
});

const capacityToRadius = capacity =>
options.maxRadius * Math.sqrt(capacity / maxCapacity);

options = addDefaults(options, {
fRegionIDToRadius: function(regionID) {
return capacityToRadius(stationIndex[regionID]['capacity_mw']);
},
fAppendLegend(svg) {
drawSimpleColorLegend(
svg,
options.width - options.legendWidth,
20,
'Generation Type',
['Hydro', 'Oil', 'Coal'].map(stationType =>
Object({
label: stationType,
color: TYPE_TO_COLOR[stationType]
})
),
options
);

drawSimpleCircleLegend(
svg,
options.width - options.legendWidth,
120,
'Generation Capacity',
[10, 100, 1000].map(capacity =>
Object({
label: `${capacity} MW`,
radius: capacityToRadius(capacity)
})
),
options
);
}
});

const regionIDToPolygonList = stationData.reduce(function(
regionIDToPolygonList,
x
) {
const regionID = x.station;
const polygonList = [[x.location]];
regionIDToPolygonList[regionID] = polygonList;
return regionIDToPolygonList;
},
{});
return drawDorlingCartogram(regionIDToPolygonList, options);
}
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