Published
Edited
Aug 31, 2020
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@5");
Insert cell
Insert cell
carData = fetch("https://vega.github.io/editor/data/cars.json")
.then(result => result.json());
Insert cell
Insert cell
aggregatedDimension = "Horsepower"
Insert cell
Insert cell
Insert cell
d3ArrayFunctions = require("d3-array@^2.2") // read more at https://github.com/d3/d3-array
Insert cell
groupedByYears = d3ArrayFunctions.group(carData, d => d.Year);
Insert cell
meanDimensionGroupedByYears = {
const meanDimensionGroupedByYears = {};
for (let [year, cars] of groupedByYears) {
const meanForThatYear = d3ArrayFunctions.mean(cars, d => d[aggregatedDimension]);
meanDimensionGroupedByYears[year] = meanForThatYear;
}
yield meanDimensionGroupedByYears;
}
Insert cell
Insert cell
Insert cell
svg`
<svg id="canvas">
<g class="vis">
<g class="marks"></g>
<g class="axis x"></g>
<g class="axis y"></g>
</g>
</svg>
`
Insert cell
width = 1000
Insert cell
height = 500
Insert cell
margin = 25
Insert cell
canvas = d3.select("#canvas")
// set the size of the canvas to (width x height)
Insert cell
vis = canvas.select(".vis")
// apply margin to the visualization by moving the container <g>, so axis labels do not overflow the canvas
Insert cell
Insert cell
Insert cell
years = {
const yearIterator = groupedByYears.keys();
const years = [];
let hasNext = true;
while (hasNext) {
let nextYear = yearIterator.next();
hasNext = !nextYear.done;
if (hasNext) years.push(nextYear.value);
}
yield years;
}
Insert cell
// scaleX = ...
Insert cell
listOfMeanValues = Object.values(meanDimensionGroupedByYears);
Insert cell
// scaleY = ...
Insert cell
Insert cell
Insert cell
// axisX = ...
Insert cell
// axisY = ...
Insert cell
// canvas.select ...
Insert cell
// canvas.select ...
Insert cell
Insert cell
Insert cell
// marks =
Insert cell
// barMark = ...
Insert cell
Insert cell
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