Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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., highcharts, flot, wijmo).

Please give specific examples of how they are different.

like image 822
zcaudate Avatar asked Nov 01 '12 23:11

zcaudate


People also ask

Is D3 js still used?

The JavaScript ecosystem has completely changed during this time, in terms of libraries, best practices and even language features. Nevertheless, D3 is still here. And it's more popular than ever.

What is D3 JavaScript used for?

D3 is a JavaScript library and framework for creating visualizations. D3 creates visualizations by binding the data and graphical elements to the Document Object Model. D3 associates (binding) the data (stuff you want to visualize) with the DOM. This allows the user to manipulate, change or add to the DOM.

Is D3 a JavaScript framework?

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS.

Which is better CSS or jQuery?

What's the difference? In a nutshell, CSS uses your graphic card to process the transition, where as using jQuery uses your main processor. However jQuery is generally more backwards compatible than CSS. That said, CSS transitions will revert to jQuery if the browser doesn't support CSS transitions.


2 Answers

  • D3 is data-driven but jQuery is not: with jQuery you directly manipulate elements, but with D3 you provide data and callbacks through D3's unique data(), enter() and exit() methods and D3 manipulates elements.

  • D3 is usually used for data visualization but jQuery is used for creating web apps. D3 has many data visualization extensions and jQuery has many web app plugins.

  • Both are JavaScript DOM manipulation libraries, have CSS selectors and fluent API and are based on web standards which makes them look similar.

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(); 
like image 57
Ali Shakiba Avatar answered Oct 20 '22 00:10

Ali Shakiba


d3 has a silly description. jQuery and d3 are not at all similar, you just don't use them for the same things.

jQuery's purpose is to do general dom manipulation. It's a general purpose javascript toolkit for anything you might want to do.

d3 was primarily designed to make it easy to make shiny graphs with data. You should definitely use it (or something similar, or something built on top of it) if you want to make graphical visualizations of data.

If you want a general purpose JS library for all your interactive form needs, consider jQuery or proto or mootools. If you want something tiny, consider underscore.js. If you want something with dependency injection and testability, consider AngularJS.

A General comparison guide from wikipedia.

I can see why someone would think they are similar. They use a similar selector syntax -- $('SELECTOR'), and d3 is an extremely powerful tool for selecting, filtering, and operating on html elements, especially while chaining these operations together. d3 tries to explain this to you on its home page by claiming to be a general purpose library, but the fact is that most people use it when they want to make graphs. It is pretty unusual to use it for your average dom manipulation because the d3 learning curve is so steep. It is, however, a far more general tool than jQuery, and generally people build other more specific libraries (such as nvd3) on top of d3 rather than using it directly.

@JohnS's answer is also very good. Fluent API = method chaining. I also agree about where the plugins and extension lead you with the libraries.

like image 32
Case Avatar answered Oct 19 '22 23:10

Case