Public
Edited
Dec 26, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
connectionsFromTrips(filteredTrips)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
minMaxNumberOfTrips = d3.extent(groupedTrips.map(([_, l]) => l.length))
Insert cell
groupedTrips = d3.groups(finalTrips, (d) => d.portStart)
Insert cell
finalLocations = tripsToLocations(finalTrips)

Insert cell
Insert cell
finalTrips = {
const s = new Set([]);
for (const t of tripsWithCoordinates) {
if (!t.coorStart || !t.coorPurchased || !t.coorFinal) continue;
s.add(t);
}
return [...s];
}
Insert cell
percentageOfMissingCoordinates = {
return +((100 * portsWithoutCoordinates.length) / portNames.length).toFixed(
2
);
}
Insert cell
Insert cell
portsWithoutCoordinates = {
const s = new Set([]);
for (const t of tripsWithCoordinates) {
if (!t.coorStart) s.add(t.portStart);
if (!t.coorPurchased) s.add(t.portPurchased);
if (!t.coorFinal) s.add(t.portFinal);
}
return [...s];
}
Insert cell
tripsWithCoordinates[123]
Insert cell
tripsWithCoordinates = {
const r = [];
const ot = originalToCoordinates;
for (const t of trips) {
const o = Object.assign(t, {
coorStart: ot[t.portStart],
coorPurchased: ot[t.portPurchased],
coorFinal: ot[t.portFinal]
});
r.push(o);
}
return r;
}
Insert cell
Insert cell
Insert cell
locationsCounts = {
const o = {};
const portType = "portStart portPurchased portFinal".split(" ");

for (const t of trips) {
for (const pt of portType) {
const locName = t[pt];
if (!o[pt]) o[pt] = [];
o[pt][locName] = o[pt][locName] ? o[pt][locName] + 1 : 1;
}
}

const oa = [];
for (const pt of portType) {
for (const loc of Object.keys(o[pt])) {
oa.push({
portType: pt,
location: loc,
count: o[pt][loc]
});
}
}

return oa;
}
Insert cell
Insert cell
import { chart as mapSVG } from "@drio/svg-map-pan-zoom-locations"
Insert cell
locations.filter((e) => e.original === "Le Havre")[0].coordinates
Insert cell
Insert cell
Insert cell
Insert cell
function plotMap(data) {
const color = (d) => {
if (d.portType === "start") return "lightgreen";
if (d.portType === "purchased") return "steelblue";
if (d.portType === "final") return "tomato";
};
return Plot.plot({
width,
projection: { type: "mercator" },
marks: [
Plot.geo(countries, { fill: "currentColor", fillOpacity: 0.2 }),
Plot.dot(data, {
x: (d) => d.latitude,
y: (d) => d.longitude,
r: 5,
fill: color,
fillOpacity: 0.2,
strokeOpacity: 0.4
})
]
});
}
Insert cell
mapChart = {
visibility();
const coordinates = [...tripsToYear.map((e) => e.coordinates)];
let flat = [];
for (const listC of coordinates) {
for (const e of listC) {
flat.push(e);
}
}
return plotMap(flat);
}
Insert cell
Insert cell
Insert cell
d3.range(1, 100, 1)
Insert cell
Insert cell
Insert cell
lineChartToYear = {
const trips = tripsByYear.filter(({ year }) => year <= untilYear);
return plotLineChart(trips);
}
Insert cell
yearsRange = d3.extent(trips.map(({ year }) => year))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
trips[123]
Insert cell
Insert cell
function genMapTripsPerYearPerPort(portKey) {
return d3.group(
trips,
(d) => d.year,
(d) => d[portKey]
);
}
Insert cell
portNamesByType = ({
portStart: [...new Set(trips.map((e) => e.portStart))],
portPurchased: [...new Set(trips.map((e) => e.portPurchased))],
portFinal: [...new Set(trips.map((e) => e.portFinal))]
})
Insert cell
Insert cell
Insert cell
function prepareForRank(mapTrips, years, portNames) {
const r = [];
const accCounts = {};
for (const y of years) {
for (const p of portNames) {
if (!accCounts[p]) accCounts[p] = 0;

let yCount;
if (!mapTrips.has(y)) yCount = 0;
else if (!mapTrips.get(y).has(p)) yCount = 0;
else yCount = mapTrips.get(y).get(p).length;

r.push({
date: new Date(y, 0),
name: p,
category: "dummy",
value: yCount + accCounts[p]
});

accCounts[p] += yCount;
}
}
return r;
}
Insert cell
prepareForRank(
genMapTripsPerYearPerPort("portFinal"),
years,
portNamesByType.portFinal
)
Insert cell
Insert cell
Insert cell
locations.map(({ original }) => original)
Insert cell
Insert cell
separator = function (color = "steelblue") {
return html`<style>
.drio-x {
height:10px;
background-color: ${color};
}
</style>
<div class="drio-x"></div>`;
}
Insert cell
Insert cell
Insert cell
import {
locations as testLocations,
countries
} from "@drio/svg-map-pan-zoom-locations"
Insert cell
import { Scrubber } from "@mbostock/scrubber"
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