6

我已经建立了一个d3带有分组节点的力有向图。我想将组封闭在云状结构中。我怎样才能做到这一点?

图的 Js Fiddle 链接:http: //jsfiddle.net/Cfq9J/5/

我的结果应该类似于此图像:

在此处输入图像描述

4

1 回答 1

11

这是一个棘手的问题,我不完全确定你能以一种表演的方式做到这一点。你可以在这里看到我的静态实现:http: //jsfiddle.net/nrabinowitz/yPfJH/

和这里的动态实现,虽然它很慢而且很紧张:http: //jsfiddle.net/nrabinowitz/9a7yy/

实施注意事项:

  • 这是通过用其组中的所有其他圆圈掩盖每个圆圈来实现的。您可能可以通过碰撞检测来加快速度。

  • 因为每个圆圈都被渲染并用作遮罩,所以大量使用use元素来引用每个节点的圆圈。实际的圆是在一个def元素中定义的,一个用于重用的非渲染定义。运行时,每个节点将呈现如下:

    <g class="node">
        <defs>
            <circle id="circlelanguages" r="46" transform="translate(388,458)" />
        </defs>
        <mask id="masklanguages">
            <!-- show the circle itself, as a base -->
            <use xlink:href="#circlelanguages" 
                fill="white" 
                stroke-width="2"
                stroke="white"></use>
            <!-- now hide all the other circles in the group -->
            <use class="other" xlink:href="#circleenglish" fill="black"></use>
            <use class="other" xlink:href="#circlereligion" fill="black">
            <!-- ... -->
        </mask>
        <!-- now render the circle, with its custom mask -->
        <use xlink:href="#circlelanguages"
            mask="url(#masklanguages)"
            style="fill: #ffffff; stroke: #1f77b4; " />
    </g>
    
  • 我将节点圆圈、链接和文本分别放在不同的g容器中,以适当地分层。

  • 您最好在节点数据中包含一个数据变量,而不是字体大小 - 我必须将该fontSize属性转换为整数才能将其用于圆半径。即使这样,由于文本的宽度与数据值无关,您会得到一些比其下方的圆圈更大的文本。

  • 不知道为什么第一个节点的圆圈没有正确放置在静态版本中 - 它适用于动态版本。神秘。

于 2012-10-16T00:45:09.757 回答