简体   繁体   English

使用D3.js创建可变数量的元素

[英]Creating variable number of elements with D3.js

I am pretty new to d3.js and maybe my question is very basic, but I haven't been able to find an answer... 我是d3.js的新手,也许我的问题很基本,但是我一直找不到答案...

I am trying to achieve the following, based on an array of data like this: 我试图基于这样的数据数组实现以下目标:

var data = [
    {
        "items": 10,
        "selected": 8
    },
    {
        "items": 12,
        "selected": 4
    }];

I would like to create a row of circles for every element of the array. 我想为数组的每个元素创建一行圆圈。 The number of circles should be the equal to the items property and in every row, the circle in selected position should be special (like a different color). 圆圈数应等于items属性,并且在每一行中,选定位置的圆圈应特殊(如不同的颜色)。 For the example above, it should display something similar to: 对于上面的示例,它应显示类似以下内容的内容:

OOOOOOO*OO OOOOOOO * OO

OOO*OOOOOOOO OOO * OOOOOOOO

For the first step, any tips on how to create a variable number of SVG elements based on data values would be a great help. 对于第一步,有关如何基于数据值创建可变数量的SVG元素的任何技巧都将有很大的帮助。

Here's an example I made on codepen. 这是我在Codepen上制作的示例。

Check out the code below and/or fork the codepen and have a play. 请查看下面的代码和/或派生代码笔并玩一玩。

Essentially, what is happening here is that I add a g element for each item in your data array. 本质上,这里发生的是我为数据数组中的每个项目添加了一个g元素。 Normally we might be-able to create a circle for each data element, but since that is contained within a property and variable, I've used an each loop (d3 each). 通常我们可以为每个数据元素创建一个圆,但是由于该圆包含在属性和变量中,因此我使用了一个each循环(每个d3)。 This creates a loop of the number of items, and creates a circle for each. 这将创建一个项目数量循环,并为每个项目创建一个圆圈。 If the element is selected, the fill color changes. 如果选择了元素,则填充颜色会更改。

things to note: 注意事项:

  • The g element has a transform of 30 * i on the y axis. g元素在y轴上的转换为30 * i This means that each group will stack down the page. 这意味着每个组将向下堆叠页面。
  • In the for loop we get access to the g element using this , but we must use the d3 select function to reference it as a d3 object so we can append. for循环中,我们可以使用this来访问g元素,但是必须使用d3 select函数将其作为d3对象引用,以便我们可以追加。

The code: 编码:

//add an svg canvas to the page
var svg = d3.select("body")
.append("svg")
.attr("transform", "translate(" + 20 + "," + 20 + ")"); //this will give it a bottom and left margin

//set up some basic data
var data = [
    {
        "items": 10,
        "selected": 8
    },
    {
        "items": 12,
        "selected": 4
    }];

var groups = svg.selectAll("g")
        .data(data)
        .enter()
    .append("g").attr("transform", function(d, i) {
      return "translate(0," + i * 30 + ")";
    });

groups.each(function(d,i) {
    for (var x = 1; x <= d.items; x++) {
      d3.select(this).append('circle').attr({cx: function(d,i) { return (x + 1) * 22; } ,
             cy: 10 ,
             r: 10 ,
             fill: function(d,i) { return x == d.selected ? "yellow" : "blue" }
       }); 
    }
});

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

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