<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">function show(){
    const root = d3.hierarchy(data);


    root.x0 = dy / 2;
    root.y0 = 0;
    root.descendants().forEach((d, i) =&gt; {
        d.id = i;
        d._children = d.children;
        //if (d.depth &amp;&amp; d.data.name.length !== 2) d.children = null; //褰撳墠鏉′欢涓嬬殑鑺傜偣涓嶈嚜鍔ㄥ睍绀�
    });


    let svg = d3.select("#chart")     //閫夋嫨鏂囨。涓殑body鍏冪礌
        .append("svg")          //娣诲姞涓€涓猻vg鍏冪礌
        .attr("width", width)       //璁惧畾瀹藉害
        .attr("viewBox", [-margin.left, -margin.top, width, dx])
        .style("font", "15px 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")
        .attr("pointer-events", "all");


    function update(source) {
        const duration = d3.event &amp;&amp; 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 =&gt; {
            if (node.x &lt; left.x) left = node;
            if (node.x &gt; right.x) right = node;
        });


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


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


        // Update the nodes鈥�
        const node = gNode.selectAll("g")
            .data(nodes, d =&gt; d.id);


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


        nodeEnter.append("circle")
            .attr("r", 6)
            .attr("fill", d =&gt; d._children ? "#ccc" : "#fff")
            .attr("stroke", 'steelblue')
            .attr("stroke-width", 2);


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


        //灏嗚妭鐐硅浆鎹㈠埌鏂颁綅缃�
        const nodeUpdate = node.merge(nodeEnter).transition(transition)
            .attr("transform", d =&gt; `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 =&gt; `translate(${source.y},${source.x})`)
            .attr("fill-opacity", 0)
            .attr("stroke-opacity", 0);


        // Update the links鈥�
        const link = gLink.selectAll("path")
            .data(links, d =&gt; d.target.id);


        // Enter any new links at the parent's previous position.
        const linkEnter = link.enter().append("path")
            .attr("d", d =&gt; {
                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 =&gt; {
                const o = {x: source.x, y: source.y};
                return diagonal({source: o, target: o});
            });


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


    update(root);


    return svg.node();
}</pre></body></html>