Published
Edited
1 fork
18 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// compute the balance of dollar notes as the difference between
// the sum of the rows and the sum of the columns of the OD matrix
balance = {
const outs = data.OD.map((l) => d3.sum(l));
const ins = d3.transpose(data.OD).map((l) => d3.sum(l));
return d3.zip(ins, outs).map(([in_, out_]) => in_ - out_);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
factor = (i, j) => {
const distance = Math.hypot(
points[i][0] - points[j][0],
points[i][1] - points[j][1]
);
return Math.pow(distance, exponent);
}
Insert cell
Insert cell
Insert cell
graph = ({
sources: Uint16Array.from(links, ([i]) => i),
targets: Uint16Array.from(links, ([, j]) => j),
costs: links.map(([i, j]) => factor(i, j))
})
Insert cell
Insert cell
flows = {
const A = new Map();

// compute the best tree from each starting point
for (let i = 0; i < regions.length; i++) {
const tree = [...shortest_tree({ graph, origins: [i] })].pop();

for (let j = 0; j < regions.length; j++) {
const p = data.OD[i][j];
let l;
let k = j;
while ((l = tree.predecessor[k]) > -1) {
A.set(`${l}-${k}`, (A.get(`${l}-${k}`) || 0) + p);
k = l;
}
}
}

return A;
}
Insert cell
Insert cell
Insert cell
// https://raw.githubusercontent.com/tributetotobler/ttt/main/data/dollars.json
data = FileAttachment("dollars.json").json()
Insert cell
points = data.coords.map(({ V2, V4 }) => [V2, V4])
Insert cell
regions = data.polygones.map((d) => d.name)
Insert cell
projection = d3
.geoIdentity()
.reflectY(true)
.fitExtent(
[
[0, 0],
[width, height]
],
{
type: "LineString",
coordinates: [
[5, 4],
[68, 45]
]
}
)
Insert cell
radius = d3.scaleSqrt([0, 1500], [2, 30])
Insert cell
height = 500
Insert cell
import { shortest_tree } from "@fil/dijkstra"
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