Published
Edited
May 31, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
maxHeight = height > 675 ? 675 : height
Insert cell
widthFixed = 1180
Insert cell
width
Insert cell
54 / 94
Insert cell
height = width < 700 ? widthFixed * 0.5 : widthFixed * 0.53
Insert cell
html`<style>

// .legend {
// font-size: 20vw
// }

</style>`
Insert cell
color = d3.scaleSequential(
[0, 6],
["#f3f3f3", "#f1e3d7", "#e6cdb7", "#dcbb9d", "#d5ac89", "#cfa178", "#c9986a"]
)
Insert cell
// import { Legend } from "@d3/color-legend"
Insert cell
function legend({ color, ...options }) {
return Legend(color, options);
}
Insert cell
// Copyright 2021, Observable Inc.
// Released under the ISC license.
// https://observablehq.com/@d3/color-legend
function Legend(
color,
{
title,
tickSize = 6,
width = 320,
height = 44 + tickSize,
marginTop = 18,
marginRight = 0,
marginBottom = 16 + tickSize,
marginLeft = 0,
ticks = widthFixed / 64,
tickFormat,
tickValues
} = {}
) {
function ramp(color, n = 256) {
const canvas = document.createElement("canvas");
canvas.width = n;
canvas.height = 1;
const context = canvas.getContext("2d");
for (let i = 0; i < n; ++i) {
context.fillStyle = color(i / (n - 1));
context.fillRect(i, 0, 1, 1);
}
return canvas;
}

const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.style("overflow", "visible")
.style("display", "block");

let tickAdjust = (g) =>
g.selectAll(".tick line").attr("y1", marginTop + marginBottom - height);
let x;

// Continuous
if (color.interpolate) {
const n = Math.min(color.domain().length, color.range().length);

x = color
.copy()
.rangeRound(
d3.quantize(d3.interpolate(marginLeft, width - marginRight), n)
);

svg
.append("image")
.attr("x", marginLeft)
.attr("y", marginTop)
.attr("width", width - marginLeft - marginRight)
.attr("height", height - marginTop - marginBottom)
.attr("preserveAspectRatio", "none")
.attr(
"xlink:href",
ramp(
color.copy().domain(d3.quantize(d3.interpolate(0, 1), n))
).toDataURL()
);
}

// Sequential
else if (color.interpolator) {
x = Object.assign(
color
.copy()
.interpolator(d3.interpolateRound(marginLeft, width - marginRight)),
{
range() {
return [marginLeft, width - marginRight];
}
}
);

svg
.append("image")
.attr("x", marginLeft)
.attr("y", marginTop)
.attr("width", width - marginLeft - marginRight)
.attr("height", height - marginTop - marginBottom)
.attr("preserveAspectRatio", "none")
.attr("xlink:href", ramp(color.interpolator()).toDataURL());

// scaleSequentialQuantile doesn’t implement ticks or tickFormat.
if (!x.ticks) {
if (tickValues === undefined) {
const n = Math.round(ticks + 1);
tickValues = d3
.range(n)
.map((i) => d3.quantile(color.domain(), i / (n - 1)));
}
if (typeof tickFormat !== "function") {
tickFormat = d3.format(tickFormat === undefined ? ",f" : tickFormat);
}
}
}

// Threshold
else if (color.invertExtent) {
const thresholds = color.thresholds
? color.thresholds() // scaleQuantize
: color.quantiles
? color.quantiles() // scaleQuantile
: color.domain(); // scaleThreshold

const thresholdFormat =
tickFormat === undefined
? (d) => d
: typeof tickFormat === "string"
? d3.format(tickFormat)
: tickFormat;

x = d3
.scaleLinear()
.domain([-1, color.range().length - 1])
.rangeRound([marginLeft, width - marginRight]);

svg
.append("g")
.selectAll("rect")
.data(color.range())
.join("rect")
.attr("x", (d, i) => x(i - 1))
.attr("y", marginTop)
.attr("width", (d, i) => x(i) - x(i - 1))
.attr("height", height - marginTop - marginBottom)
.attr("fill", (d) => d);

tickValues = d3.range(thresholds.length);
tickFormat = (i) => thresholdFormat(thresholds[i], i);
}

// Ordinal
else {
x = d3
.scaleBand()
.domain(color.domain())
.rangeRound([marginLeft, width - marginRight]);

svg
.append("g")
.selectAll("rect")
.data(color.domain())
.join("rect")
.attr("x", x)
.attr("y", marginTop)
.attr("width", Math.max(0, x.bandwidth() - 1))
.attr("height", height - marginTop - marginBottom)
.attr("fill", color);

tickAdjust = () => {};
}

svg
.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.call(
d3
.axisBottom(x)
.ticks(ticks, typeof tickFormat === "string" ? tickFormat : undefined)
.tickFormat(typeof tickFormat === "function" ? tickFormat : undefined)
.tickSize(tickSize)
.tickValues(tickValues)
)
.call(tickAdjust)
.call((g) => g.select(".domain").remove())
.call((g) =>
g
.append("text")
.attr("x", marginLeft)
.attr("y", marginTop + marginBottom - height - 6)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.attr("font-weight", "bold")
// .style("font-size", "clamp(3.2rem, 2vw, 2.5rem)")
.attr("class", "title")
.text(title)
);

return svg.node();
}
Insert cell
width
Insert cell
scaleFactor = widthFixed > 600
? widthFixed > 1000
? 1400
: widthFixed > 800
? 1100
: 800
: 500
Insert cell
projection = d3
.geoOrthographic()
.scale(scaleFactor)
.rotate([135, -50])
.translate([widthFixed / 2, maxHeight / 2 + 20])
Insert cell
allFishGeo[SpeciesShow]
Insert cell
us = states
Insert cell
CAProvincesGeoJSON = FileAttachment("canada_provinces.geojson").json()
Insert cell
basinOpacity = 0.2
Insert cell
basinColor = "#BA7537"
Insert cell
allFishGeo
Insert cell
// daWorld = topojson.feature(world, world.objects.countries).features
Insert cell
// borders = topojson.mesh(world, world.objects.countries, (a, b) => a !== b)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { stateShapes as states } from "@enjalot/us-county-datasets"
// import { states } from "@mbrownshoes/integrated-coastal-observatory-ico-visualizations"
Insert cell
Insert cell
// world = d3.json("https://cdn.jsdelivr.net/npm/world-atlas@2/countries-50m.json")
Insert cell
// land = topojson.feature(world, world.objects.land)
Insert cell
Insert cell
allFish[4]
Insert cell
Insert cell
CohoBasinsTopo = await FileAttachment("coho_distribution_basins4326@1.json").json()
Insert cell
CohoBasins = topojson.feature(
CohoBasinsTopo,
CohoBasinsTopo.objects.coho_distribution_basins4326
).features
Insert cell
ChumBasins = topojson.feature(
ChumBasinsTopo,
ChumBasinsTopo.objects.chum_distribution_basins4326
).features
Insert cell
ChumBasinsTopo = await FileAttachment(
"chum_distribution_basins4326@1.json"
).json()
Insert cell
SockeyeBasins = topojson.feature(
SockeyeBasinsTopo,
SockeyeBasinsTopo.objects.sockeye_distribution_basins4326
).features
Insert cell
SockeyeBasinsTopo = await FileAttachment(
"sockeye_distribution_basins4326@1.json"
).json()
Insert cell
SteelheadeBasinsTopo = await FileAttachment(
"Steelhead_distribution_basins4326 (2)@1.json"
).json()
Insert cell
SteelheadBasins = topojson.feature(
SteelheadeBasinsTopo,
SteelheadeBasinsTopo.objects.Steelhead_distribution_basins4326
).features
Insert cell
ChinookheadeBasinsTopo = await FileAttachment(
"Chinook_distribution_basins4326 (2).json"
).json()
Insert cell
ChinookBasins = topojson.feature(
ChinookheadeBasinsTopo,
ChinookheadeBasinsTopo.objects.Chinook_distribution_basins4326
).features
Insert cell
PinkBasinsTopo = await FileAttachment("pink_distribution_basins4326@1.json").json()
Insert cell
PinkBasins = topojson.feature(
PinkBasinsTopo,
PinkBasinsTopo.objects.pink_distribution_basins4326
).features
Insert cell
Insert cell
allBasinsData = FileAttachment("allBasins.json").json()
Insert cell
FileAttachment("us-10m.v1.json").json()
Insert cell
d3 = require("d3@6")
Insert cell
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