Published
Edited
Nov 26, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// graph = {return {nodes:cities, links:flights}};
Insert cell
Insert cell
Insert cell
Insert cell
function createNodes(links, param) {
// unique list of destinations
let target_airports = [...new Set(links.map(obj => obj.target))]
// include origin in the list
target_airports.push(param)
// change nodes' key name
let nodes_unfiltered=[]
cities.forEach(function(entry) {
var singleObj = {};
singleObj['name'] = entry;
nodes_unfiltered.push(singleObj);
});
let nodes = nodes_unfiltered.filter(d=> target_airports.includes(d.name))
return nodes
}
Insert cell
Insert cell
nodes = createNodes(createLinks(parameter.airport),parameter.airport )
Insert cell
import {form} from "@mbostock/form-input"
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
// const links = data.links.map(d => { Object.create(d); });
// const nodes = data.nodes.map(d => Object.create(d));
// const links = createLinks(parameter.airport);
// const nodes = createNodes(createLinks(parameter.airport),parameter.airport );
console.log(nodes);
const simulation = d3.forceSimulation(nodes)
.force('charge', d3.forceManyBody().strength(-10))
.force('center', d3.forceCenter(width /2, height /2))
.force('link', d3.forceLink().links(links).id(function(d,i) {
return d.name
}).distance(d=>linkScale(links[d.index].distance)));
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const link = svg.append("g")
.attr("stroke-opacity", 0.1)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke", 'lightgrey')
.attr("stroke-width", '2');
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 5)
// .attr("fill", 'lightblue')
.attr("fill", d=> { if(d.name === parameter.airport){return"orange"}
else{ return "lightblue" }
})
.call(drag(simulation));

var texts = svg.selectAll(".texts")
.data(nodes)
.join("text")
.attr("dy", -4)
.attr("font-family", "sans-serif")
.attr('font-size', '10px')
.attr('text-anchor', 'middle')
.text( d => d.name);
simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
texts
.attr("x", d => d.x)
.attr("y", d => d.y);
});

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

return svg.node();

}
Insert cell
Insert cell
drag = simulation => {
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
linkScale = d3.scaleLinear()
.domain(d3.extent(links.map(d=>d.distance)))
// .range([maxDistance,minDistance]);
.range([130,20]);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@6");
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more