D3 Development Environment

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Setup D3.js Development Environment

In this chapter, we will learn how to setup d3.js development environment.

Before we start, you'll need the following components:

  1. D3 library
  2. Web server
  3. Editor
  4. Web browser

D3 Library:

You need to include d3.js library into your HTML webpage in order to use D3 to create data visualization. You can do it by two way:

  1. Include D3 library from your project's folder
  2. Include D3 library from CDN (Content Delivery Network).

Download D3 Library:

Since D3 is an open-source library, the source code is freely available on the D3 website d3js.org.

  1. Visit the D3 website: https://d3js.org
  2. Download the latest version of d3 (d3.zip). As of writing this, the latest version is 4.6.0.

  3. After the download is complete, unzip the d3 folder and look for d3.min.js This is the minified version of the d3 source code. Copy d3.min.js file and paste it to your project's root folder or any other folder where you want to keep all your library files. Include d3.min.js file in your HTML page as shown below.

Example: Use D3 Library from Local Machine/Server


<!DOCTYPE html>
<html lang="en">
<head>
    <script src="../d3.min.js"></script>
</head>
<body>

<script>
    // write your d3 code here.. 
</script>
</body>
</html>
            

D3 is a JavaScript code so you can write all your d3 code into <script> tag. You may need to manipulate existing DOM elements so it is advisable to write D3 code just before the end of </body> tag

Include D3 Library from CDN:

You can use D3 library by linking it directly to your HTML page from the Content Delivery Network (CDN). CDN is a network of servers where files are hosted and are delivered to a user based on their geographic location. If you use the CDN, you don't need to download the source code.

Include D3 library using CDN url https://d3js.org/d3.v4.min.js into your page as shown below.

Example: Use D3 library from CDN


<!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>
            

Web Server:

Most browsers serve local HTML files directly in your web browser. However, there are certain restrictions when it comes to loading external data files. In the later chapters of this tutorial, we'll be loading data from external files like CSV and JSON. So it will be easier for you if you set up the web server right from the beginning.

You can use any webserver you are comfortable with e.g. IIS, Apache etc.

Editor:

Finally, you will need an editor where you can start writing your code. There are some great IDEs (Integrated Development Environment) with support for Javascript like

  • Visual Studio Code
  • Webstorm
  • Eclipse
  • SublimeText

These IDEs provide intelligent code completion as well as support some of the modern Javascript frameworks.

If you don't have fancy IDEs, you can always use Notepad!

Web Browser:

D3 works on all browsers except IE8 and lower. For the purposes of this tutorial, we will use Google Chrome.

After setting up the development environment, it's time to start exploring D3. Learn how to manipulate DOM by first selecting it and then manipulating with D3 in the next sections.