d3.js 以 HTML 为节点的二叉树

发布于 2024-12-16 21:41:49 字数 405 浏览 0 评论 0原文

我一直在使用 d3.js,它非常酷。我需要一个足够灵活的二叉树来创建新的子节点。

我正在考虑在包含所有 HTML 的

后面添加一个 元素(用于行)。我已经设法让二叉树看起来正确,即使当您单击一个节点时,它也会创建另一个节点,但仅来自绑定到 元素下的圆圈的事件。我如何获得类似
的东西来触发在父节点下添加子节点的事件?

另外,由于 html 与 svg 画布是分开的,因此窗口大小调整如何处理?

I've been playing around with d3.js and it's pretty cool. I need to have a binary tree that's flexible enough to create new child nodes.

I was thinking of having an <svg> element (for the lines) behind a <div> that would contain all of the HTML. I have managed to get the binary tree to look right and even when you click on a node it creates another node but ONLY from the event thats bound to a circle under the <svg> element. How would I get something like a <div> to trigger an event that adds a child node under a parent?

Also how would window resizing work with this since the html is separate from the svg canvas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗地喜欢 2024-12-23 21:41:49

您可以将事件处理程序附加到 html,就像附加到 svg 元素一样

// Enter any new nodes at the parent's previous position.
node.enter()

    // append an html div
    .append("div")

    // event handlers
    .on("click", function(d) {
        // your code to add a new node
    })

(上次我检查 d3 的事件处理程序非常基本,不支持自定义事件)

处理窗口大小调整可能很棘手,因为您必须设置画布和生成树之前的 html 容器尺寸。

处理这个问题的一种快速而肮脏的方法是为 html 容器设置非常大的尺寸

vis = d3.select("#chart-inner").append("svg:svg").attr("id", "svg-container").attr("width", 40000).attr("height", 20000).append("svg:g").attr("transform", "translate(0,100)");

,然后用容器 div 将其全部包裹起来,

<div id="chart" style="width:100%; overflow:hidden">
     <div id="chart-inner">

这并不是一个真正理想的解决方案。如果你想正确地做到这一点,你必须根据初始 html 元素大小计算出树的深度和宽度(这将涉及大量“遍历”数据来计算)。

You would attach the event handlers to the html the same way you would to an svg element

// Enter any new nodes at the parent's previous position.
node.enter()

    // append an html div
    .append("div")

    // event handlers
    .on("click", function(d) {
        // your code to add a new node
    })

(Last time I checked d3's event handlers are pretty basic and don't support custom events)

Handling window resizing can be tricky as you have to set the canvas and html container dimensions before generating the tree.

A quick and dirty way of handling this would be to set very large sizes for the html container

vis = d3.select("#chart-inner").append("svg:svg").attr("id", "svg-container").attr("width", 40000).attr("height", 20000).append("svg:g").attr("transform", "translate(0,100)");

Then wrap it all up with a container div

<div id="chart" style="width:100%; overflow:hidden">
     <div id="chart-inner">

It's not really an ideal solution. If you want to do it properly, you'll have to work out the depth and width of the tree based on the initial html element size (which would involve a lot of 'walking' the data to work out).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文