1

Could someone describe a simple strategy of integration AngularJS and D3.js? Both libraries seem to work so differently. D3 wants all the data bound to DOM elements and Angular wants to keep it in the ViewModel ("controller").

I am having hard time thinking of how to approach writing a UI in D3 that is interactive while still keeping things in AngularJS.

Any help would be appreciated.

1 Answer 1

3

Use Angular directives for almost everything. Use of controllers should be minimal. In your d3 code, only interact with the element that the directive provides in its link function. e.g.

.directive('myDirective', function() {
  return {
    link: function(scope, element, attrs) {
      d3.select(element[0]).append('svg');
    }
  };
});
Sign up to request clarification or add additional context in comments.

2 Comments

Are you saying all the D3.js code would go in the link function? Then, what happens if you need to do something to some other element on click of this one?
Yup. You would use Angular functionality to communicate to the other directive that it needs to change its state. I usually use a service to manager communication between directives. Import the service in your directives and have them register callbacks with the service in their link functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.