I'm playing around with different ways of integrating d3 and angular, in the context of learning both of these frameworks, and am hoping for some input:

My client application receives a JSON array of nodes and an array of edges from my server. The central component of the client is a graph visualization implemented as a d3 force-directed layout: For each node in the nodes array, a (SVG) circle element is added to this visualization, and lines between these circles are added for each edge in the edges array.

Of course, d3's selection.data( ) makes it trivial to add these elements and bind each to the data it represents, but the graph visualization is only part of a much larger application: I need to create different types of elements representing these same nodes and edges in different parts of the application (which d3 has nothing to do with), and I'd like to keep all of these elements bound to a single dataset.

My primary goal is minimizing code complexity, and - although I've fallen in love with the simplicity and elegance of d3's data-binding functionality - I've arrived at the tentative conclusion that using it in one part of the application while using Angular to do the same in other parts is creating unnecessary complexity, and that the simplest approach would be to use Angular to handle the data-binding/element-creation

In other words, instead of using selection.data( ).enter( ).append( ) to create SVG elements, I'm thinking I should do so using a ng-repeat="node in nodes", perhaps creating creating each as a custom directive to allow for custom functionality. Having done so, I would then need to get these elements "into" d3, so that they can be laid out on the screen and animated by its force-

Is my reasoning here sound? In particular, I'm worried that I'm overlooking complications this will create with regard to object constancy, which is an important requirement as nodes will be entering, exiting and moving about the screen constantly and I need these transitions to be smooth. How would you recommend I go about integrating my angular-created elements into d3 (more precisely, getting them into my force.nodes{ } and force.links( ) arrays) to avoid any such complications?

Finally, I'm also considering a strategy that I'm hoping might give me the best of both worlds: I could use d3 to create my SVG elements and bind them to their respective datum, but rather than executing that code in the link function of visualization directive (as I've been doing, and as all the ng/d3 tutorials I've found do), I could run it in the compile function, and do something like this:

    .enter( ).append('circle').attr("class", "node-icon"); //...other attributes/styles etc  

where node-icon is the normalized name of a directive with a restrict property that includes C. If this runs in the compile method, angular should then compile all of these directives as normal, adding any additional functionality / interfaces with other directives (etc.), the same way it does with any other type of element . Right?

This is the option I'm most attracted to intuitively - are there any pitfalls of it I might be overlooking, which might make the former strategy preferable?

NOTE: this question is cross-posted on SO. I know that cross-posting is discouraged in general, but in this case I'm not sure which forum is the most appropriate venue for this question: It does reference concrete implementations, but the core of the question is about general design patterns. Interpreted in the context of SO, I think its a question about implementation. Interpreted in this context, I think its a different question entirely, which is how I've justified cross-posting. Please feel free to comment if you disagree with this justification and I'll consider deleting the question.

  • I thought that there were Angular wrappers for D3. Did you look into any? I hope that you get some good answers here, as I am just about to start using D3 with Angular, for the first time, in a day or two. – Mawg says reinstate Monica Apr 4 '16 at 7:41
  • 1
    @Mawg see the cross-posted SO question linked to above - I did indeed get some good feedback there and the approach went pretty well. – drew moore Apr 4 '16 at 21:26
  • Personally, I might also cross-post on softwarerecs.stackexchange.com asking for an AngularJs wrapper for D3. And you should certainly search there for D3JS, as there are a few question which should be of help. – Mawg says reinstate Monica Apr 5 '16 at 7:38
  • @Mawg Well, cross-posting is frowned-upon so I shouldn't have it cross-posted here.. shhh :-P – drew moore Apr 5 '16 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.