Code

D3.js

Chord Diagram

Advanced Chord diagram visualising bidirectional flows between groups using d3.chord() and d3.ribbon(). Hover a ribbon for the flow value. d3-chord

// Square flow matrix [n × n]
const matrix = [
  [  0, 800, 600, 200, 100 ],
  [800,   0, 700, 300,  50 ],
  // …
];

const chord = d3.chord()
  .padAngle(0.04)
  .sortSubgroups(d3.descending);

const chords = chord(matrix);

// Outer arc for each group
const arc = d3.arc()
  .innerRadius(innerR)
  .outerRadius(outerR);

// Ribbon for each flow
const ribbon = d3.ribbon().radius(innerR);

// Render group arcs
g.selectAll('path')
  .data(chords.groups)
  .join('path')
  .attr('d', arc)
  .attr('fill', d => color(names[d.index]));

// Render ribbons
g.selectAll('path.ribbon')
  .data(chords)
  .join('path')
  .attr('d', ribbon)
  .attr('fill', d => color(
    names[d.source.index]));
Key APIs
  • d3.chord() — computes chord layout from an N×N matrix
  • chords.groups — one arc descriptor per matrix row/column
  • d3.arc() — draws the outer ring segments
  • d3.ribbon() — draws the curved flow ribbons between groups
  • .padAngle() — gap between group arcs in radians
  • chord.source / chord.target — each ribbon has two sub-group descriptors