d3js Tutorial
d3js Tutorial
js
This is an introductory tutorial, which covers the basics of Data-Driven Documents and
explains how to deal with its various components and sub-components.
Audience
This tutorial is prepared for professionals who are aspiring to make a career in online data
visualization. This tutorial is intended to make you comfortable in getting started with the
Data-Driven Documents and its various functions.
Prerequisites
Before proceeding with the various types of concepts given in this tutorial, it is being
assumed that the readers are already aware about what a Framework is. In addition to
this, it will be very helpful, if the readers have a sound knowledge on HTML, CSS and
JavaScript.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
i
D3.js
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Copyright and Disclaimer ................................................................................................................................. i
Table of Contents ............................................................................................................................................ ii
9. D3.js – Animation.................................................................................................................................... 49
The duration() Method .................................................................................................................................. 50
ii
D3.js
iii
D3.js
iv
1.D3.JS – INTRODUCTION D3.js
Data visualization is the presentation of data in a pictorial or graphical format. The primary
goal of data visualization is to communicate information clearly and efficiently via statistical
graphics, plots and information graphics.
Data visualization helps us to communicate our insights quickly and effectively. Any type of
data, which is represented by a visualization allows users to compare the data, generate
analytic reports, understand patterns and thus helps them to take the decision. Data
visualizations can be interactive, so that users analyze specific data in the chart. Well, Data
visualizations can be developed and integrated in regular websites and even mobile
applications using different JavaScript frameworks.
What is D3.js?
D3.js is a JavaScript library used to create interactive visualizations in the browser. The
D3.js library allows us to manipulate elements of a webpage in the context of a data set.
These elements can be HTML, SVG, or Canvas elements and can be introduced, removed,
or edited according to the contents of the data set. It is a library for manipulating the DOM
objects. D3.js can be a valuable aid in data exploration, it gives you control over your data's
representation and lets you add interactivity.
D3.js Features
D3.js is one of the best data visualization framework and it can be used to generate simple
as well as complex visualizations along with user interaction and transition effects. Some of
its salient features are listed below:
Extremely flexible.
Easy to use and fast.
Supports large datasets.
5
D3.js
Declarative programming.
Code reusability.
Has wide variety of curve generating functions.
Associates data to an element or group of elements in the html page.
D3.js Benefits
D3.js is an open source project and works without any plugin. It requires very less code and
comes up with the following benefits:
It is modular. You can download a small piece of D3.js, which you want to use. No
need to load the whole library every time.
DOM manipulation.
In the next chapter, we will understand how to install D3.js on our system.
6
2.D3.JS – INSTALLATION D3.js
In this chapter, we will learn how to set up the D3.js development environment. Before we
start, we need the following components:
D3.js library
Editor
Web browser
Web server
D3.js Library
We need to include the D3.js library into your HTML webpage in order to use D3.js to create
data visualization. We can do it in the following two ways:
After the download is complete, unzip the file and look for d3.min.js. This is the minified
version of the D3.js source code. Copy the d3.min.js file and paste it into your project's root
folder or any other folder, where you want to keep all the library files. Include the
d3.min.js file in your HTML page as shown below.
<!DOCTYPE html>
<html lang="en">
<head>
<script src="/path/to/d3.min.js"></script>
7
D3.js
</head>
<body>
<script>
// write your d3 code here..
</script>
</body>
</html>
D3.js is a JavaScript code, so we should write all our D3 code within “script” tag. We may
need to manipulate the existing DOM elements, so it is advisable to write the D3 code just
before the end of the “body” tag.
Include the D3.js library using the CDN URL https://d3js.org/d3.v4.min.js into our page as
shown below.
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script>
// write your d3 code here..
</script>
</body>
</html>
8
D3.js
D3.js Editor
We will need an editor to start writing your code. There are some great IDEs (Integrated
Development Environment) with support for JavaScript like –
WebStorm
Eclipse
Sublime Text
These IDEs provide intelligent code completion as well as support some of the modern
JavaScript frameworks. If you do not have fancy IDE, you can always use a basic editor like
Notepad, VI, etc.
Web Browser
D3.js works on all the browsers except IE8 and lower.
Web Server
Most browsers serve local HTML files directly from the local file system. However, there are
certain restrictions when it comes to loading external data files. In the latter chapters of this
tutorial, we will be loading data from external files like CSV and JSON. Therefore, it will be
easier for us, if we set up the web server right from the beginning.
You can use any web server, which you are comfortable with – e.g. IIS, Apache, etc.
9
3.D3.JS – CONCEPTS D3.js
Laying out visual elements for linear, hierarchical, network and geographic data.
Web Standards
Before we can start using D3.js to create visualizations, we need to get familiar with web
standards. The following web standards are heavily used in D3.js.
JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
10
D3.js
<title></title>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Document</title>
</head>
<body>
<div>
<h1>Greeting</h1>
<p>Hello World!</p>
</div>
</body>
</html>
11
D3.js
Think of SVG as a canvas on which we can paint different shapes. So to start with, let us
create an SVG tag:
The default measurement for SVG is pixels, so we do not need to specify if our unit is pixel.
Now, if we want to draw a rectangle, we can draw it using the code below:
12
D3.js
We can draw other shapes in SVG such as – Line, Circle, Ellipse, Text and Path.
Just like styling HTML elements, styling SVG elements is simple. Let us set the background
color of the rectangle to yellow. For that, we need to add an attribute “fill” and specify the
value as yellow as shown below:
JavaScript
JavaScript is a loosely typed client side scripting language that executes in the user's browser.
JavaScript interacts with HTML elements (DOM elements) in order to make the web user
interface interactive. JavaScript implements the ECMAScript Standards, which includes core
features based on ECMA-262 specifications as well as other features, which are not based on
the ECMAScript standards. JavaScript knowledge is a prerequisite for D3.js.
13
4.D3.JS – SELECTIONS D3.js
Selections is one of the core concepts in D3.js. It is based on CSS selectors. It allows us to
select one or more elements in a webpage. In addition, it allows us to modify, append, or
remove elements in a relation to the pre-defined dataset. In this chapter, we will see how to
use selections to create data visualizations.
D3.js helps to select elements from the HTML page using the following two methods:
select() – Selects only one DOM element by matching the given CSS selector. If there
are more than one elements for the given CSS selector, it selects the first one only.
selectAll() – Selects all DOM elements by matching the given CSS selector. If you
are familiar with selecting elements with jQuery, D3.js selectors are almost the same.
ID of a HTML element
Selection by Tag
You can select HTML elements using its TAG. The following syntax is used to select the
“div” tag elements,
d3.select(“div”)
<!DOCTYPE html>
<html>
<head>
14
D3.js
By requesting the webpage through the browser, you will see the following output on the
screen:
d3.select(“.<class name>”)
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3/d3.min.js"></script>
15
D3.js
</head>
<body>
<div class="myclass">
Hello World!
</div>
<script>
alert(d3.select(".myclass").text());
</script>
</body>
</html>
By requesting the webpage through the browser, you will see the following output on the
screen.
Selection by ID
Every element in a HTML page should have a unique ID. We can use this unique ID of an
element to access it using the select() method as specified below.
d3.select(“#<id of an element>”)
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3/d3.min.js"></script>
16
D3.js
</head>
<body>
<div id="hello">
Hello World!
</div>
<script>
alert(d3.select("#hello").text());
</script>
</body>
</html>
By requesting the webpage through the browser, you will see the following output on the
screen.
17
D3.js
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3/d3.min.js"></script>
</head>
<body>
<div class="myclass">
Hello World!
</div>
<script>
d3.select("div.myclass").append("span");
</script>
</body>
</html>
Requesting the webpage through browser, you could see the following output on the screen,
Here, the append() method adds a new tag span inside the div tag as shown below:
<div class="myclass">
Hello World!<span></span>
</div>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3/d3.min.js"></script>
</head>
<body>
<div class="myclass">
Hello World!
</div>
<script>
d3.select("div.myclass").append("span").text("from D3.js");
</script>
</body>
</html>
Now refresh the webpage and you will see the following response.
Here, the above script performs a chaining operation. D3.js smartly employs a technique
called the chain syntax, which you may recognize from jQuery. By chaining methods
together with periods, you can perform several actions in a single line of code. It is fast and
easy. The same script can also access without chain syntax as shown below.
19
D3.js
Modifying Elements
D3.js provides various methods, html(), attr() and style() to modify the content and style
of the selected elements. Let us see how to use modify methods in this chapter.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3/d3.min.js"></script>
</head>
<body>
<div class="myclass">
Hello World!
</div>
<script>
20
D3.js
By requesting the webpage through the browser, you will see the following output on the
screen.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3/d3.min.js"></script>
</head>
<body>
<div class="myclass">
Hello World!
</div>
<script>
d3.select(".myclass").attr("style", "color: red");
</script>
</body>
</html>
21
D3.js
22