Published
Edited
Jul 15, 2022
Fork of Simple D3
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
raw = FileAttachment("data@4.json").json()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
height = 500
Insert cell
Insert cell
liesboschGeoJson = ({
coordinates: [
[
[5.1110458, 52.058982],
[5.1061106, 52.0493235],
[5.1092005, 52.042857],
[5.1164961, 52.0449686],
[5.1221609, 52.0564225],
[5.1110458, 52.058982]
]
],
type: "Polygon"
})
Insert cell
LiesboschArea = turf.polygon(liesboschGeoJson.coordinates)
Insert cell
Insert cell
function nFormatter(num, digits) {
// https://stackoverflow.com/a/9462382
const lookup = [
{ value: 1, symbol: "" },
{ value: 1e6, symbol: "M" },
{ value: 1e9, symbol: "B" },
{ value: 1e12, symbol: "T" }
];
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
var item = lookup
.slice()
.reverse()
.find(function (item) {
return num >= item.value;
});
return item
? (num / item.value).toFixed(digits).replace(rx, "$1") + item.symbol
: "0";
}
Insert cell
function nAreaFormatter(num, digits) {
// https://stackoverflow.com/a/9462382
const lookup = [
{ value: 1, symbol: "m²" },
{ value: 1e6, symbol: "km²" }
];
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
var item = lookup
.slice()
.reverse()
.find(function (item) {
return num >= item.value;
});
return item
? (num / item.value).toFixed(digits).replace(rx, "$1") + item.symbol
: "0";
}
Insert cell
// Copyright 2021 Observable, Inc.
// Released under the ISC license.
// https://observablehq.com/@d3/bar-chart
function BarChart(
data,
{
x = (d, i) => i, // given d in data, returns the (ordinal) x-value
y = (d) => d, // given d in data, returns the (quantitative) y-value
title, // given d in data, returns the title text
marginTop = 20, // the top margin, in pixels
marginRight = 0, // the right margin, in pixels
marginBottom = 30, // the bottom margin, in pixels
marginLeft = 40, // the left margin, in pixels
width = 640, // the outer width of the chart, in pixels
height = 400, // the outer height of the chart, in pixels
xDomain, // an array of (ordinal) x-values
xRange = [marginLeft, width - marginRight], // [left, right]
yType = d3.scaleLinear, // y-scale type
yDomain, // [ymin, ymax]
yRange = [height - marginBottom, marginTop], // [bottom, top]
xPadding = 0.1, // amount of x-range to reserve to separate bars
yFormat, // a format specifier string for the y-axis
yLabel, // a label for the y-axis
color = (d) => d // bar fill color
} = {}
) {
// Compute values.
const X = d3.map(data, x);
const Y = d3.map(data, y);
const Color = d3.map(data, color);

// Compute default domains, and unique the x-domain.
if (xDomain === undefined) xDomain = X;
if (yDomain === undefined) yDomain = [0, d3.max(Y)];
xDomain = new d3.InternSet(xDomain);

// Omit any data not present in the x-domain.
const I = d3.range(X.length).filter((i) => xDomain.has(X[i]));

// Construct scales, axes, and formats.
const xScale = d3.scaleBand(xDomain, xRange).padding(xPadding);
const yScale = yType(yDomain, yRange);
const xAxis = d3.axisBottom(xScale).tickSizeOuter(0);
const yAxis = d3.axisLeft(yScale).ticks(height / 40, yFormat);

// Compute titles.
if (title === undefined) {
const formatValue = yScale.tickFormat(100, yFormat);
title = (i) => `${X[i]}\n${formatValue(Y[i])}`;
} else {
const O = d3.map(data, (d) => d);
const T = title;
title = (i) => T(O[i], i, data);
}

const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr(
"style",
"max-width: 100%; height: auto; height: intrinsic;background-color:#404040"
);

svg
.append("g")
.attr("class", "axisWhite")
.attr("transform", `translate(${marginLeft},0)`)
.call(yAxis)
.call((g) => g.select(".domain").remove())
.call((g) =>
g
.selectAll(".tick line")
.clone()
.attr("x2", width - marginLeft - marginRight)
.attr("stroke-opacity", 0.25)
)
.call((g) =>
g
.append("text")
.attr("x", -marginLeft)
.attr("y", 10)
.attr("text-anchor", "start")
.text(yLabel)
);

const bar = svg
.append("g")
.selectAll("rect")
.data(I)
.join("rect")
.attr("fill", (i) => Color[i])
.attr("x", (i) => xScale(X[i]))
.attr("y", (i) => yScale(Y[i]))
.attr("height", (i) => yScale(0) - yScale(Y[i]))
.attr("width", xScale.bandwidth());

if (title) bar.append("title").text(title);

svg
.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.attr("class", "axisWhite")
.call(xAxis);

return svg.node();
}
Insert cell
mapboxgl = {
const gl = await require("mapbox-gl");
if (!gl.accessToken) {
gl.accessToken = TEMP_TOKEN;
const href = await require.resolve("mapbox-gl/dist/mapbox-gl.css");
document.head.appendChild(html`<link href=${href} rel=stylesheet>`);
}
return gl;
}
Insert cell
MapboxGeocoder = {
const geocoder = require("https://bundle.run/@mapbox/mapbox-gl-geocoder@4.7.4");
const href = await require.resolve(
"@mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.css"
);
document.head.appendChild(html`<link href=${href} rel=stylesheet>`);
return geocoder;
}
Insert cell
turf = require("@turf/turf@6.5.0/turf.min.js")
Insert cell
_ = require("lodash@4.17.21/lodash.js")
Insert cell
import { slider, number } from "@jashkenas/inputs"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more