如何将 D3 树布局的方向更改 90 度 [英] How to change orientation of a D3 tree layout by 90 degrees

查看:24
本文介绍了如何将 D3 树布局的方向更改 90 度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始参与网络可视化,所以我完全是新手.我的目标是显示一个家谱,其中根节点将有多个父节点和子节点.在寻找解决方案时,我发现了这个例子:http://bl.ocks.org/jdarling/2503502这很棒,因为它似乎具有我需要的功能.但是,我想改变方向(从上到下).我尝试使用以下示例:http://bl.ocks.org/mbostock/3184089 但失败了.

I just started getting involved in web visualizations, so I'm totally novice. My goal is to display a family tree where a root node would have both multiple parents and children. While looking for a solution I found this example: http://bl.ocks.org/jdarling/2503502 It's great because it seems to have the feature I need. However, I would like to alter the orientation (top-to-bottom). I tried to do so using this example: http://bl.ocks.org/mbostock/3184089 but failed.

我的代码:

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

var diagonal = d3.svg.diagonal()
    .projection(function(d) {
        return [d.x, d.y];
    });

var elbow = function (d, i){
    var source = calcTop(d.source);
    var target = calcTop(d.target);
    var hx = (target.x-source.x)/2;
    if(d.isRight)
        hx = -hx;
    return  "M" + source.x + "," + source.y
          + "H" + (source.x+hx)
          + "V" + target.y + "H" + target.x;
};

var connector = elbow;

var calcTop = function(d){
    var top = d.x;
    if(!d.isRight){
        top = d.x-halfHeight;
        top = halfHeight - top;
    }
    return {x : top, y : d.y};
};

var vis = d3.select("#chart")
    .append("svg")
    .attr("height", height + margin.top + margin.bottom)
    .attr("width", width + margin.right + margin.left)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.json("tree.json", function(json) {
    root = json;
    root.x0 = height / 2;
    root.y0 = width / 2;
    var t1 = d3.layout.tree()
        .size([halfHeight, width])
        .children(function(d){
            return d.winners;
        });
    var t2 = d3.layout.tree()
        .size([halfHeight, width])
        .children(function(d){
            return d.challengers;
        });
    t1.nodes(root);
    t2.nodes(root);

    var rebuildChildren = function(node){
        node.children = getChildren(node);
        if(node.children)
            node.children.forEach(rebuildChildren);
    }
    rebuildChildren(root);
    root.isRight = false;
    update(root);
});

var toArray = function(item, arr){
    arr = arr || [];
    var i = 0, l = item.children?item.children.length:0;
    arr.push(item);
    for(; i < l; i++){
        toArray(item.children[i], arr);
    }
    return arr;
};

