D3.js

D3.js
Developer(s) Mike Bostock, Jeffrey Heer, Vadim Ogievetsky, and community
Initial release 18 February 2011 (2011-02-18)
Stable release
4.4.0 / 22 November 2016 (2016-11-22)[1]
Development status Active
Written in JavaScript
Type Data visualization, JavaScript library
License BSD
Website d3js.org

D3.js (or just D3 for Data-Driven Documents) is a JavaScript library for producing dynamic, interactive data visualizations in web browsers. It makes use of the widely implemented SVG, HTML5, and CSS standards. It is the successor to the earlier Protovis framework.[2] In contrast to many other libraries, D3.js allows great control over the final visual result.[3] Its development was noted in 2011,[4] as version 2.0.0 was released in August 2011.[5]

Data visualization company Datameer officially uses D3.js as its core technology,[3] while The New York Times sometimes uses it for rich graphs.[6] It is used by the iD editor for editing OpenStreetMap.[7][8] D3.js has been extensively used for GIS map making, managing both GeoJSON and Topojson files as input.

Context

The first web browsers appeared in the early 1990s. They were initially capable of displaying static web pages only: the only way for a user to interact with the web was through clicking links and scrolling pages. There were many efforts to overcome such limitations. One of the most significant was the integration of JavaScript as the scripting language for web browsers. JavaScript gradually became the de facto standard language for creating web pages with rich user interactivity. This played a crucial role in the decision to use JavaScript as the language of D3.js.

At the same time, researchers, engineers, and practitioners from various branches of engineering and science looked for tools that would enable web browsers to visually present data within web pages. There were multiple projects with that goal, each of which had its successes and failures, and inspired the subsequent ones. The most notable examples were the Prefuse, Flare, and Protovis toolkits, which can all be considered as direct predecessors of D3.js.

Prefuse was a visualization toolkit created in 2005 that required usage of Java, and visualizations were rendered within browsers with a Java plug-in. Flare was a similar toolkit from 2007 that used ActionScript, and required a Flash plug-in for rendering.

In 2009, based on the experience of developing and utilizing Prefuse and Flare, Professor Jeff Heer, Ph.D. student Mike Bostock, and M.S. student Vadim Ogievetsky of Stanford University's Stanford Visualization Group created Protovis, a JavaScript library to generate SVG graphics from data. The library received notable acceptance both by data visualization practitioners and academics.[9]

In 2011, the development of Protovis was stopped to focus on a new project, D3.js. Informed by experiences with Protovis, Bostock, along with Heer and Ogievetsky, developed D3.js to provide a more expressive framework that, at the same time, focuses on web standards and provides improved performance.[10]

Technical principles

Embedded within an HTML webpage, the JavaScript D3.js library uses pre-built JavaScript functions to select elements, create SVG objects, style them, or add transitions, dynamic effects or tooltips to them. These objects can also be widely styled using CSS. Large datasets can be easily bound to SVG objects using simple D3.js functions to generate rich text/graphic charts and diagrams. The data can be in various formats, most commonly JSON, comma-separated values (CSV) or geoJSON, but, if required, JavaScript functions can be written to read other data formats.

Selections

The central principle of D3.js design is to enable the programmer to first use a CSS-style selector to select a given set of Document Object Model (DOM) nodes, then use operators to manipulate them in a similar manner to jQuery.[11] For example, by using D3.js, one may select all HTML <p>...</p> elements, and then change their text color, e.g. to lavender:

 d3.selectAll("p")                 // select all <p> elements
   .style("color", "lavender")     // set style "color" to value "lavender"
   .attr("class", "squares")       // set attribute "class" to value "squares"
   .attr("x", 50);                 // set attribute "x" (horizontal position) to value 50px

The selection can be based on tag (as in the above example), class, identifier, attribute, or place in the hierarchy. Once elements are selected, one can apply operations to them. This includes getting and setting attributes, display texts, and styles (as in the above example). Elements may also be added and removed. This process of modifying, creating and removing HTML elements can be made dependent on data, which is the basic concept of D3.js.

Transitions

By declaring a transition, values for attributes and styles can be smoothly interpolated over a certain time. The following code will make all HTML <p>...</p> elements on a page gradually change their text color to pink:

 d3.selectAll("p")             // select all <p> elements
   .transition("trans_1")      // transition with name "trans_1"
     .delay(0)                 // transition starting 0ms after trigger
     .duration(500)            // transitioning during 500ms
     .ease("linear")           // transition easing progression is linear...
   .style("color", "pink");    // ... to color:pink

Data-binding

For more advanced uses, loaded data drives the creation of elements. D3.js loads a given dataset, then, for each element of it, create an SVG object with associated properties (shape, colors, values) and behaviors (transitions, events).[12][13][14]

