Implied Functions Applied

When binding a primitive datum to a node in a D3 visualization, the callback function can be implicitly called on the datum without the typical verbosity:

1
2
3
4
5
6
svg.selectAll("circle")
    .data([32, 57, 112, 293])
  .enter().append("circle")
    .attr("cy", 90)
    .attr("cx", String)
    .attr("r", Math.sqrt);

In line 5, String() is performed on the data element, as is Math.sqrt in line 6, with the same result as:

1
2
3
4
5
6
7
8
9
10
svg.selectAll("circle")
    .data([32, 57, 112, 293])
  .enter().append("circle")
    .attr("cy", 90)
    .attr("cx", function(d) {
      return String(d);
    })
    .attr("r", function(d) {
      return Math.sqrt(d);
    });

… but better.

Awesome.

Resources

Three Little Circles D3 Tutorial