Public
Edited
Aug 13, 2023
2 forks
Insert cell
Insert cell
chart = {
// Specify the chart’s dimensions.
const width = 928;
const height = width;
const cx = width * 0.50; // adjust as needed to fit
const cy = height * 0.40; // adjust as needed to fit
const radius = Math.min(width, height) / 2 - 80;

// Create a radial tree layout. The layout’s first dimension (x)
// is the angle, while the second (y) is the radius.
const tree = d3.tree()
.size([2 * Math.PI, radius])
.separation((a, b) => (a.parent == b.parent ? 1 : 2) / a.depth);

// Sort the tree and apply the layout.
const root = tree(d3.hierarchy(data1)
.sort((a, b) => d3.ascending(a.data.name, b.data.name)));

// Creates the SVG container.
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-cx, -cy, width, height])
.attr("style", "width: 100%; height: auto; font: 13px sans-serif;");

// Append links.
svg.append("g")
.attr("fill", "none")
.attr("stroke", "#4e8905")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.0)
.selectAll()
.data(root.links())
.join("path")
.attr("d", d3.linkRadial()
.angle(d => d.x)
.radius(d => d.y));

// append URL's to each item that has it listed in the JSON table
const linkGroup = svg.append("g")
.attr("fill", "none")
linkGroup
.selectAll()
.data(root.links())
.join("path")
.attr("d", d3.linkRadial()
.angle(d => d.x)
.radius(d => d.y));

const nodeGroup = svg.append("g");
nodeGroup
.selectAll()
.data(root.descendants())
.join("a")
.attr("href", d => d.data.url)
.append("circle")
.attr("transform", d => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0)`)
.attr("fill", d => d.children ? "#416614" : "#a2db06")
.attr("r", 4);

const labelGroup = svg.append("g")
labelGroup
.selectAll()
.data(root.descendants())
.join("a")
.attr("href", d => d.data.url)
.append("text")
.attr("transform", d => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0) rotate(${d.x >= Math.PI ? 180 : 0})`)
.attr("dy", "0.31em")
.attr("x", d => d.x < Math.PI === !d.children ? 10 : -10) //text distance from circle
.attr("text-anchor", d => d.x < Math.PI === !d.children ? "start" : "end")
.attr("fill", "currentcolor") //doesn't change the font color of url alone
.text(d => d.data.name);

return svg.node();
}
Insert cell
data1 = FileAttachment("data@1.json").json()
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