简体   繁体   English

在D3中创建圆图?

[英]Creating a circle chart in D3?

I've got a little chart in css: 我在CSS中有一个小图表:

 .circle-chart { position: relative; box-sizing: border-box; width: 400px; height: 400px; } .circle-chart .person { position: absolute; box-sizing: border-box; top: 37.5%; left: 37.5%; width: 25%; height: 25%; border-radius: 10000px 10000px 10000px 10000px; background: white; border: 3px solid black; text-align: center; } .circle-chart .parentA, .circle-chart .parentB { position: absolute; box-sizing: border-box; top: 25%; left: 25%; height: 25%; width: 50%; border-radius: 10000px 10000px 0 0; border-top: 4px solid black; border-bottom: 2px solid black; border-left: 4px solid black; border-right: 4px solid black; background: white; } .circle-chart .parentB { top: 50%; left: 25%; transform: rotate(180deg); } .circle-chart .grandParentA, .circle-chart .grandParentB, .circle-chart .grandParentC, .circle-chart .grandParentD { position: absolute; box-sizing: border-box; top: 12.5%; left: 12.6%; height: 37.5%; width: 37.5%; border-radius: 10000px 0 0 0; border-top: 4px solid black; border-bottom: 2px solid black; border-left: 4px solid black; border-right: 2px solid black; } .circle-chart .grandParentB { left: 50%; transform: rotate(90deg); } .circle-chart .grandParentC { top: 50%; left: 50%; transform: rotate(180deg); } .circle-chart .grandParentD { top: 50%; transform: rotate(270deg); } 
 <div class="circle-chart"> <div class="grandParentA"></div> <div class="grandParentB"></div> <div class="grandParentC"></div> <div class="grandParentD"></div> <div class="parentA"></div> <div class="parentB"></div> <div class="person"></div> </div> 

And I'd like to make the chart go back another two or three rings. 我想让图表再回溯两三声。 But that got me thinkings, is there anyway to do this in D3? 但这使我产生了想法,在D3中是否有这样做的方法? D3 would probably be able handle the situation where there would be 2+ parents as well, I hope. 我希望D3可能也可以解决2个以上父母的情况。

Here is my attempt: 这是我的尝试:

First define the data: 首先定义数据:

Here first array defines the Great Grand Parent. 在这里,第一个数组定义了曾祖父。

Second array defines the Grand Parent. 第二个数组定义了“祖父母”。

Third array defines the Parent. 第三个数组定义了父级。

var my = {
  name: "me",
  parentArray: [

    [{
      name: "F",
      relation: "motherC"
    }, {
      name: "G",
      relation: "fatherC"
    }, {
      name: "H",
      relation: "motherD"
    }, {
      name: "I",
      relation: "fatherD"
    }, {
      name: "J",
      relation: "fatherZ"
    }, {
      name: "K",
      relation: "motherZ"
    }, {
      name: "L",
      relation: "fatherE"
    }, {
      name: "M",
      relation: "motherE"
    }],
    [{
      name: "C",
      relation: "motherA"
    }, {
      name: "D",
      relation: "fatherA"
    }, {
      name: "Z",
      relation: "motherB"
    }, {
      name: "E",
      relation: "fatherB"
    }],
    [{
      name: "A",
      relation: "mother"
    }, {
      name: "B",
      relation: "father"
    }]
  ]
};

Then depending on the array make donut chart: 然后根据数组制作甜甜圈图:

my.parentArray.forEach(function(d, i) {

  var diff = i * 20 + 20 ;
  var arc = d3.svg.arc()
    .innerRadius(radius - diff)
    .outerRadius(radius - (diff - 20));

  var path = svg.selectAll(".grand" + i)
    .data(pie(d))
    .enter().append("path")
    .attr("class", "grand" + i)
    .attr("fill", function(d, i) {
      return color(i);
    })
    .attr("d", arc);

});

My Assumption: 我的假设:

Each person will have at least (1 father + 1 mother). 每个人至少要有 (1个父亲+ 1个母亲)。

You can also have a person with (2 father + 2 mother) but definitely not (2 father + 3 mother) :) 您也可以拥有一个(2个父亲+ 2个母亲)的人,但绝对不能(2个父亲+ 3个母亲)的人:)

Working code here 这里的工作代码

Working code name: me having 2 mother 2 father here 工作代号名称:我在这里有2个母亲2个父亲

Let me know your thoughts :) 让我知道你的想法 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM