Public
Edited
May 6, 2024
Insert cell
Insert cell
Insert cell
import {powerplants_2019} from "@emfielduva/dvlib_sampledata"
Insert cell
powerplants_2019
Insert cell
Insert cell
benchmark = powerplants_2019.find(d => d["Plant name"] == "North Anna")
Insert cell
Insert cell
findDataNeighbors = (data,benchmarkElement,compareField,countAboveBelow,sortType) => {
if (!sortType) {sortType = d3.descending}
let toNum = (text) => Number(text.replace(/,/g,''))
let sortedData = d3.sort(data, (a,b) => sortType(toNum(a[compareField]),toNum(b[compareField])));
let benchmarkIndex = sortedData.indexOf(benchmarkElement);
let countAbove, countBelow;
if (benchmarkIndex < countAboveBelow) { // this element is close to the beginning, shift count positions.
let leftover = countAboveBelow - benchmarkIndex;
countAbove = countAboveBelow + leftover;
countBelow = benchmarkIndex;
} else if (benchmarkIndex > data.length - countAboveBelow) { // this element is close to the end, shift count positions
let leftover = data.length - benchmarkIndex;
countAbove = leftover;
countBelow = countAboveBelow + leftover;
} else {
countAbove = countBelow = countAboveBelow;
}
return sortedData.slice(benchmarkIndex-countAbove,benchmarkIndex+countAbove);
}
Insert cell
Insert cell
neighbors = findDataNeighbors(powerplants_2019,benchmark,"Plant annual net generation (MWh)",5,d3.descending)
Insert cell
neighbors
Type Table, then Shift-Enter. Ctrl-space for more 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