Public
Edited
Jul 30, 2024
1 star
Insert cell
Insert cell
chart = {
const width = 1150;
const height = width;
const innerRadius = Math.min(width, height) * 0.5 - 20;
const outerRadius = innerRadius + 10;

const names = Array.from(new Set(data.flatMap(d => [d.source, d.target])));
const index = new Map(names.map((name, i) => [name, i]));
const matrix = Array.from(index, () => new Array(names.length).fill(0));
for (const {source, target, value} of data) matrix[index.get(source)][index.get(target)] += value;

const chord = d3.chordDirected()
.padAngle(12 / innerRadius)
.sortSubgroups(d3.descending)
.sortChords(d3.descending);

const arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);

const ribbon = d3.ribbonArrow()
.radius(innerRadius - 0.5)
.padAngle(1 / innerRadius);

const color = d3.scaleOrdinal(names, d3.schemeSet3);

const formatValue = x => `${x}`;

const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "width: 100%; height: auto; font: 10px sans-serif;");

const chords = chord(matrix);

const textId = DOM.uid("text");

svg.append("path")
.attr("id", textId.id)
.attr("fill", "none")
.attr("d", d3.arc()({outerRadius, startAngle: 0, endAngle: 2 * Math.PI}));

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

const g = svg.append("g")
.selectAll()
.data(chords.groups)
.join("g");

g.append("path")
.attr("d", arc)
.attr("fill", d => color(names[d.index]))
.attr("stroke", "#fff");

g.append("text")
.attr("dy", -3)
.append("textPath")
.attr("xlink:href", textId.href)
.attr("startOffset", d => d.startAngle * outerRadius)
.text(d => names[d.index]);

g.append("title")
.text(d => `${names[d.index]}
In: ${formatValue(d3.sum(matrix[d.index]))}
Out: ${formatValue(d3.sum(matrix, row => row[d.index]))}`);

return svg.node();
}
Insert cell
data = [
{source: 'Acmena smithii', target: 'Illawarra Escarpment Subtropical Rainforest', value: 1},
{source: 'Acmena smithii', target: 'Coastal Sandstone Gallery Rainforest', value: 1},
{source: 'Acmena smithii', target: 'Coastal Warm Temperate Rainforest', value: 1},
{source: 'Acmena smithii', target: 'Coastal Dune Littoral Rainforest', value: 1},
{source: 'Acmena smithii', target: 'Coastal Escarpment Littoral Rainforest', value: 1},
{source: 'LAcmena smithii', target: 'Coastal Headland Littoral Thicket', value: 1},
{source: 'Acmena smithii', target: 'Hinterland Dry Rainforest', value: 1},
{source: 'Ceratopetalum apetalum', target: 'Illawarra Escarpment Subtropical Rainforest', value: 1},
{source: 'Ceratopetalum apetalum', target: 'Coastal Sandstone Gallery Rainforest', value: 1},
{source: 'Ceratopetalum apetalum', target: 'Coastal Warm Temperate Rainforest', value: 1},
{source: 'Ceratopetalum apetalum', target: 'Coastal Escarpment Littoral Rainforest', value: 1},
{source: 'Livistona australis', target: 'Illawarra Escarpment Subtropical Rainforest', value: 1},
{source: 'Livistona australis', target: 'Coastal Warm Temperate Rainforest', value: 1},
{source: 'Livistona australis', target: 'Coastal Escarpment Littoral Rainforest', value: 1},
{source: 'Dendrocnide excelsa', target: 'Illawarra Escarpment Subtropical Rainforest', value: 1},
{source: 'Dendrocnide excelsa', target: 'Coastal Warm Temperate Rainforest', value: 1},
{source: 'Ficus spp.', target: 'Illawarra Escarpment Subtropical Rainforest', value: 1},
{source: 'Ficus spp.', target: 'Coastal Headland Littoral Thicket', value: 1},
{source: 'Ficus spp.', target: 'Hinterland Dry Rainforest', value: 1},
{source: 'Pittosporum undulatum', target: 'Coastal Sandstone Gallery Rainforest', value: 1},
{source: 'Pittosporum undulatum', target: 'Coastal Warm Temperate Rainforest', value: 1},
{source: 'Pittosporum undulatum', target: 'Coastal Escarpment Littoral Rainforest', value: 1},
{source: 'Cryptocarya spp.', target: 'Illawarra Escarpment Subtropical Rainforest', value: 1},
{source: 'Cryptocarya spp.', target: 'Coastal Warm Temperate Rainforest', value: 1},
{source: 'Glochidion ferdinandi', target: 'Coastal Escarpment Littoral Rainforest', value: 1},
{source: 'Glochidion ferdinandi', target: 'Coastal Dune Littoral Rainforest', value: 1},
{source: 'Tristaniopsis laurina', target: 'Coastal Sandstone Gallery Rainforest', value: 1},
{source: 'Tristaniopsis laurina', target: 'Coastal Warm Temperate Rainforest', value: 1},
{source: 'Synoum glandulosum', target: 'Coastal Escarpment Littoral Rainforest', value: 1},
{source: 'Synoum glandulosum', target: 'Coastal Headland Littoral Thicket', value: 1},
{source: 'Clerodendrum tomentosum', target: 'Coastal Sandstone Gallery Rainforest', value: 1},
{source: 'Clerodendrum tomentosum', target: 'Hinterland Dry Rainforest', value: 1},
{source: 'Notelaea longifolia', target: 'Hinterland Dry Rainforest', value: 1},
{source: 'Notelaea longifolia', target: 'Coastal Escarpment Littoral Rainforest', value: 1}
// Adding more relationships as needed
]
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