Published
Edited
Mar 23, 2021
Insert cell
Insert cell
chart = {
const root = d3.hierarchy(data);

root.x0 = dy / 2;
root.y0 = 0;
root.descendants().forEach((d, i) => {
d.id = i;
d._children = d.children;
if (d.depth && d.data.name.length !== 7) d.children = null;
});

const svg = d3.create("svg")
.attr("width", width)
.attr("height", dx)
.attr("viewBox", [-margin.left, -margin.top, width, dx])
.style("font", "10px sans-serif")
.style("user-select", "none");

const gLink = svg.append("g")
.attr("fill", "none")
.attr("stroke", "#555")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5);

const gNode = svg.append("g")
.attr("cursor", "pointer");

function update(source) {
const duration = d3.event && d3.event.altKey ? 2500 : 250;
const nodes = root.descendants().reverse();
const links = root.links();

// Compute the new tree layout.
tree(root);

let left = root;
let right = root;
root.eachBefore(node => {
if (node.x < left.x) left = node;
if (node.x > right.x) right = node;
});

const height = right.x - left.x + margin.top + margin.bottom;

const transition = svg.transition()
.duration(duration)
.attr("height", height)
.attr("viewBox", [-margin.left, left.x - margin.top, width, height])
.tween("resize", window.ResizeObserver ? null : () => () => svg.dispatch("toggle"));

// Update the nodes…
const node = gNode.selectAll("g")
.data(nodes, d => d.id);

// Enter any new nodes at the parent's previous position.
const nodeEnter = node.enter().append("g")
.attr("transform", d => `translate(${source.y0},${source.x0})`)
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0)
.on("click", d => {
d.children = d.children ? null : d._children;
update(d);
});

nodeEnter.append("circle")
.attr("r", 2.5)
.attr("fill", d => d._children ? "#555" : "#999");

nodeEnter.append("text")
.attr("dy", "0.31em")
.attr("x", d => d._children ? -6 : 6)
.attr("text-anchor", d => d._children ? "end" : "start")
.text(d => d.data.label)
.clone(true).lower()
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.attr("stroke", "white");

// Transition nodes to their new position.
const nodeUpdate = node.merge(nodeEnter).transition(transition)
.attr("transform", d => `translate(${d.y},${d.x})`)
.attr("fill-opacity", 1)
.attr("stroke-opacity", 1);

// Transition exiting nodes to the parent's new position.
const nodeExit = node.exit().transition(transition).remove()
.attr("transform", d => `translate(${source.y},${source.x})`)
.attr("fill-opacity", 0)
.attr("stroke-opacity", 0);

// Update the links…
const link = gLink.selectAll("path")
.data(links, d => d.target.id);

// Enter any new links at the parent's previous position.
const linkEnter = link.enter().append("path")
.attr("d", d => {
const o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});

// Transition links to their new position.
link.merge(linkEnter).transition(transition)
.attr("d", diagonal);

// Transition exiting nodes to the parent's new position.
link.exit().transition(transition).remove()
.attr("d", d => {
const o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
});

// Stash the old positions for transition.
root.eachBefore(d => {
d.x0 = d.x;
d.y0 = d.y;
});
}

update(root);