// Data
  var data = [
     { name:"Ireland",  income:53000, life: 78, pop:6378, color: "green"},
     { name:"Norway",   income:73000, life: 87, pop:5084, color: "blue" },
     { name:"Tanzania", income:27000, life: 50, pop:3407, color: "grey" }
  ];
// Create SVG container
  var svg = d3.select("#hook").append("svg")
        .attr("width", 120)
        .attr("height", 120)
        .style("background-color", "#D0D0D0");
// Create SVG elements from data 
    svg.selectAll("circle")                  // create virtual circle template
      .data(data)                            // bind data
    .enter()                                 // for each row in data...
      .append("circle")                      // bind circle & data row such that... 
        .attr("id", function(d) { return d.name })           // set the circle's id according to the country name
        .attr("cx", function(d) { return d.income /1000  })  // set the circle's horizontal position according to income 
        .attr("cy", function(d) { return d.life })           // set the circle's vertical position according to life expectancy 
        .attr("r",  function(d) { return d.pop /1000 *2 })   // set the circle's radius according to country's population 
        .attr("fill",function(d){ return d.color });         // set the circle's color according to country's color

Generated SVG graphics are designed according to the provided data.

Appending nodes using data

Once a dataset is bound to a document, use of D3.js typically follows a pattern wherein an explicit .enter() function, an implicit "update," and an explicit .exit() function is invoked for each item in the bound dataset. Any methods chained after the .enter() command will be called for each item in the dataset not already represented by a DOM node in the selection (the previous selectAll()). Likewise, the implicit update function is called on all existing selected nodes for which there is a corresponding item in the dataset, and .exit() is called on all existing selected nodes that do not have an item in the dataset to bind to them. The D3.js documentation provides several examples of how this works.[15]

API structure

D3.js API contains several hundred functions, and they can be grouped into following logical units:[16]

Maths

Arrays

D3.js array operations are built to complement existing array support in JavaScript (mutator methods: sort, reverse, splice, shift and unshift; accessor methods: concat, join, slice, indexOf and lastIndexOf; iteration methods: filter, every, forEach, map, some, reduce and reduceRight). D3.js extends this functionality with:

Geometry

Colour

Similar tools and libraries

References

  1. "d3 Releases". Github.com. Retrieved November 29, 2016.
  2. "For Protovis Users", Mbostock.github.com, retrieved August 18, 2012
  3. 1 2 Viau, Christophe (June 26, 2012), "What's behind our Business Infographics Designer? D3.js of course", Datameer's blog, retrieved August 18, 2012
  4. Myatt, Glenn J.; Johnson, Wayne P. (September 2011), "5.10 Further reading", Making Sense of Data III: A Practical Guide to Designing Interactive Data Visualizations, Hoboken, New Jersey: John Wiley & Sons, p. A–2, ISBN 978-0-470-53649-0, retrieved January 23, 2013
  5. "Release Notes", D3.js, retrieved August 22, 2012
  6. Ashkenas, Jeremy; Bloch, Matthew; Carter, Shan; Cox, Amanda (May 17, 2012). "The Facebook Offering: How It Compares". Nytimes.com. Retrieved January 23, 2013.
  7. "New OpenStreetMap Editor iD Increases Participation". Mapbox.com. Retrieved 26 December 2013.
  8. "iD". OpenStreetMap wiki. Retrieved 26 December 2013.
  9. Academic example: Savva, Manolis; Kong, Nicholas; Chhajta, Arti; Li, Feifei; Agrawala, Maneesh; Heer, Jeffrey (2011), "ReVision: Automated Classification, Analysisand Redesign of Chart Images", ACM User Interface Software & Technology, retrieved January 23, 2013
  10. (Bostock, Ogievetsky & Heer 2011)
  11. [Bostock, Ogievetsky & Heer 2011, chap. 3
  12. Bostock, Mike (February 5, 2012), Thinking with Joins
  13. "A Pen by Lopez Hugo". Codepen.io. Retrieved 2016-08-01.
  14. "Edit fiddle". JSFiddle.net. Retrieved 2016-08-01.
  15. "Three Little Circles". Mbostock.github.io. Retrieved 2016-08-01.
  16. d3 (2016-06-30). "API Reference · d3/d3 Wiki · GitHub". Github.com. Retrieved 2016-08-01.

Further reading

Background on data visualization and information graphics:

Background on D3.js itself:

Using D3.js - beginner level:

Using D3.js - intermediate level:

Others

Videos

External links

Wikimedia Commons has media related to D3.js.
This article is issued from Wikipedia - version of the 11/29/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.