简体   繁体   English

向径向 D3 图表添加多个组

[英]Adding multiple groups to radial D3 chart

Sorry if the title is misleading.抱歉,如果标题具有误导性。 Basically I have the following d3 chart:基本上我有以下 d3 图表:

d3 径向条形图

Which is based on an array of 5 elements, each one containing 3 separate values.它基于一个由 5 个元素组成的数组,每个元素包含 3 个单独的值。 As you can see, the radial chart is grouped into 5 sections (with a gap) and then each section contains the 3 separate values as bar charts.如您所见,径向图分为 5 个部分(有一个间隙),然后每个部分包含 3 个单独的值作为条形图。

Here is a js fiddle: https://jsfiddle.net/kpu5o7v2/ And my data:这是一个 js 小提琴: https://jsfiddle.net/kpu5o7v2/我的数据:

[
        {
            index: 0,
            name: "John",
            red: 35,
            green: 16,
            blue: 56
        },
        {
            index: 1,
            name: "Spencer",
            red: 12,
            green: 34,
            blue: 8
        },
        {
            index: 2,
            name: "Alice",
            red: 6,
            green: 6,
            blue: 70
        },
        {
            index: 3,
            name: "Cat",
            red: 12,
            green: 80,
            blue: 1
        },
        {
            index: 4,
            name: "Dave",
            red: 80,
            green: 1,
            blue: 2
        }
    ]

What I want to know, is how can I also add sections to correspond to the new_data.name value?我想知道的是,我怎样才能添加与 new_data.name 值相对应的部分? ie. IE。 the wider groups.更广泛的群体。 So I can then style them indivdually So, for example, for "John":所以我可以单独设置它们的样式所以,例如,对于“John”:

  • start angle is 0起始角度为 0
  • end angle is the same end angle as John's blue section端角与约翰的蓝色部分相同
  • radius is some number > largest subsection.半径是某个数字 > 最大的小节。

Below is an example (if you were to give this group a stroke):下面是一个例子(如果你要给这个组一个中风):

在此处输入图像描述

I figured out the answer to this:我想出了这个问题的答案:

  • We need to add a new g to the SVG with the same new_data again我们需要再次使用相同的 new_data 向 SVG 添加一个新的 g
  • Create a new arc object for the names ("John", "Spencer", etc) instead of the keys ("red", "green", "blue")为名称(“John”、“Spencer”等)而不是键(“red”、“green”、“blue”)创建一个新的 arc object
  • Radius is y(ymax) where ymax is the largest value in all the data半径是 y(ymax),其中 ymax 是所有数据中的最大值
  • start angle is just x0(name) (on a scale of 0 to 2PI)起始角度只是 x0(name)(范围为 0 到 2PI)
  • end angle is the same as above + the width.结束角度与上面相同+宽度。 Which is circumference/number of names.这是周长/名字的数量。

  let arc2 = d3.arc()
    .innerRadius(innerRadius)
    .outerRadius((d) => {
      return y(ymax);
    })
    .startAngle(d => {
      return x0(d.name)
    })
    .endAngle(d => {
      //let circumference = 2* Math.PI;
      //let bandwidth = circumference/names.length;
      let endAngle = x0(d.name) + (x0.bandwidth() * 0.8);
      return endAngle;
    })
    .padAngle(0.01)
    .padRadius(innerRadius);

  svg.append("g")
    .selectAll("path")
    .data(new_data)
    .enter()
    .append("path")
    .attr('fill', d => {
      return "none";
    })
    .attr("stroke", "#333")
    .attr("stroke-width", 2)
    .attr("d", arc2);

New JS Fiddle: https://jsfiddle.net/kpu5o7v2/1/新的 JS 小提琴: https://jsfiddle.net/kpu5o7v2/1/

Result:结果: 在此处输入图像描述

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

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