D3.js (also known as D3, short for Data-Driven Documents) is a
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
library for producing dynamic, interactive
data visualization
Data and information visualization (data viz/vis or info viz/vis) is the practice of designing and creating Graphics, graphic or visual Representation (arts), representations of a large amount of complex quantitative and qualitative data and i ...
s in
web browser
A web browser, often shortened to browser, is an application for accessing websites. When a user requests a web page from a particular website, the browser retrieves its files from a web server and then displays the page on the user's scr ...
s. It makes use of
Scalable Vector Graphics
Scalable Vector Graphics (SVG) is an XML-based vector graphics format for defining two-dimensional graphics, having support for interactivity and animation. The SVG specification is an open standard developed by the World Wide Web Consortium sin ...
(SVG),
HTML5
HTML5 (Hypertext Markup Language 5) is a markup language used for structuring and presenting hypertext documents on the World Wide Web. It was the fifth and final major HTML version that is now a retired World Wide Web Consortium (W3C) recommend ...
, and
Cascading Style Sheets (CSS) standards. It is the successor to the earlier Protovis framework. Its development was noted in 2011, as version 2.0.0 was released in August 2011. With the release of version 4.0.0 in June 2016, D3 was changed from a single library into a collection of smaller, modular libraries that can be used independently.
Context
There have been various previous attempts to bring data visualization to web browsers. 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
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, and visualizations were rendered within browsers with a Java plug-in. Flare was a similar toolkit from 2007 that used
ActionScript
ActionScript is an object-oriented programming language originally developed by Macromedia Inc. (later acquired by Adobe). It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript (mean ...
, and required a Flash plug-in for rendering.
In 2009, based on the experience of developing and utilizing Prefuse and Flare,
Jeffrey Heer
Jeffrey Michael Heer (born June 15, 1979) is an American computer scientist best known for his work on information visualization and interactive data analysis. He is a professor of computer science & engineering at the University of Washington, ...
,
Mike Bostock
Michael Bostock is an American computer scientist and data visualization specialist. He is one of the co-creators of Observable and a key developer of D3.js, a JavaScript library used to produce dynamic, interactive data visualizations for web ...
, and Vadim Ogievetsky of
Stanford University
Leland Stanford Junior University, commonly referred to as Stanford University, is a Private university, private research university in Stanford, California, United States. It was founded in 1885 by railroad magnate Leland Stanford (the eighth ...
's Stanford Visualization Group created Protovis, a JavaScript library to generate SVG graphics from data. The library was known to data visualization practitioners and academics.
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.
Technical principles
The D3.js library uses pre-built functions to select elements, create SVG objects, style them, or add transitions, dynamic effects, or
tooltip
The tooltip, also known as infotip or hint, is a common graphical user interface (GUI) element in which, when hoverbox, hovering over a screen element or component, a text box displays information about that element, such as a description of a ...
s. These objects can also be styled using CSS. Large datasets can be bound to SVG objects using D3.js functions to generate text/graphic charts and diagrams. The data can be in various formats such as
JSON
JSON (JavaScript Object Notation, pronounced or ) is an open standard file format and electronic data interchange, data interchange format that uses Human-readable medium and data, human-readable text to store and transmit data objects consi ...
,
comma-separated values
Comma-separated values (CSV) is a text file format that uses commas to separate values, and newlines to separate records. A CSV file stores Table (information), tabular data (numbers and text) in plain text, where each line of the file typically r ...
(CSV) or
geoJSON
GeoJSON is an open standard format designed for representing simple geographical features, along with their non-spatial attributes. It is based on the JSON format.
The features include points (therefore addresses and locations), line strings ...
, 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
The Document Object Model (DOM) is a cros s-platform and language-independent API that treats an HTML or XML document as a tree structure wherein each node is an object representing a part of the document. The DOM represents a document with ...
(DOM) nodes, then use operators to manipulate them in a similar manner to
jQuery. For example, one may select all HTML paragraph elements (represented by ), and then change their text color, e.g. to lavender:
d3.selectAll("p") // select all 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 an HTML tag, 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 elements on a page gradually change their text color to pink:
d3.selectAll("p") // select all elements
.transition("trans_1") // transition with name "trans_1"
.delay(0) // transition starting 0ms after trigger
.duration(500) // transitioning for 500ms
.ease(d3.easeLinear) // 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 of its elements, creates an SVG object with associated properties (shape, colors, values) and behaviors (transitions, events).
// Data
var countriesData = ,
,
// 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(countriesData) // bind data
.join("circle") // joins data to the selection and creates circle elements for each individual data
.attr("id", function(d) ) // set the circle's id according to the country name
.attr("cx", function(d) ) // set the circle's horizontal position according to income
.attr("cy", function(d) ) // set the circle's vertical position according to life expectancy
.attr("r", function(d) ) // set the circle's radius according to country's population
.attr("fill", function(d) ); // 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.
See also
*
JavaScript framework
A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build and ...
*
JavaScript library
A JavaScript library is a library of pre-written JavaScript code that allows for easier development of JavaScript-based applications, especially for AJAX and other web-centric technologies. They can be included in a website by embedding it directl ...
*
Open-source software
Open-source software (OSS) is Software, computer software that is released under a Open-source license, license in which the copyright holder grants users the rights to use, study, change, and Software distribution, distribute the software an ...
References
Further reading
; Background on D3.js itself
*
; Using D3.js - beginner level
*
*
; Using D3.js - intermediate level
*
*
; Others
*
*
*
*
*
*
*
; Videos
*
*
Related projects
* The
Vega and Vega-Lite visualisation grammars are a declarative visualization specification built on parts of D3.js.
External links
*
D3.js GalleryBlocksplorer search for blocks by methods used
* D3 "reusable chart" libraries:
** C3 https://c3js.org
** Plotly https://plot.ly/javascript
bl.ock.org an
blockbuilder.org popular early gallery system.
Observablehq.com: main D3js playground.
{{Authority control
Articles with example JavaScript code
Visualization software
Visualization (or American and British English spelling differences#-ise, -ize (-isation, -ization), visualisation ), also known as graphics visualization, is any technique for creating images, diagrams, or animations to communicate a message. ...
Free software programmed in JavaScript
JavaScript libraries
JavaScript visualization toolkits
Software using the BSD license
Visualization API