Understanding how D3.js binds data to nodes

前端 未结 3 1483
春和景丽
春和景丽 2020-12-22 18:53

I\'m reading through the D3.js documentation, and am finding it hard to understand the selection.data method from the documentation.

This is the example code given

3条回答
  •  悲&欢浪女
    2020-12-22 19:58

    Use the counter i to show the index of the data being used.

    var tr = d3.select("body").append("table").selectAll("tr")
    .data(matrix)
    .enter().append("tr") //create a row for each data entry, first index
    .text(function(d, i) { return i}); // show the index i.e. d[0][] then d[1][] etc.
    
    var td = tr.selectAll("td")
    .data(function(d) { return d; })
    .enter().append("td")
    .style("background-color", "yellow") //show each cell
    .text(function(d,i) { return i + " " + d; }); // i.e d[from the tr][0] then d[from the tr][1]...
    

提交回复
热议问题