【问题标题】:How to approach a d3js tree design如何处理 d3js 树设计
【发布时间】:2014-08-25 14:40:29
【问题描述】:

我一般是 d3js 和 javascript 的新手。我正在尝试基于使用 d3js 建模 json 数据来创建交互式 IP 管理概述。我对自己想做的事情有了大致的了解,我认为适合这项工作的工具是使用 d3.layout.tree,它为我提供了所有节点之间的深度和链接。但是考虑到这是我的第一个 d3js 项目,我无法完全掌握如何处理这种设计。看了几个教程,学习了基于d3.layout.tree的例子,比如http://bl.ocks.org/mbostock/1093025。但是曲线太陡了,无法找到解决方案。

这是我根据这个 json 数据大致要完成的工作:http://pastebin.com/dajCKb2P

Initially rendered as:

---------------------------------------------------
| 10.0.0.0/16                                     |
---------------------------------------------------
---------------------------------------------------
| 10.10.0.0/16                                    |
---------------------------------------------------

On-click 10.20.0.0/16 (type supernet):

---------------------------------------------------
| 10.0.0.0/16                                     |
---------------------------------------------------
---------------------------------------------------
| 10.20.0.0/16                                    |
---------------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.200.0/24                               |
    -----------------------------------------------

On-click 10.0.0.0/16: (collapse all other parents)

---------------------------------------------------
| 10.0.0.0/16                                     |
---------------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.1.0/24                                 |
 |  -----------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.100.0/24                               |
 |  -----------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.200.0/24                               |
    -----------------------------------------------
---------------------------------------------------
| 10.20.0.0/16                                    |
---------------------------------------------------

On-click 10.0.1.0/24 (type supernet):

---------------------------------------------------
| 10.0.0.0/16                                     |
---------------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.1.0/24                                 |
    -----------------------------------------------
     |  -------------------------------------------
     -- | 10.0.1.64/26                            |
     |  -------------------------------------------
     |  -------------------------------------------
     -- | 10.0.1.128/26                           |
        -------------------------------------------
    -----------------------------------------------
    | 10.0.100.0/24                               |
    -----------------------------------------------
    -----------------------------------------------
    | 10.0.200.0/24                               |
    -----------------------------------------------
---------------------------------------------------
| 10.20.0.0/16                                    |
---------------------------------------------------

On-click 10.0.100.64/26 (type subnet):

---------------------------------------------------
| 10.0.0.0/16                                     |
---------------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.1.0/24                                 |
 |  -----------------------------------------------
 |   |  -------------------------------------------
 |   -- | 10.0.1.64/26                            |
 |   |  -------------------------------------------
 |   |/                                           |
 |   /                                            |   
 |  /                                             |    
 | /                                              |
 |/                                               |
 /                                                |
---------------------------------------------------
| x x x x x x x x x x x x x x x x x x x x x x x x |   <-- rendered based on hosts array in subnet nodes
| x x x x x x x x x x x x x x x x x x x x x x x x |
| x x x x x x x x x x x x x x x x x x x x x x x x |
| x x x x x x x x x x x x x x x x x x x x x x x x |
---------------------------------------------------
 |   |  -------------------------------------------
 |   -- | 10.0.1.128/26                           |
 |      -------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.100.0/24                               |
 |  -----------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.200.0/24                               |
    -----------------------------------------------
---------------------------------------------------
| 10.20.0.0/16                                    |
---------------------------------------------------

On-click 10.0.1.128/26 (type subnet):
(auto collapse all children under other nodes on same level)

---------------------------------------------------
| 10.0.0.0/16                                     |
---------------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.1.0/24                                 |
 |  -----------------------------------------------
 |   |  -------------------------------------------
 |   -- | 10.0.1.64/26                            |
 |   |  -------------------------------------------
 |   |  -------------------------------------------
 |   -- | 10.0.1.128/26                           |
 |      -------------------------------------------
 |    /                                           |
 |   /                                            |   
 |  /                                             |    
 | /                                              |
 |/                                               |
 /                                                |
---------------------------------------------------
| x x x x x x x x x x x x x x x x x x x x x x x x |   <-- rendered based on hosts array in subnet nodes
| x x x x x x x x x x x x x x x x x x x x x x x x |
| x x x x x x x x x x x x x x x x x x x x x x x x |
| x x x x x x x x x x x x x x x x x x x x x x x x |
---------------------------------------------------       
 |  -----------------------------------------------
 -- | 10.0.100.0/24                               |
 |  -----------------------------------------------
 |  -----------------------------------------------
 -- | 10.0.200.0/24                               |
    -----------------------------------------------
