Public
Edited
Apr 12, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
gridMapLayer = d3.select(svgContainer).select("#gridmap") // put this on the gridmap layer
Insert cell
import {gridMap} from "@emfielduva/dvlib_maps"
Insert cell
usGridMapData = FileAttachment("usGridMapData.json").json()
Insert cell
gridOptions = ({
boxSize: 50,
labelField: "code"
})
Insert cell
myGridMap = gridMap(gridMapLayer,"usgrid",usGridMapData,gridOptions)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rollupData = d3.rollups(powerplants_2019, v => d3.sum(v, d => toNum(d["Plant annual net generation (MWh)"])), d => d["Plant state abbreviation"], d => d["Plant primary fuel generation category"]);
Insert cell
Insert cell
Insert cell
donutLayer = d3.select(svgContainer).select("#donuts") // put this on the donuts layer
Insert cell
donuts = {
let donutGroups = donutLayer.selectAll("g")
.data(rollupData)
.join("g")
.attr("id", d => d[0])
.attr("transform", d => {
let stateAbbr = d[0];
let xLoc = usGridMapData.filter(d => d.code == stateAbbr)[0].col * gridOptions.boxSize;
let yLoc = usGridMapData.filter(d => d.code == stateAbbr)[0].row * gridOptions.boxSize;
return "translate(" + xLoc + " " + yLoc + ")";
});

donutGroups.each(function (d,i,nodes) {
let thisData = d;
let thisSVGGroup = d3.select(this); // this state <g> element in the donuts layer
let thisStateFuelData = d[1]; // the data associated with just this state, as we run through the full set.
let thisStateFuelDataSorted = d3.sort(thisStateFuelData, (a,b) => d3.descending(a[1],b[1])); // sort it
//let donut = dvDonut(thisSVGGroup, thisStateFuelDataSorted, 25, 12);
// size the donuts to their relative generation
let thisStateSumGeneration = d3.sum(thisStateFuelData, d => d[1]); // total the values for this state.
let scaleThisStateDonut = scaleDonuts(thisStateSumGeneration); // scale that for size on the map
let donut = dvDonut(thisSVGGroup, thisStateFuelDataSorted, scaleThisStateDonut, 5);
// recalculate the shift for each one to center the donuts.
thisSVGGroup.attr("transform", d => {
let xLoc = (usGridMapData.filter(d => d.code == thisData[0])[0].col * gridOptions.boxSize) + (gridOptions.boxSize/2 - scaleThisStateDonut);
let yLoc = (usGridMapData.filter(d => d.code == thisData[0])[0].row * gridOptions.boxSize) + (gridOptions.boxSize/2 - scaleThisStateDonut);
return "translate(" + xLoc + " " + yLoc + ")";
})
})
}
Insert cell
Insert cell
stateTotals = d3.rollup(powerplants_2019, v => d3.sum(v, d => toNum(d["Plant annual net generation (MWh)"])), d => d["Plant state abbreviation"]);
Insert cell
stateMax = d3.max(stateTotals, d => d[1])
Insert cell
scaleDonuts = d3.scaleLinear().domain([0,stateMax]).range([10,25])
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