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:
- D3 library
- Web server
- Web browser
You need to include D3.js library into your HTML webpage in order to use D3 to create data visualization. You can do it in two ways:
- Include D3 library from your project's folder
- 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.
- Visit the D3 website: https://d3js.org
- Download the latest version of d3 (d3.zip). (At the time of writing this chapter, the latest version is 4.6.0.)
- 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.
<!DOCTYPE html> <html lang="en"> <head> <script src="../d3.min.js"></script> </head> <body> <script> // write your d3 code here.. </script> </body> </html>
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.
<!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>
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 latter 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.
- Visual Studio Code
If you don't have fancy IDEs, you can always use Notepad!
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 section.