---------------------------------------------------
| 10.20.0.0/16                                    |
---------------------------------------------------

Super and subnet "bars" to be rendered with indication of % of usage based on children or hosts:
(++++ is different gradient color)
---------------------------------------------------
| 10.20.0.0/16+++++++++++60%|                     |
---------------------------------------------------

(我会自己从另一个系统输出 json 文件,所以可以调整)

我已经设置了一个粗略的大纲来访问 json 代码并计算树:

var x_margin = 20,
    y_margin = 20,
    height = window.innerHeight - y_margin,
    width = window.innerWidth - x_margin;

var body = d3.select("body");
var svg = body.append("svg").attr("width", width).attr("height", height);

var tree = d3.layout.tree()
    .size([height, width]);

function supernet_usage(supernet) {
    //returns the amount of space used based on children in given supernet in percentage
}

function subnet_usage(subnet) {
    //returns the amount of space used based on hosts in given subnet in percentage
}

function supernet(supernet) {
    //renders a supernet view of the given subnet
}

function subnet(subnet) {
    //renders a subnet view of the given subnet
}

function calc_hosts(length) {
    //Calculates the number of hosts in an IP subnet based on masklength
    return Math.pow(2,(32 - length))
}

function max_subnet_size (data) {
    // returns largest subnet size in this level of the hierarchie
    return d3.max(data, function(d) { return +d.net_masklength;} );
}

// Toggle children on click.
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);
}

function main() {
    // Main application code
    d3.json("/d3/data.json", function(data) {
        // Executing all actions is done inside the json function call, because of asynchronous handling otherwise
        //console.log(data);
        root = data[0]

        // Compute the new tree layout.
        var nodes = tree.nodes(root),
            links = tree.links(nodes);

        console.log(nodes);
        console.log(links);

    });
}

main();

有人可以大致概述我到达那里需要采取的步骤吗? 像这样的东西真的很有帮助: - 具有执行以下操作的功能: - 在 foreach 循环中调用此函数 - 输入具有所需属性的对象 - 等等

【问题讨论】:

  • 我完全不清楚为什么要在这个设计中使用 D3.js。使用普通的旧 JavaScript 和一些基本的 HTML 和 CSS 来实现可能要简单得多。您为什么不先尝试这种方式以避免 D3 使事情复杂化呢?然后,一旦您的代码和交互工作正常,如果需要,添加 D3.js 作为增强功能会容易得多(尽管我认为您不需要它)。
  • 如果您不想自己编写代码,可以使用大量的 JavaScript 手风琴库。 Here's one 从 2007 年开始仍然可以正常工作。 (查看垂直嵌套示例。)

标签: javascript json d3.js


【解决方案1】:

您的数据可以简单地添加到 d3.js 树形图中。

大致(取决于您将如何显示它);

  • 你加载你的数据
  • 声明您的布局(大小等) 图
  • 将整个 SVG 元素附加到您的页面
  • 声明 树的“根”点
  • 将数据应用到 d3.layout.tree 过程

...可能有点像;

  • 使用 d3.js 魔法分配节点和链接。
  • 调整树的每个分支的深度
  • 附加节点(在本例中为圆圈)
  • 附加链接

使用您的数据的示例是here

所以看起来有点像这样;

(我确实必须删除导致数据“麻烦”的杂散逗号)

您还可以使其具有交互性(单击节点以折叠和增长),例如示例 here(再次使用您的数据);

您可以根据您的数据设置链接或节点的样式,或者在节点上显示更多信息,让您心满意足。

这是基于代码和指令here

【讨论】:

  • 非常感谢 d3noob 的详尽回答。正如您在我最初的帖子中看到的那样,我已经朝着使用树功能的方向前进。感谢您将其置于我的上下文中,这应该为我提供了一个很好的解决框架。您将如何区分超网和子网节点?子网节点还包含一组主机,我想以不同的方式呈现它们。另外,我如何确保 10.0.0.0/16 和 10.20.0.0/16 都被拾取?似乎 10.0.0.0/16 已用作根节点,因此未选择 10.20/16。谢谢。
  • 要以不同的方式渲染子网中的主机,您只需检查“类型”,同时适当地渲染节点和颜色或大小(书中有示例。同时使用 10.0.0.0/16和 10.20.0.0/16 你需要一个节点,它们都是它们的子节点。不太确定你会如何表示它。
猜你喜欢
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 2016-08-07
  • 2012-06-23
  • 2011-10-14
  • 1970-01-01
相关资源
最近更新 更多