Public
Edited
Apr 26, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3['schemeTableau10']
Insert cell
chart(data1, 'April', '2019')
Insert cell
chart(data2, 'October', '2019')
Insert cell
chart(data3, 'November', '2019')
Insert cell
chart(data4, 'April', '2020')
Insert cell
chart(data5, 'October', '2020')
Insert cell
chart(data6, 'November', '2020')
Insert cell
chart(data7, 'April', '2021')
Insert cell
chart(data4, 'April', '2020')
Insert cell
data4.links[24]
Insert cell
data7.links[24]
Insert cell
font = 'Gill Sans'
Insert cell
chart = function(data, month, year) {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
let regionDomain = []
// 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 xScale = d3.scaleLinear().domain(longDomain).range([20, width-200])
let yScale = d3.scaleLinear().domain(latDomain).range([height-20, 20])

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

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);

// build the arrow.
svg.append("svg:defs").selectAll("marker")
.data(["end"]) // Different link/path types can be defined here
.enter().append("svg:marker") // This section adds in the arrows
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 1)
.attr("markerHeight", 1)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
const link = svg.append("g")
.attr("stroke-opacity", 0.3)
.selectAll("path")
.data(links)
.join("path")
.attr("stroke-width", d => strokeFxn(d[volMeasure]))
.attr("stroke", d => {console.log(d.source.id); return colorFxn(d.source.id)})
.attr("fill", "transparent")
// .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.id))
.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-family", font)
.attr("fill", "#000")
.attr('dx', -20)
.attr('dy', 4)

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.id === 'North-East' || d.source.id === 'East' || d.source.id === 'North') {
sweep = 1;
}
// Make drx and dry different to get an ellipse
// instead of a circle.
const drx = 70;
const dry = 70;

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 = [91100, 2754700]
Insert cell
latDomain = [1.1304753, 1.4504753]
Insert cell
longDomain = [103.6920359, 104.0120359]
Insert cell
strokeDomain = {
if (volMeasure == 'dayAve') {
return [1300, 1596600]
} else {
return [0.01, 0.6]
}
}
Insert cell
data1.nodes[0]
Insert cell
# Data imports
Insert cell
data1 = FileAttachment("flows_ratios_4_2019.json").json()
Insert cell
data2 = FileAttachment("flows_ratios_10_2019.json").json()
Insert cell
data3 = FileAttachment("flows_ratios_11_2019.json").json()
Insert cell
data4 = FileAttachment("flows_ratios_4_2020.json").json()
Insert cell
data5 = FileAttachment("flows_ratios_10_2020.json").json()
Insert cell
data6 = FileAttachment("flows_ratios_11_2020.json").json()
Insert cell
data7 = FileAttachment("flows_ratios_4_2021.json").json()
Insert cell
height = 800
Insert cell
width = 1000
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) {
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