function Pack(
data,
{
path,
id = Array.isArray(data) ? (d) => d.id : null,
parentId = Array.isArray(data) ? (d) => d.parentId : null,
children,
value,
sort = (a, b) => d3.descending(a.value, b.value),
label,
title,
link,
linkTarget = "_blank",
width = 640,
height = 400,
margin = 1,
marginTop = margin,
marginRight = margin,
marginBottom = margin,
marginLeft = margin,
padding = 3,
fill = "#ddd",
fillOpacity,
stroke = "#bbb",
strokeWidth,
strokeOpacity,
colorScale = d3.interpolateGreys
} = {}
) {
// format), and use d3.hierarchy.
const root =
path != null
? d3.stratify().path(path)(data)
: id != null || parentId != null
? d3.stratify().id(id).parentId(parentId)(data)
: d3.hierarchy(data, children);
// Compute the values of internal nodes by aggregating from the leaves.
value == null ? root.count() : root.sum((d) => Math.max(0, value(d)));
//Color scale to distinguish levels
const color = d3.scaleSequential([0, 50], colorScale);
// Compute labels and titles.
const descendants = root.descendants();
const leaves = descendants.filter((d) => !d.children);
leaves.forEach((d, i) => (d.index = i));
const L = label == null ? null : leaves.map((d) => label(d.data, d));
const T = title == null ? null : descendants.map((d) => title(d.data, d));
// Sort the leaves (typically by descending value for a pleasing layout).
if (sort != null) root.sort(sort);
// Compute the layout.
d3
.pack()
.size([width - marginLeft - marginRight, height - marginTop - marginBottom])
.padding(padding)(root);
const svg = d3
.create("svg")
.attr("viewBox", [-marginLeft, -marginTop, width, height])
.attr("width", width)
.attr("height", height)
.attr("style", "max-width: 100%; height: auto; height: intrinsic;")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "middle");
const node = svg
.selectAll("a")
.data(descendants)
.join("a")
.attr("xlink:href", link == null ? null : (d, i) => link(d.data, d))
.attr("target", link == null ? null : linkTarget)
.attr("transform", (d) => `translate(${d.x},${d.y})`);
node
.append("circle")
.attr("fill", (d) => color(d.height))
.attr("fill-opacity", (d) => (d.children ? null : fillOpacity))
.attr("stroke", stroke)
.attr("stroke-width", (d) => (d.children ? strokeWidth : null))
.attr("stroke-opacity", (d) => (d.children ? strokeOpacity : null))
.attr("r", (d) => d.r);
if (T) node.append("title").text((d, i) => T[i]);
if (L) {
// A unique identifier for clip paths (to avoid conflicts).
const uid = `O-${Math.random().toString(16).slice(2)}`;
const leaf = node.filter(
(d) => !d.children && d.r > 10 && L[d.index] != null
);
leaf
.append("clipPath")
.attr("id", (d) => `${uid}-clip-${d.index}`)
.append("circle")
.attr("r", (d) => d.r);
leaf
.append("text")
.attr(
"clip-path",
(d) => `url(${new URL(`#${uid}-clip-${d.index}`, location)})`
)
.selectAll("tspan")
.data((d) => `${L[d.index]}`.split(/\n/g))
.join("tspan")
.attr("x", 0)
.attr("y", (d, i, D) => `${i - D.length / 2 + 0.85}em`)
.attr("fill-opacity", (d, i, D) => (i === D.length - 1 ? 0.7 : null))
.attr("font-size", "12px")
.text((d) => {
return d.split("\n")[0];
});
const groupLabel = svg
.append("g")
.attr("pointer-events", "none")
.attr("text-anchor", "middle")
.selectAll("text")
.data(root.descendants())
.join("text")
.attr("font-size", "12px")
.text((d) => {
return d.height > 0 ? d.data.name : "";
});
groupLabel
.attr("transform", (d) => `translate(${d.x}, ${d.y - d.r})`)
.attr("dy", "-0.25em")
.attr("class", "repo-name")
// .attr("fill", "#fff")
.clone(true)
.lower()
.attr("aria-hidden", "true")
.attr("fill", "black")
.attr("font-size", "12px");
}
return svg.node();
}