Advanced Rendering with Callbacks


By default, D3 visualizations created with r2d3 are monolithic scripts that do all of their work as a single computation (the body of the script). While this makes scripts straightforward to author by default, not all visualizations will confirm to this simple model. Rather, some visualizations will want to distinguish between the following activities:

  1. Code that performs one-time initialization for the visualization (e.g. creation of a D3 layout).

  2. Code that runs whenever the data underlying the visualization is changed.

  3. Code that runs whenever the visualizations container element is resized.

This article describes how you can use the r2d3 advanced rendering API to organize your visualization code to run in this more granular fashion.

onRender Callback

To distinguish between code that runs at initialization-time only and code that runs when data changes, organize your code so that the code which responds to data changes is contained within the r2d3.onRender() callback. For example:

// Initialization
 .attr("font-family", "sans-serif")
 .attr("font-size", "8")
 .attr("text-anchor", "middle");
var pack = d3.pack()
  .size([width, height])
var format = d3.format(",d");
var color = d3.scaleOrdinal(d3.schemeCategory20c);

// Rendering
r2d3.onRender(function(data, svg, width, height, options) {
  var root = d3.hierarchy({children: data})
    .sum(function(d) { return d.value; })
    .each(function(d) {
      if (id = {
        var id, i = id.lastIndexOf("."); = id;
        d.package = id.slice(0, i);
        d.class = id.slice(i + 1);

  var node = svg.selectAll(".node")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

      .attr("id", function(d) { return; })
      .attr("r", function(d) { return d.r; })
      .style("fill", function(d) { return color(d.package); });

      .attr("id", function(d) { return "clip-" +; })
      .attr("xlink:href", function(d) { return "#" +; });

      .attr("clip-path", function(d) { return "url(#clip-" + + ")"; })
    .data(function(d) { return d.class.split(/(?=[A-Z][^A-Z])/g); })
      .attr("x", 0)
      .attr("y", function(d, i, nodes) { return 13 + (i - nodes.length / 2 - 0.5) * 10; })
      .text(function(d) { return d; });

      .text(function(d) { return + "\n" + format(d.value); });

onResize Callback

By default, when the element which contains your visualization is resized, r2d3 will call back your script to re-render the visualization from scratch using the new size. In some cases this might be acceptable, but in other cases it makes sense to have code which explicitly handles the resize in a more efficient fashion.

You can provide an explicit resize handler by implementing the r2d3.onResize() callback. For example, in a force directed D3 layout you might do this in the onResize() callback:

r2d3.onResize(function(width, height) {
  force.force("center", d3.forceCenter(width / 2, height / 2))