Public
Edited
Jul 24, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof textSize = rangeSlider({
min: 5,
max: 200,
// Note that values must be specified as array of length 2.
value: this ? this.value : [12, 16],
// Custom slider CSS, replaces all styles.
// theme: themes[theme],
// Overrides the range color. Support for range colors is up to the theme.
title: 'Font size',
description: 'Control minimum and maximum font size',
})
Insert cell
Insert cell
Insert cell
viewof textToggle = Inputs.toggle({label: "Resize Text?", value: false})
Insert cell
viewof showText = Inputs.toggle({label: "Show Text?", value: true})
Insert cell
viewof differStroke = Inputs.toggle({label: "Emphasize internal travel?", value: true})
Insert cell
apr19 = chart1(data1, 'April', '2019')
Insert cell
oct19 = chart1(data2, 'October', '2019')
Insert cell
nov19 = chart1(data3, 'November', '2019')
Insert cell
apr20 = chart1(data4, 'April', '2020')
Insert cell
oct20 = chart1(data5, 'October', '2020')
Insert cell
nov20 = chart1(data6, 'November', '2020')
Insert cell
apr21 = chart1(data7, 'April', '2021')
Insert cell
data7
Insert cell
font = 'Gill Sans'
Insert cell
data1.nodes[0]
Insert cell
data2.nodes[0]
Insert cell
colors = d3.scaleSequential([100, height], d3.interpolateTurbo)
Insert cell
chart1 = function(data, month, year) {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
// data.nodes.forEach((row) => {
// rDomain.push(row.dayAve)
// });

// data.nodes.forEach((row) => {
// regionDomain.push(row.id)
// });

// data.links.forEach((row) => {
// strokeDomain.push(row.dayAve)
// });
let radiusFxn = d3[radiusMeasure]().domain(d3.extent(rDomain)).range(rNode)
let strokeFxn = d3[scaleMeasure]().domain(d3.extent(strokeDomain)).range(sWidth)
let colorFxn = d3.scaleOrdinal().domain(regionDomain).range(d3['schemeTableau10']);//.range(["#dd6e42","#e3ce94","#7199ab","#c0d6df","#c7bbbb"])
let textFxn = d3[radiusMeasure]().domain(rDomain).range(textSize);
let xScale = d3.scaleLinear().domain(longDomain).range([200, width-200])
let yScale = d3.scaleLinear().domain(latDomain).range([height-100, 100])

const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(25))
.force("charge", d3.forceManyBody().strength(-500))
.force('collision', d3.forceCollide().radius(100))
.force("center", d3.forceCenter(width / 1.85, height / 2))
.force('x', d3.forceX((d) => xScale(d.x)).strength(5))
.force('y', d3.forceY((d) => yScale(d.y)).strength(5));

const svg = d3.select(DOM.svg(width, height));

svg.append("text")
.attr("x", (width / 2))
.attr("y", 50)
.attr("text-anchor", "middle")
.style("font-size", "48px")
.attr("font-family", font)
.attr("fill", "#000")
.text(month + ' ' + year);


const gradient = DOM.uid();
svg.append("linearGradient")
.attr("id", gradient.id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", 0)
.attr("y1", height - 100)
.attr("x2", 0)
.attr("y2", 100)
.selectAll("stop")
.data(d3.ticks(0, 1, 10))
.join("stop")
.attr("offset", d => d)
.attr("stop-color", colors.interpolator());
const link = svg.append("g")

.selectAll("path")
.data(links)
.join("path")
.attr("stroke-width", d => strokeFxn(d[volMeasure]))
.attr("stroke", d => {console.log(d.source.region); return colorFxn(d.source.region)})
//.attr('stroke', '#e8e8e8')
// .attr("stroke", gradient)
.attr("fill", "transparent")
.attr("stroke-opacity", d => {
if (differStroke === true) {
if(d.source.id === d.target.id) {
return 0.5;
} else {
return 0.125;
}
}

return 0.2;
})
// .attr("marker-end", "url(#end)");

const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 6)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => radiusFxn(d.dayAve))
.attr("fill", d => colorFxn(d.region))
.call(drag(simulation))

const textElements = svg.append('g')
.selectAll('text')
.data(nodes)
.enter().append('text')
.text(node => node.id)
// .attr('font-size', 18)
.attr('font-size', node => {
if (textToggle === true) {
return textFxn(node.volume);
}
return 24;
})
.attr("font-family", font)
.attr("fill", "#000")
.attr('dx', -20)
.attr('dy', 4)
.attr('opacity', showText ? 1 : 0);

simulation.on("tick", () => {
link
.attr("d", function(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy)*arcness;
if (dx === 0 && dy ===0) {
// Fiddle with this angle to get loop oriented.
const xRotation = -45;
// Needs to be 1.
const largeArc = 1;
let sweep = 0
// Change sweep to change orientation of loop.
if (d.source.region === 'North-East' || d.source.region === 'East' || d.source.region === 'North') {
sweep = 1;
}
// Make drx and dry different to get an ellipse
// instead of a circle.
const drx = 30;
const dry = 30;

const x1 = d.source.x;
const y1 = d.source.y;
// For whatever reason the arc collapses to a point if the beginning
// and ending points of the arc are the same, so kludge it.
const x2 = d.target.x + 1;
const y2 = d.target.y + 1;
return "M" + x1 + "," + y1 + "A" + drx + "," + dry + " " + xRotation + "," + largeArc + "," + sweep + " " + x2 + "," + y2;
} else {
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
}

});
textElements
.attr("x", node => node.x)
.attr("y", node => node.y)
// node
// .attr("cx", d => d.x)
// .attr("cy", d => d.y);

node.attr('transform', (d) => {
return 'translate(' + (d.x) + ',' + (d.y) + ')';
});
});

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
rDomain = [1800, 3462700]
Insert cell
latDomain = [1.1304753, 1.4504753]
Insert cell
longDomain = [103.6920359, 104.0120359]
Insert cell
strokeDomain = {
if (volMeasure == 'dayAve') {
return [0, 397100]
} else {
return [0.01, 0.6]
}
}
Insert cell
regionDomain = ['Central', 'East', 'North','North-East', 'West']
Insert cell
# Data imports
Insert cell
data1 = FileAttachment("flows_total_pa4_2019@5.json").json()
Insert cell
data2 = FileAttachment("flows_total_pa10_2019.json").json()
Insert cell
data3 = FileAttachment("flows_total_pa11_2019.json").json()
Insert cell
data4 = FileAttachment("flows_total_pa4_2020.json").json()
Insert cell
data5 = FileAttachment("flows_total_pa10_2020.json").json()
Insert cell
data6 = FileAttachment("flows_total_pa11_2020.json").json()
Insert cell
data7 = FileAttachment("flows_total_pa4_2021.json").json()
Insert cell
height = 1200
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.group);
}
Insert cell
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
d.fixed = true;
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
d3 = require("d3@5")
Insert cell
import {rangeSlider} from '@mootari/range-slider'
Insert cell
import {slider} 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