function update(source) {
// Compute the new tree layout.
var nodes = toArray(source);

// Normalize for fixed-depth.
nodes.forEach(function(d) { d.x = d.depth * 180 + halfHeight; });

// Update the nodes…
var node = vis.selectAll("g.node")
    .data(nodes, function(d) { return d.id || (d.id = ++i); });

// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
    .attr("class", "node")
    .attr("transform", function(d) {
        return "translate(" + source.x0 + "," + source.y0 + ")";
    })
    .on("click", click);

nodeEnter.append("circle")
    .attr("r", 1e-6)
    .style("fill", function(d) {
        return d._children ? "lightsteelblue" : "#fff";
    });

nodeEnter.append("text")
    .attr("dy", function(d) { return d.isRight?14:-8;})
    .attr("text-anchor", "middle")
    .text(function(d) { return d.name; })
    .style("fill-opacity", 1e-6);

// Transition nodes to their new position.
var nodeUpdate = node.transition()
    .duration(duration)
    .attr("transform", function(d) {
        p = calcTop(d);
        return "translate(" + p.x + "," + p.y + ")";
});

nodeUpdate.select("circle")
    .attr("r", 4.5)
    .style("fill", function(d) {
        return d._children ? "lightsteelblue" : "#fff";
    });

nodeUpdate.select("text")
    .style("fill-opacity", 1);

// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
    .duration(duration)
    .attr("transform", function(d) {
         p = calcTop(d.parent||source);
         return "translate(" + p.x + "," + p.y + ")";
    })
    .remove();

nodeExit.select("circle")
    .attr("r", 1e-6);

nodeExit.select("text")
    .style("fill-opacity", 1e-6);

// Update the links...
var link = vis.selectAll("path.link")
    .data(tree.links(nodes), function(d) { return d.target.id; });

// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
    .attr("class", "link")
    .attr("d", function(d) {
        var o = {x: source.x0, y: source.y0};
        return connector({source: o, target: o});
    });

// Transition links to their new position.
link.transition()
    .duration(duration)
    .attr("d", connector);

// Transition exiting nodes to the parent's new position.
link.exit()
    .transition()
    .duration(duration)
    .attr("d", function(d) {
        var o = calcTop(d.source||source);
        if(d.source.isRight)
            o.x -= halfHeight - (d.target.x - d.source.x);
        else
            o.x += halfHeight - (d.target.x - d.source.x);
        return connector({source: o, target: o});
    })
    .remove();

// Stash the old positions for transition.
nodes.forEach(function(d) {
    var p = calcTop(d);
    d.x0 = p.x;
    d.y0 = p.y;
});

// 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(source);

}}

非常感谢您的帮助!

推荐答案

您正在查看的示例实际上已经翻转了 - 这可能会引起您的一些困惑.D3 中的树自然是自顶向下的树,代码做了很多 x-y 翻转使树侧向.

The example you're looking at is actually already flipped - this might be causing you some confusion. Trees in D3 are naturally top-down trees, and the code does a lot of x-y flipping to make the tree sideways.

变化

  var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform", function(d) { p = calcLeft(d); return "translate(" + p.y + "," + p.x + ")"; })
      ;

  var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform", function(d) { p = calcLeft(d); return "translate(" + p.x + "," + p.y + ")"; })
      ;

将使节点显示在正确的位置.对 update() 内的任何交换 x-y 坐标实例进行类似的更改可以修复大部分定位问题.最后一件事是肘部函数/变量/随便你怎么称呼它,其中

will get the nodes displaying in the right position. Doing a similar change with any instance of swapped x-y coordinates inside update() fixes most of the positioning issues. One last thing is the elbow function/variable/whatever you want to call it, where

  return "M" + source.y + "," + source.x
         + "H" + (source.y+hy)
         + "V" + target.x + "H" + target.y;

应该改为

  return "M" + source.x + "," + source.y
         + "V" + (source.y+hy)
         + "H" + target.x + "V" + target.y;

这会将连接器形状从水平垂直水平更改为垂直水平垂直.请注意,这是一个原始的 SVG 行,根本不是 d3.我所做的更改(加上交换宽度和高度,以及更改 AJAX JSON 请求以对数据进行硬编码 - AJAX 很难在小提琴中工作)都在 http://jsfiddle.net/Zj3th/2/.

This changes the connector shape from horizontal vertical horizontal to vertical horizontal vertical. Note that this is a raw SVG line, not d3 at all. The changes I made (plus swapping width and height, and changing the AJAX JSON request to hardcoding the data - AJAX is hard to get working in fiddle) are all at http://jsfiddle.net/Zj3th/2/.

如果你没有使用 d3 和 SVG 的经验,我肯定会看看并完全理解一个简单的例子,比如 http://blog.pixelingene.com/2011/07/building-a-tree-diagram-in-d3-js/ 在你之前进一步修改代码.

If you have no experience with d3 and SVG, I would definitely take a look and fully understand a simple example like http://blog.pixelingene.com/2011/07/building-a-tree-diagram-in-d3-js/ before you go further in modifying the code.

这篇关于如何将 D3 树布局的方向更改 90 度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