{
const width = 928;
const height = width;
const cx = width * 0.5;
const cy = height * 0.54;
const outerRadius = Math.min(width, height) / 2 - 20;
const innerRadiusForTrends = outerRadius * 0.2;
const trendColors = new Map([
["Growing self-censorship", "#730362"],
["Harm to independent outlets", "#9D0998"],
["Erosion of public access", "#0CA5B3"],
["Media curry favor with the president", "#1A2E7F"],
["Hostility of media environment", "#28124A"]
]);
const tree = d3.cluster()
.size([2 * Math.PI, outerRadius * 0.8])
.separation((a, b) => (a.parent == b.parent ? 1 : 2) / a.depth);
const root = tree(d3.hierarchy({name: "Root", children: nest(data, ["Trend", "Data", "Item"])})
.sort((a, b) => d3.ascending(a.data.name, b.data.name)));
const itemTypeColors = d3.scaleOrdinal()
.domain(["bad", "good"])
.range(["#FF312E", "#1CD81F"]);
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-cx, -cy, width, height])
.attr("style", "width: 100%; height: auto; font: 10px sans-serif;");
// Append links (representing the connection to the 'Data' level)
const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll()
.data(root.links())
.join("path")
.attr("d", d3.linkRadial()
.angle(d => d.x)
.radius(d => d.y))
.attr("stroke", d => d.target.depth === 2 && d.target.parent ? trendColors.get(d.target.parent.data.name) : "#555"); // Color Data level links
// Append labels for Trends following the lines
svg.append("g")
.attr("font-weight", "bold")
.attr("font-size", "12px")
.selectAll()
.data(root.children) // Target the direct children (Trends)
.join("text")
.attr("transform", d => {
const angle = d.x * 180 / Math.PI;
const radius = innerRadiusForTrends; // Position labels at the defined inner radius
return `rotate(${angle - 90}) translate(${radius}, 0) rotate(${angle < 180 ? 0 : 180})`;
})
.attr("dy", "0.31em")
.attr("text-anchor", "middle")
.attr("paint-order", "stroke")
.attr("stroke", "white")
.attr("fill", d => trendColors.get(d.data.name) || "currentColor") // Use mapped color or default
.text(d => d.data.name);
// Append nodes.
const node = svg.append("g")
.selectAll()
.data(root.descendants())
.join("g")
.attr("transform", d => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0)`);
node.append("circle")
.attr("fill", d => d.children ? "#555" : itemTypeColors(d.data.Type)) // Color leaf nodes based on Type
.attr("r", 2.5);
// Append labels with color-coded text, connecting lines, and links for Items
node.filter(d => !d.children && d.depth > 1) // Target leaf nodes (Items)
.append("g") // Group for the label and its connecting line
.attr("transform", d => `rotate(${d.x >= Math.PI ? 180 : 0})`) // Flip text orientation
.call(g => g.append("path") // Connecting line
.attr("d", `M${d => d.y + 2.5},0 L${d => (d.y + 15)},0`) // Adjust line length (15) as needed
.attr("stroke", d => itemTypeColors(d.data.Type))
.attr("stroke-opacity", 0.8)
.attr("stroke-width", 1))
.call(g => g.append("a")
.attr("xlink:href", d => d.data.URL)
.attr("target", "_blank")
.append("text")
.attr("dy", "0.31em")
.attr("x", d => d.x < Math.PI ? 20 : -20) // Adjust text position (20) to avoid overlap
.attr("text-anchor", d => d.x < Math.PI ? "start" : "end")
.attr("paint-order", "stroke")
.attr("stroke", "white")
.attr("fill", d => itemTypeColors(d.data.Type)) // Color the text
.text(d => d.data.Item)); // Use 'Item' for the label
return svg.node();
}