d3所有分支相互垂直垂直树状图. r2d3闪亮 [英] d3 all branches on top of each other vertical dendogram. r2d3 shiny

查看:222
本文介绍了d3所有分支相互垂直垂直树状图. r2d3闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个垂直的树状图,但是所有分支都彼此重叠,所以我不明白为什么.

I want to make a vertical dendogram, however all branches are on top of each other, and I don't understand why.

在此处输入图片描述

这是基于耀斑数据集的树状图... 该代码旨在进入一个闪亮的应用程序,这就是为什么我使用r2d3的原因.不知道这是否会带来很大的不同,因为.js无论如何都不会显示正确的树.

this is a dendogram based on the flare dataset... The code is meant to go into a shiny app, and that is why i use r2d3. Not sure if this makes a huge difference as the .js doesn't show the proper tree anyway.

我现在使用的代码是:

// !preview r2d3 data = read.csv("flare.csv"), d3_version = 4
// Based on: https://bl.ocks.org/mbostock/4063570

var g = svg.append("g").attr("transform", "translate(300,10)");

var tree = d3.tree()
  .size([0,200])
  //.size([height, width - 160]);
  .separation(function separation(a, b) {return (a.parent == b.parent ? 1 : 1) ;});

var stratify = d3.stratify()
    .parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });

r2d3.onRender(function(data, svg, w, h, options) {
  var root = stratify(data)
      .sort(function(a, b) { return (a.height - b.height) || a.id.localeCompare(b.id); });

  root = tree(root);

  var y_spanning = 0.4;

  var link = g.selectAll(".link")
  .data(root.descendants().slice(1))
    .enter().append("path")
      .attr("class", "link")
      .attr("d", function(d) {
        return "M" + (d.x) + "," + (d.y) / y_spanning
        //return "M" + d.y + "," + d.x
        + "C" + (d.parent.x ) + "," + (d.y )  / y_spanning
        //+ "C" + (d.parent.y + 100) + "," + d.x
        + " " + (d.parent.x ) + "," + (d.parent.y ) / y_spanning
        //+ " " + (d.parent.y + 100) + "," + d.parent.x
        + " " + (d.parent.x ) + "," + d.parent.y / y_spanning;
        //+ " " + d.parent.y + "," + d.parent.x;
      });


//  link.append("text")
//    .attr("x", function(d) {return d.x; })
//    .attr("y", function(d) {return d.y; })
//    .attr("font-size", 2 + 4 * height / 500)
//    .attr("text-anchor", "middle")
//    .text(function(d) { return d.id.substring(d.id.lastIndexOf(".") +     1);
//    });

 // var linkText = g.selectAll(".link")
 //   .append("text")
 //   .attr("class", "link-label")
 //   //.attr("dy", ".35em")
 //   .attr("text-anchor", "middle")
 //   .text(function(d) {
 //       return d.parentId;
 //   });

  var node = g.selectAll(".node")
      .data(root.descendants())
    .enter().append("g")
      .attr("class", function(d) { return "node" + (d.children ? " node--    internal" : " node--leaf"); })
      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y / y_spanning + ")"; })

  node.append("circle")
      .attr("r", 2.5);

  node.append("text")
      .attr("dx", 15)
      .attr("dy", 4)
      .attr("font-size", 2 + 4 * height / 500)
      .attr("x", function(d) { return d.children ? -20 : 20; })
      //.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
      .text(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1); });

});

推荐答案

实际上,通过切换x和y,我从一棵水平树开始到一棵垂直树.

I actually started from a horizontal to a vertical tree, by switching the x and y.

我发现了问题:这是关于正确排序数据的问题.父母和孩子应该保持正确的顺序,否则将无法正常工作.

I found the problem: It was about sorting the data properly. parents and childs should be in the correct order, otherwise it doesn't work properly.

这篇关于d3所有分支相互垂直垂直树状图. r2d3闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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