d3 data()数据绑定中的key函数

官网https://github.com/d3/d3-selection/blob/master/README.md#selection_data

var data = [
  {name: "Locke", number: 4},
  {name: "Reyes", number: 8},
  {name: "Ford", number: 15},
  {name: "Jarrah", number: 16},
  {name: "Shephard", number: 31},
  {name: "Kwon", number: 34}
];

d3.selectAll("div")
  .data(data, function(d) { return d ? d.name : this.id; })
    .text(function(d) { return d.number; });

This example key function uses the datum d if present, and otherwise falls back to the element’s id property. Since these elements were not previously bound to data,

the datum d 

is null when the key function is evaluated on selected elements,and

non-null when the key function is evaluated on the new data.

这样写就行了。

原文地址:https://www.cnblogs.com/xuanmanstein/p/9939476.html