return svg.node();
}
Insert cell
diagonal = d3.linkHorizontal().x(d => d.y).y(d => d.x)
Insert cell
tree = d3.tree().nodeSize([dx, dy])
Insert cell
data = ({"name":"ContainerFactory","label":"Container","path":"src/components/container.js","children":[{"name":"KeplerGlFactory","label":"KeplerGl","path":"src/components/kepler-gl.js","children":[{"name":"BottomWidgetFactory","label":"BottomWidget","path":"src/components/bottom-widget.js","children":[{"name":"TimeWidgetFactory","label":"TimeWidget","path":"","children":[],"isRoot":false}],"isRoot":false},{"name":"MapContainerFactory","label":"MapContainer","path":"src/components/map-container.js","children":[{"name":"MapPopoverFactory","label":"MapPopover","path":"","children":[],"isRoot":false},{"name":"MapControlFactory","label":"MapControl","path":"","children":[],"isRoot":false}],"isRoot":false},{"name":"ModalContainerFactory","label":"ModalContainer","path":"src/components/modal-container.js","children":[{"name":"DeleteDatasetModalFactory","label":"DeleteDatasetModal","path":"","children":[],"isRoot":false},{"name":"DataTableModalFactory","label":"DataTableModal","path":"","children":[],"isRoot":false},{"name":"LoadDataModalFactory","label":"LoadDataModal","path":"","children":[],"isRoot":false},{"name":"ExportImageModalFactory","label":"ExportImageModal","path":"","children":[],"isRoot":false},{"name":"ExportDataModalFactory","label":"ExportDataModal","path":"","children":[],"isRoot":false},{"name":"ExportConfigModalFactory","label":"ExportConfigModal","path":"","children":[],"isRoot":false},{"name":"AddMapStyleModalFactory","label":"AddMapStyleModal","path":"","children":[],"isRoot":false}],"isRoot":false},{"name":"SidePanelFactory","label":"SidePanel","path":"src/components/side-panel.js","children":[{"name":"SidebarFactory","label":"Sidebar","path":"src/components/side-panel/side-bar.js","children":[{"name":"CollapseButtonFactory","label":"CollapseButton","path":"","children":[],"isRoot":false}],"isRoot":false},{"name":"PanelHeaderFactory","label":"PanelHeader","path":"","children":[],"isRoot":false},{"name":"PanelToggleFactory","label":"PanelToggle","path":"","children":[],"isRoot":false},{"name":"PanelTitleFactory","label":"PanelTitle","path":"","children":[],"isRoot":false},{"name":"LayerManagerFactory","label":"LayerManager","path":"src/components/side-panel/layer-manager.js","children":[{"name":"AddDataButtonFactory","label":"AddDataButton","path":"","children":[],"isRoot":false},{"name":"LayerPanelFactory","label":"LayerPanel","path":"","children":[],"isRoot":false},{"name":"SourceDataCatalogFactory","label":"SourceDataCatalog","path":"","children":[],"isRoot":false}],"isRoot":false},{"name":"FilterManagerFactory","label":"FilterManager","path":"src/components/side-panel/filter-manager.js","children":[{"name":"SourceDataCatalogFactory","label":"SourceDataCatalog","path":"","children":[],"isRoot":false},{"name":"FilterPanelFactory","label":"FilterPanel","path":"","children":[],"isRoot":false}],"isRoot":false},{"name":"InteractionManagerFactory","label":"InteractionManager","path":"src/components/side-panel/interaction-manager.js","children":[{"name":"InteractionPanelFactory","label":"InteractionPanel","path":"src/components/side-panel/interaction-panel/interaction-panel.js","children":[{"name":"TooltipConfigFactory","label":"TooltipConfig","path":"","children":[],"isRoot":false},{"name":"BrushConfigFactory","label":"BrushConfig","path":"","children":[],"isRoot":false}],"isRoot":false}],"isRoot":false},{"name":"MapManagerFactory","label":"MapManager","path":"src/components/side-panel/map-manager.js","children":[{"name":"MapStyleSelectorFactory","label":"MapStyleSelector","path":"","children":[],"isRoot":false},{"name":"LayerGroupSelectorFactory","label":"LayerGroupSelector","path":"","children":[],"isRoot":false}],"isRoot":false}],"isRoot":false},{"name":"PlotContainerFactory","label":"PlotContainer","path":"src/components/plot-container.js","children":[{"name":"MapContainerFactory","label":"MapContainer","path":"src/components/map-container.js","children":[{"name":"MapPopoverFactory","label":"MapPopover","path":"","children":[],"isRoot":false},{"name":"MapControlFactory","label":"MapControl","path":"","children":[],"isRoot":false}],"isRoot":false}],"isRoot":false},{"name":"NotificationPanelFactory","label":"NotificationPanel","path":"src/components/notification-panel.js","children":[{"name":"NotificationItemFactory","label":"NotificationItem","path":"","children":[],"isRoot":false}],"isRoot":false}],"isRoot":false}],"isRoot":true})

Insert cell
dx = 10
Insert cell
dy = width / 6
Insert cell
margin = ({top: 10, right: 120, bottom: 10, left: 40})
Insert cell
d3 = require("d3@5")
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