- D3.js - Working Example
- D3.js - Timer API
- Delimiter-Separated Values API
- D3.js - Requests API
- D3.js - Zooming API
- D3.js - Dragging API
- D3.js - Transitions API
- D3.js - Colors API
- D3.js - Shapes API
- D3.js - Axis API
- D3.js - Scales API
- D3.js - Paths API
- D3.js - Selection API
- D3.js - Collections API
- D3.js - Array API
- D3.js - Geographies
- D3.js - Graphs
- D3.js - Drawing Charts
- D3.js - Animation
- D3.js - Transition
- D3.js - SVG Transformation
- D3.js - Introduction to SVG
- D3.js - Data Join
- D3.js - Selections
- D3.js - Concepts
- D3.js - Installation
- D3.js - Introduction
- D3.js - Home
D3.js Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
D3.js - Data Join
Data join is another important concept in D3.js. It works along with selections and enables us to manipulate the HTML document with respect to our data set (a series of numerical values). By default, D3.js gives data set the highest priority in its methods and each item in the data set corresponds to a HTML element. This chapter explains data joins in detail.
What is a Data Join?
Data join enables us to inject, modify and remove elements (HTML element as well as embedded SVG elements) based on the data set in the existing HTML document. By default, each data item in the data set corresponds to an element (graphical) in the document.
As the data set changes, the corresponding element can also be manipulated easily. Data join creates a close relationship between our data and graphical elements of the document. Data join makes manipulation of the elements based on the data set a very simple and easy process.
How Data Join Works?
The primary purpose of the Data join is to map the elements of the existing document with the given data set. It creates a virtual representation of the document with respect to the given data set and provides methods to work with the virtual representation. Let us consider a simple data set as shown below.
[10, 20, 30, 25, 15]
The data set has five items and so, it can be mapped to five elements of the document. Let us map it to the p element of the following document using the selector s selectAll() method and data join s data() method.
HTML
<ul id = "pst"> <p><p> <p></p> </ul>
D3.js code
d3.select("#pst").selectAll("p").data([10, 20, 30, 25, 15]);
Now, there are five virtual elements in the document. The first two virtual elements are the two p element defined in the document as shown below.
1. p - 10 2. p - 20
We can use all the selector s element modifying methods pke attr(), style(), text(), etc., for the first two p as shown below.
d3.select("#pst").selectAll("p") .data([10, 20, 30, 25, 15]) .text(function(d) { return d; });
The function in the text() method is used to get the p elements mapped data. Here, d represent 10 for first p element and 20 for second p element.
The next three elements can be mapped to any elements and it can be done using the data join s enter() and selector s append() method. The enter() method gives access to the remaining data (which is not mapped to the existing elements) and the append() method is used to create a new element from the corresponding data. Let us create p for the remaining data items as well. The data map is as follows −
3. p - 30 4. p - 25 5. p - 15
The code to create new a p element is as follows −
d3.select("#pst").selectAll("p") .data([10, 20, 30, 25, 15]) .text(function(d) { return "This is pre-existing element and the value is " + d; }) .enter() .append("p") .text(function(d) { return "This is dynamically created element and the value is " + d; });
Data join provides another method called as the exit() method to process the data items removed dynamically from the data set as shown below.
d3.selectAll("p") .data([10, 20, 30, 15]) .exit() .remove()
Here, we have removed the fourth item from the data set and its corresponding p using the exit() and the remove() methods.
The complete code is as follows −
<!DOCTYPE html> <html> <head> <script type = "text/javascript" src = "https://d3js.org/d3.v4.min.js"></script> <style> body { font-family: Arial; } </style> </head> <body> <ul id = "pst"> <p></p> <p></p> </ul> <input type = "button" name = "remove" value = "Remove fourth value" oncpck = "javascript:remove()" /> <script> d3.select("#pst").selectAll("p") .data([10, 20, 30, 25, 15]) .text(function(d) { return "This is pre-existing element and the value is " + d; }) .enter() .append("p") .text(function(d) { return "This is dynamically created element and the value is " + d; }); function remove() { d3.selectAll("p") .data([10, 20, 30, 15]) .exit() .remove() } </script> </body> </html>
The result of the above code will be as follows −