Published
Edited
Jul 12, 2021
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);

const chords = chord(dats.matrix);

const group = svg.append("g")
.attr("font-size", 10)
.attr("font-family", "sans-serif")
.selectAll("g")
.data(chords.groups)
.join("g");
group.append("path")
.attr("fill", d => color(names[d.index]))
.attr("d", arc)

group.append("text")
.each(d => (d.angle = (d.startAngle + d.endAngle) / 2))
.attr("dy", "0.35em")
.attr("transform", d => `
rotate(${(d.angle * 180 / Math.PI - 90)})
translate(${outerRadius + 5})
${d.angle > Math.PI ? "rotate(180)" : ""}
`)
.attr("text-anchor", d => d.angle > Math.PI ? "end" : null)
.text(d => names[d.index]);

group.append("title")
.text(d => `${names[d.index]}
${d3.sum(chords, c => (c.source.index === d.index) * c.source.value)} outgoing →
${d3.sum(chords, c => (c.target.index === d.index) * c.source.value)} incoming ←`);

svg.append("g")
.attr("fill-opacity", 0.75)
.selectAll("path")
.data(chords)
.join("path")
.style("mix-blend-mode", "multiply")
.attr("fill", d => color(names[d.source.index]))
.attr("d", ribbon)
.append("title")
.text(d => `From: ${names[d.source.index]} → To : ${names[d.target.index]} ${d.source.value}`);


return svg.node();
}
Insert cell
data.links.values()
Insert cell
data = d3.autoType(await FileAttachment("mails_links2.json")).json();


Insert cell
datasort = data.links.sort((a, b) => d3.descending(a.sector, b.sector))


Insert cell
names = Array.from(new Set(datasort.flatMap(d => [d.source])))

Insert cell
dats = {
const index = new Map(names.map((name, i) => [name, i]));
const nameByIndex = new Map(names.map((name, i) => [i, name]));
const matrix = Array.from(index, () => new Array(names.length).fill(0));
for (const {source, target} of data.links.filter(fil_dia).filter(fil_sector)) matrix[index.get(source)][index.get(target)] += 1;
return {matrix, index, nameByIndex}
}
Insert cell
groups = [
{sIndex: 0, eIndex: 10, title: 'Security', color: '#6f8ec1'},
{sIndex: 11, eIndex: 15, title: 'Information Technology', color: '#5487cb'},
{sIndex: 16, eIndex: 28, title: 'Facilities', color: '#ebd3ad'},
{sIndex: 29, eIndex: 33, title: 'Executive', color: '#8cc207'},
{sIndex: 34, eIndex: 46, title: 'Engineering', color: '#948f8c'},
{sIndex: 47, eIndex: 53, title: 'Administration', color: '#e6ecff'}
];
Insert cell
Insert cell
Insert cell
chord = d3.chordDirected()
.padAngle(12 / innerRadius)
.sortSubgroups(d3.descending)
.sortChords(d3.descending)
Insert cell
arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius)
Insert cell
ribbon = d3.ribbonArrow()
.radius(innerRadius - 0.5)
.padAngle(1 / innerRadius)
Insert cell
color = d3.scaleOrdinal(names, d3.schemeCategory10)
Insert cell
formatValue = x => `${x.toFixed(0)}B`
Insert cell
outerRadius = innerRadius + 10
Insert cell
innerRadius = Math.min(width, height) * 0.5 - 100
Insert cell
width = 800
Insert cell
height = width
Insert cell
import {select} from "@jashkenas/inputs"
Insert cell
import {rangeSlider} from "@mootari/range-slider"
Insert cell
d3 = require("d3@6")
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