As D3 is a well established library, we won't go very deep into D3 specifics in this article, but rather focus on how to properly utilize D3 in Meteor applications with full reactivity.
The good news is that D3 integrates very well with Meteor applications to create collection driven visualizations. Only minor changes are needed to adapt most D3 examples to be fully reactive to any data changes. Since D3 directly manipulates the DOM to create the data visualization, the Meteor rendering engine (Blaze) is not very involved in the actual rendering as D3 handles the creation and manipulation of SVG elements which make up the visualization. Meteor covers everything but the rendering as it manages the backing data, orchestrates the reactive updates, and binds events to the visualization elements.
Since D3 can be used to produce wide array of data visualizations, a good way to start on building your own is to look over the many high quality examples and find one that is close to what you are looking for. A multitude of excellent example graphs can be found in the following galleries: https://github.com/mbostock/d3/wiki/Gallery, http://bl.ocks.org/mbostock
In the demo application for this article we have provided example code for most basic chart types common in web applications. Each of these charts is fully reactive to data changes in documents, adding or removing documents, and sorting.
The approach in this document seems to only work with documents that are bound directly to DOM elements. It doesn't work when data is post-processed with `d3.nest` followed by a `d3.layout`, for example - which are often required for more advanced aggregate visualizations. However, doing the aggregate operations continuously may be doable if the underlying data array is updated efficiently; see comment below.
We will use the bar chart example to talk about the basic pattern used to integrate D3 into a Meteor application. This same basic pattern was used for all common chart examples and should be extendable to any D3 visualization within a Meteor application.
We start by defining an empty SVG element in template markup
Not much here at all, this is really just a placeholder for D3 to do its work.
All D3 code goes in the rendered callback for the template. Some code has been omitted using ... for brevity and to more clearly illustrate the pattern.
//perform a reactive query on the collection to get an array
var dataset = Bars.find(...).fetch();
//update scale domains and axises
//select elements that correspond to documents
var bars = svg.selectAll("rect")
.data(dataset, key); //bind dataset to objects using key function
//handle new documents via enter()
//handle updates to documents via transition()
//handle removed documents via exit()
Following this basic pattern allows for relatively straightforward refactoring of most D3 examples to be driven via Meteor collections. The key part is separating the D3 code into what gets run once to initiate the visual (before the Deps.autorun block) and what gets run on each collection query result change (inside the Deps.autorun block).
This seems like it will only work with very small datasets. `cursor.fetch()` deep copies all documents when it is called, so this will be extremely slow. It seems like the best approach is to create a single reactive array that is updated using `cursor.observe`, specifically using the `addedAt` and `removedAt` callbacks, which can then be repeatedly joined with `selection.data` in D3. This basically accomplishes the same effect as repeatedly calling `fetch()` without the huge CPU and memory usage of a large collection, and could be easily implemented as part of a d3 helpers package.