Published
Edited
Nov 26, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
graph = {return {nodes: cities.map(d => ({ airport: d })), links:flights}};
Insert cell
graph.links[0].destination
Insert cell
{
let testMatrix = [];
for(let i in cities){
var newConn = {};
newConn[cities[i]] = 0;
testMatrix.push(newConn);
}
}
Insert cell
graphMatrix = {
let matrix = graph.nodes;
for(let i in matrix){
matrix[i].connections = [];
for(let j in cities){
var orig = matrix[i].airport;
var dest = cities[j];
var newConn = {};
newConn[dest] = 0;
for(let k in flights){
if((flights[k].origin == orig) && (flights[k].destination == dest)){
newConn[dest] = flights[k].distance;
}
}
matrix[i].connections.push(newConn);
}
}

return matrix;
}
Insert cell
Insert cell
Insert cell
matrixChart = {
const svg = d3.create('svg')
.attr('viewBox', [0, 0, width, height]);
var chart = svg.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
var row = svg.selectAll('g.row')
.data(graphMatrix)
.join('g')
.attr('class', 'row')
.attr('transform', function (d, i) {return 'translate(0,' + x(i) + ')'; })
.each(makeRow)
function makeRow(d, i) {
const rowData = d;
//console.log(rowData);
var cell = d3.select(this).selectAll('rect')
.data(graphMatrix)
.join('rect')
.attr('x', (d, i) => x(i))
.attr('width', x.bandwidth())
.attr('height', x.bandwidth())
.style('fill', (d, i) => {
//console.log(d.connections);
return colorScale(Object.values(d.connections[i]));
//console.log(Object.values(d.connections[1]) == 0);
/*if (Object.values(d.connections[i]) == 0){
return '#cecece';
} else {
return colorScale(Object.values(d.connections[i]));
}*/
})
}
//console.log(graphMatrix)
row.append('text')
.attr('class', 'label')
.attr('x', margin.left - 4)
.attr('y', x.bandwidth() / 2)
.attr('dy', '0.32em')
.attr('font-size', '10px')
.attr('text-anchor', 'end')
.text(d => d.aiport);
var column = svg.selectAll('g.column')
.data(graphMatrix)
.join('g')
.attr('class', 'column')
.attr('transform', function(d, i) { return 'rotate(-90, '+margin.top+', '+margin.left+')'; })
.append('text')
.attr('class', 'label')
.attr('x', margin.top + 4)
.attr('y', (d,i) => x(i) + x.bandwidth() / 2)
.attr('dy', '0.32em')
.attr('font-size', '10px')
.text(d => d.airport)
return svg.node();
}
Insert cell
chart = {
const links = graph.links.map(d => Object.create(d));
const nodes = graph.nodes.map(d => Object.create(d));

const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).distance(d => linkScale(graph.links[d.index].score)))
.force("charge", d3.forceManyBody().strength(-250))
.force("center", d3.forceCenter(width / 2, height / 2).strength(0.4));

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const link = svg.append("g")
.attr("stroke-opacity", 0.3)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke", "gray")
.attr("stroke-width", d => Math.sqrt(d.score)*0.15);

const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 5)
.attr("fill", "black")
.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.index );

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
colorScale(Object.values(graphMatrix[0].connections[19]))
Insert cell
Object.values(graphMatrix[0].connections[14])
Insert cell
Insert cell
linkScale = d3.scaleLinear()
.domain(d3.extent(graph.links.map(d=>d.distance)))
.range([150,100]);
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
x = d3.scaleBand()
.rangeRound([margin.left, width-margin.right])
.domain(d3.range(graphMatrix.length))
.paddingInner(0.1)
.align(0);
Insert cell
maxDistance = {
var distances = [];
for (let i in flights) {
var newDist = flights[i].distance;
distances.push(newDist);
}
return d3.max(distances);
}
Insert cell
colorScale = d3.scaleSequential().domain([0, maxDistance]).interpolator(d3.interpolateRdPu);
Insert cell
height = 900
Insert cell
width = 900
Insert cell
margin = ({top: 50, right: 50, bottom: 50, left: 50});
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