D3.js People
   HOME

TheInfoList



OR:

D3.js (also known as D3, short for Data-Driven Documents) is a
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
library for producing dynamic, interactive
data visualization Data and information visualization (data viz or info viz) is an interdisciplinary field that deals with the graphic representation of data and information. It is a particularly efficient way of communicating when the data or information is num ...
s in
web browser A web browser is application software 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 screen. Browsers are used on ...
s. It makes use of
Scalable Vector Graphics Scalable Vector Graphics (SVG) is an XML-based vector image 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 sinc ...
(SVG),
HTML5 HTML5 is a markup language used for structuring and presenting content on the World Wide Web. It is the fifth and final major HTML version that is a World Wide Web Consortium (W3C) recommendation. The current specification is known as the HTML ...
, and
Cascading Style Sheets Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone techno ...
(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 (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
, 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 (meaning i ...
, 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-visualisation specialist. He is one of the co-creators of ''Observable'' and noted as one of the key developers of D3.js, a JavaScript library used for producing dynamic, interactive, o ...
, and Vadim Ogievetsky of
Stanford University Stanford University, officially Leland Stanford Junior University, is a private research university in Stanford, California. The campus occupies , among the largest in the United States, and enrolls over 17,000 students. Stanford is consider ...
'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 hovering over a screen element or component, a text box displays information about that element, such as a description of a button's f ...
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 ; also ) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other ser ...
,
comma-separated values A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separat ...
(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 cross-platform and language-independent interface that treats an XML or HTML document as a tree structure wherein each node is an object representing a part of the document. The DOM represents a document wi ...
(DOM) nodes, then use operators to manipulate them in a similar manner to
jQuery jQuery is a JavaScript library designed to simplify HTML DOM tree traversal and manipulation, as well as event handling, CSS animation, and Ajax. It is free, open-source software using the permissive MIT License. As of Aug 2022, jQuery is used ...
. 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.


API structure

Given the wide array of visualizations and utilities supported by D3.js, the library is split into multiple
GitHub GitHub, Inc. () is an Internet hosting service for software development and version control using Git. It provides the distributed version control of Git plus access control, bug tracking, software feature requests, task management, continuous ...
repositories, each containing many functions related to a certain area. These areas include: * Visualization creation **
Axis An axis (plural ''axes'') is an imaginary line around which an object rotates or is symmetrical. Axis may also refer to: Mathematics * Axis of rotation: see rotation around a fixed axis * Axis (mathematics), a designator for a Cartesian-coordinat ...
creation
d3-axis
** Chord diagrams
d3-chord
**
Contour Contour may refer to: * Contour (linguistics), a phonetic sound * Pitch contour * Contour (camera system), a 3D digital camera system * Contour, the KDE Plasma 4 interface for tablet devices * Contour line, a curve along which the function ha ...
creation
d3-contour
** Delaunay and Voronoi diagrams
d3-delaunay
** Force-directed graphs
d3-force
** Map creation
d3-geo
** Hierarchy visualization
d3-hierarchy
** Path/line creation
d3-path
** Polygon creation
d3-polygon
** Visual encoding of data in scales
d3-scale
** Scales specifically for colors
d3-scale-chromatic
** Shape generation
d3-shape
* Interaction and Animation ** Brushing, for chart region selection
d3-brush
** Element dragging
d3-drag
** Transition easing
d3-ease
** Visual transitions and animation
d3-transition
** Zoom and Pan functionality
d3-zoom
* Data and visualization utilities ** Array utilities
d3-array
** Color utilities
d3-color
** Data file parsing
d3-dsv
** Data fetching
d3-fetch
** String formatting
d3-format
**
Quadtree A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees are the two-dimensional analog of octrees and are most often used to partition a two-dimensional space by recursively subdividing it into four q ...
utilities
d3-quadtree
** Random numbers and distributions
d3-random
**
DOM Dom or DOM may refer to: People and fictional characters * Dom (given name), including fictional characters * Dom (surname) * Dom La Nena (born 1989), stage name of Brazilian-born cellist, singer and songwriter Dominique Pinto * Dom people, an et ...
selection and manipulation
d3-selection
** Time utilities
d3-time
** Time formatting
d3-time-format
** Timer creation
d3-timer


Maths

* Generation of pseudorandom numbers with
normal Normal(s) or The Normal(s) may refer to: Film and television * ''Normal'' (2003 film), starring Jessica Lange and Tom Wilkinson * ''Normal'' (2007 film), starring Carrie-Anne Moss, Kevin Zegers, Callum Keith Rennie, and Andrew Airlie * ''Norma ...
,
log-normal In probability theory, a log-normal (or lognormal) distribution is a continuous probability distribution of a random variable whose logarithm is normally distributed. Thus, if the random variable is log-normally distributed, then has a normal ...
,
Bates Bates may refer to: Places * Bates, Arkansas, an unincorporated community * Bates, Illinois. an unincorporated community in Sangamon County * Bates, Michigan, a community in Grand Traverse County * Bates, New York, a hamlet in the town of Ell ...
, and Irwin-Hall distributions. * Transformations in 2D:
translation Translation is the communication of the Meaning (linguistic), meaning of a #Source and target languages, source-language text by means of an Dynamic and formal equivalence, equivalent #Source and target languages, target-language text. The ...
,
rotation Rotation, or spin, is the circular movement of an object around a '' central axis''. A two-dimensional rotating object has only one possible central axis and can rotate in either a clockwise or counterclockwise direction. A three-dimensional ...
,
skew Skew may refer to: In mathematics * Skew lines, neither parallel nor intersecting. * Skew normal distribution, a probability distribution * Skew field or division ring * Skew-Hermitian matrix * Skew lattice * Skew polygon, whose vertices do not ...
, and
scaling Scaling may refer to: Science and technology Mathematics and physics * Scaling (geometry), a linear transformation that enlarges or diminishes objects * Scale invariance, a feature of objects or laws that do not change if scales of length, energ ...
.


Arrays

D3.js array operations are built to complement existing array support in JavaScript (
mutator method In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (together also known as accessors), which returns the value of the priva ...
s: sort, reverse, splice, shift and unshift; accessor methods: concat, join, slice, indexOf and lastIndexOf; iteration methods:
filter Filter, filtering or filters may refer to: Science and technology Computing * Filter (higher-order function), in functional programming * Filter (software), a computer program to process a data stream * Filter (video), a software component tha ...
, every, forEach,
map A map is a symbolic depiction emphasizing relationships between elements of some space, such as objects, regions, or themes. Many maps are static, fixed to paper or some other durable medium, while others are dynamic or interactive. Although ...
, some,
reduce Reduction, reduced, or reduce may refer to: Science and technology Chemistry * Reduction (chemistry), part of a reduction-oxidation (redox) reaction in which atoms have their oxidation state changed. ** Organic redox reaction, a redox react ...
and reduceRight). D3.js extends this functionality with: * Functions for finding minimum, maximum, extent, sum, mean, median, and quantile of an array. * Functions for ordering, shuffling, permuting, merging, and bisecting arrays. * Functions for nesting arrays. * Functions for manipulating associative arrays. * Support for map and set collections.


Geometry

* Computing
convex hull In geometry, the convex hull or convex envelope or convex closure of a shape is the smallest convex set that contains it. The convex hull may be defined either as the intersection of all convex sets containing a given subset of a Euclidean space ...
of a set of points. * Computing Voronoi tesselation of a set of points. * Support for point
quadtree A quadtree is a tree data structure in which each internal node has exactly four children. Quadtrees are the two-dimensional analog of octrees and are most often used to partition a two-dimensional space by recursively subdividing it into four q ...
data structure. * Support for basic operations on polygon.


Color

* Support for
RGB The RGB color model is an additive color model in which the red, green and blue primary colors of light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three addi ...
, HSL,
HCL HCL may refer to: Science and medicine * Hairy cell leukemia, an uncommon and slowly progressing B cell leukemia * Harvard Cyclotron Laboratory, from 1961 to 2002, a proton accelerator used for research and development * Hollow-cathode lamp, a spe ...
, and L*a*b* color representation. * Brightening, darkening, and interpolation of colors.


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 Vega and Vega-Lite are visualization tools implementing a grammar of graphics, similar to ggplot2. The Vega and Vega-Lite grammars extend Leland Wilkinson Leland Wilkinson (November 5, 1944 – December 10, 2021) was an American statistician ...
are a declarative visualization specification built on parts of D3.js.


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. Libraries With the expanded demands for JavaScript, an ea ...


External links

*
D3.js Gallery

Blocksplorer
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
Visualization software Visualization or visualisation (see spelling differences) is any technique for creating images, diagrams, or animations to communicate a message. Visualization through visual imagery has been an effective way to communicate both abstract an ...
Free software programmed in JavaScript JavaScript libraries JavaScript visualization toolkits Software using the BSD license Visualization API