简体   繁体   English

扩展d3树布局以在最终节点处提供HTML框

[英]Extending d3 tree layout to provide HTML box at final node

I would like to Extend d3 tree layout to provide hyperlinks and text for each final node 我想扩展d3树的布局以为每个最终节点提供超链接和文本

http://mbostock.github.io/d3/talk/20111018/tree.html http://mbostock.github.io/d3/talk/20111018/tree.html

For example if you go to 例如,如果您转到

flare> analytics > ckuster > AgglomerativeClustr

I would like under the text AgglomerativeCluster I would like to have a textbox with the ability to have custom HTML. 我想在文本AgglomerativeCluster下创建一个具有自定义HTML功能的文本框。

How would I do this? 我该怎么做?

There are several ways to achieve what you want, but I believe the solution below is both simple and efficient. 有几种方法可以实现您想要的,但是我相信下面的解决方案既简单又有效。

I'll start with an example that is similar to yours, and also does not have any hyperlinks: 我将从与您的示例相似的示例开始,并且也没有任何超链接:

link to jsfiddle - starting point , 链接到jsfiddle起点

and will walk you through all necessary but simple modifications. 并引导您完成所有必要但简单的修改。

First, let's add field "url" for nodes that we want to have labels as hyperlinks: 首先,让我们为想要将标签作为超链接的节点添加字段“ url”:

        {
            "children": [{
            "name": "AgglomerativeCluster",
            "size": 3938,
            "url": "http://www.example.com/"    
        }, {
            "name": "CommunityStructure",
            "size": 3812,
            "url": "http://www.example.com/"    

        }, {
            "name": "HierarchicalCluster",
            "size": 6714,
            "url": "http://www.example.com/"    

        }, {
            "name": "MergeEdge",
            "size": 743,
            "url": "http://www.example.com/"    
        }

Now, let's write code that selects all labels whose node data contain anything in field "url", and for each such label adds appropriate clock handler that will open correspondent url: (it also sets different cursor pointer for mouseovers so that user knows the label is actually a hyperlink) 现在,让我们编写代码,以选择节点数据在“ url”字段中包含任何内容的所有标签,并为每个此类标签添加适当的时钟处理程序,以打开相应的url :(它还为鼠标悬停设置了不同的光标指针,以便用户知道该标签实际上是一个超链接)

d3.selectAll(".node text")
    .filter(function(d){ return d.url; })
    .style("cursor", "pointer")
    .on("click", function(d){
        document.location.href = d.url;
    });

That's it! 而已!

link to jsfiddle - finished example 链接到jsfiddle-完成的示例

NOTE: by the time I finished this, my good buddy @VividD had already supplied an answer. 注意:在完成此操作时,我的好友@VividD已经提供了答案。 But because the OP expressed a desire for custom HTML, this may still be relevant. 但是因为OP表示希望使用自定义HTML,所以这可能仍然很重要。

Basically one wants to mix HTML with SVG in one form or another, a common request. 基本上,人们希望以一种或另一种形式(一种常见的请求)将HTML与SVG混合。 However, chances are you will not find a ready-made implementation of this since it takes time and has a fair amount of layout considerations to make it work (see the setting of x,y attributes in the fiddle I linked below - hardcoded for simplicity). 但是,您可能会找不到现成的实现,因为它需要时间并且要进行大量布局注意事项(请参见下面我链接的小提琴中的x,y属性设置-为简单起见,采用硬编码)。 Having said that, here is my attempt at helping you. 话虽如此,这是我为您提供的帮助。

The most common solution is the use of an SVG:foreignObject . 最常见的解决方案是使用SVG:foreignObject But be aware that IE does not support it. 但是请注意,IE不支持它。 Here is a gist by the great Mike that showcases a very simple example. 这是伟大的迈克的要旨 ,展示了一个非常简单的例子。 I took the liberty to extend it and create a FIDDLE with a slightly more elaborate example, a form with a textarea. 我自由地扩展了它,并创建了一个FIDDLE,其中包含一个稍微复杂一些的示例,即带有文本区域的表单。 I believe this should be enough to generate some ideas and get you going. 我相信这足以产生一些想法并使您前进。

svg.append("foreignObject")
    .attr("x", 40)
    .attr("y", 40)
    .attr("width", 480)
    .attr("height", 240)
  .append("xhtml:body")
    ...

You might want to consider generating a fiddle of your own (based on this one) if you don't solve your issue completely. 如果您不能完全解决问题,则可能要考虑自己动手(基于此)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM