javascriptjquerycssd3.jshtmlelements

What is the difference between D3 and jQuery?


Referring to this example:

http://vallandingham.me/stepper_steps.html

it seems that the D3 and jQuery libraries are very similar in the sense that they both do DOM manipulation in an object-chaining way.

I'm curious as to know what functions D3 makes easier than jQuery and vice versa. There are plenty of graphing and visualization libraries that use jQuery as a basis (e.g., , , ).

Please give specific examples of how they are different.


Solution

  • Following code is an example of D3 usage which is not possible with jQuery (try it in jsfiddle):

      // create selection
      var selection = d3.select('body').selectAll('div');
    
      // create binding between selection and data
      var binding = selection.data([50, 100, 150]);
    
      // update existing nodes
      binding
        .style('width', function(d) { return d + 'px'; });
    
      // create nodes for new data
      binding.enter()
        .append('div')
        .style('width', function(d) { return d + 'px'; });
    
      // remove nodes for discarded data
      binding.exit()
        .remove();