将D3树节点位置绑定到X轴 [英] Binding D3 Tree Node Position to X Axis

查看:48
本文介绍了将D3树节点位置绑定到X轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从头开始在D3中创建家谱,但我无法将图形的节点分配给相应的出生年份.我已将轴放置在树形图的下方,但希望看到具有不同链接长度的节点.

I am playing with creating a family tree in D3 from scratch, and I am having trouble getting the nodes of my graph to be assigned to the corresponding birth year. I have the axis placed underneath the tree map, but would like to see the nodes having different link lengths.

WIP小提琴

JS:

var familyTree =
  {'name':'Child', 'born':1990, 'parents': [
    {'name':'Father', 'born':1970, 'parents': [
      {'name':'GrandMother1', 'born':1950},
      {'name':'GrandFather1', 'born':1940}]},
    {'name':'Mother', 'born':1960, 'parents':[
      {'name':'GrandFather2', 'born':1930},
      {'name':'GrandMother2', 'born':1945}]}
  ]};


var margin = {top:20, right:90, bottom:30, left:90},
  width = 900 - margin.left - margin.right,
  height = 400 - margin.top - margin.bottom;

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

var nodes = d3.hierarchy(familyTree, function(d){
  return d.parents;
});

nodes = treemap(nodes);

var svg = d3.select('body').append('svg')
  .attr('width', width + margin.left + margin.right)
  .attr('height', height + margin.top + margin.bottom),
  g = svg.append('g')
    .attr('transform','translate(' + margin.left + ',' + margin.top + ')');


var formatNumber = d3.format('');
var x = d3.scaleLinear()
  .domain([2017,1900])
  .range([0,width]);
var xAxis = d3.axisBottom(x)
  .ticks(25)
  .tickFormat(formatNumber);
g.append('g')
  .attr('transform','translate(0,' + height + ')')
  .call(customXAxis);
function customXAxis(g) {
  g.call(xAxis);
  g.select('.domain').remove();
};


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

var node = g.selectAll('.node')
  .data(nodes.descendants())
  .enter().append('g')
  .attr('class',function(d){
    return 'node' +
      (d.parents ? ' node--internal' : ' node--leaf');})
  .attr('transform',function(d) {
    return 'translate(' + d.y  + ',' + d.x + ')';});

node.append('circle')
  .attr('r',10);

node.append('text')
  .attr('dy','.35em')
  .attr('x',function(d){return d.parents ? -13 : 13;})
  .style('text-anchor',function(d){
    return d.parents ? 'end' : 'start';})
  .text(function(d){return d.data.name;});

有人可以告诉我如何获得节点X的位置来表示出生年份吗?

Can someone show me how to get the node X position to represent the birth year?

推荐答案

我能够通过调整链接和节点追加以使用born属性而不是y属性来解决此问题.工作小提琴

I was able to solve the issue by adjusting the link and node appends to use the born attribute rather than the y attribute. Working Fiddle

部分已更新:

var link = g.selectAll('.link')
  .data(nodes.descendants().slice(1))
  .enter().append('path')
  .attr('class','link')
  .attr('d', function(d) {
    return 'M' + x(d.data.born) + ',' + d.x
      + 'C' + (x(d.data.born) + x(d.parent.data.born)) / 2 + ',' + d.x
      + ' ' + (x(d.data.born) + x(d.parent.data.born)) / 2 + ',' + d.parent.x
      + ' ' + x(d.parent.data.born) + ',' + d.parent.x;
  });

var node = g.selectAll('.node')
  .data(nodes.descendants())
  .enter().append('g')
  .attr('class',function(d){
    return 'node' +
      (d.parents ? ' node--internal' : ' node--leaf');})
  .attr('transform',function(d) {
    return 'translate(' + x(d.data.born)  + ',' + d.x + ')';});

这篇关于将D3树节点位置绑定到X轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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