DG LogoDeepak G.
Back to Series

1. The Canvas & Coordinate System

5 min read
d3svg

1. How to add D3.js to your project?

You can add D3 to your project by installing it via npm (npm install d3) or by including it directly via a CDN script tag in your HTML file.

2. The Core Mechanics: D3 and SVG

D3.js (Data-Driven Documents) is a JavaScript library that takes raw numbers (your data) and turns them into visual shapes on your screen. Instead of you manually writing code for every single bar in a bar chart, you give D3 an array of data, and D3 automatically generates all the required HTML or SVG elements based on those numbers.

While D3 can manipulate standard HTML elements like a <div>, its true power is unlocked when paired with SVG (Scalable Vector Graphics).

Unlike traditional raster images (like PNG or JPG) which are painted using static, hard-coded pixels, an SVG draws shapes using math. Think of an SVG element as an infinite, high-resolution whiteboard. Instead of coloring in pixels, you feed it instructions like "draw a <circle> here" or "draw a <path> there."

The D3 and SVG Relationship

If SVG is the mathematical whiteboard, D3 is the master artist holding the protractor. D3 calculates exactly where every line, circle, and rectangle needs to go based on your raw data, and then draws it flawlessly onto that SVG canvas.

3. Why D3? An Example

Let's say we have an array of two numbers: [100, 250], and we want to draw two <rect> bars to represent them.

Vanilla JS

Long, messy, and repetitive. You must manually orchestrate loops, declare obscure XML namespaces, and write identical boilerplate for every shape.

With D3.js

Clean, declarative, and automatic. Just map your data to elements, tell D3 what you want, and it handles the rest.

In the D3 version, we don't write a `for` loop. We pass a function (d, i) => ... to the .attr() method, and D3 figures out the rest. This declarative pattern is what makes D3 unparalleled for data visualization.

4. The SVG Coordinate System

Before we draw anything, you must understand how the SVG canvas works. The mathematical coordinate system in an SVG is completely flipped from the standard Cartesian graphs you learned in school.

(0,0)
+ X Axis
+ Y Axis
(x: 128, y: 96)

Welcome to the flipped world of computer graphics:

  • 1The Origin Point (0,0) is permanently anchored to the absolute Top-Left Corner.
  • 2The X-Axis increases as you move exactly like normal: left-to-right.
  • 3The Y-Axis is inverted! It increases as you move downwards towards the bottom of the screen.

Because the Y-Axis points downwards, any shape you draw with y="0" is anchored to the absolute top edge of the SVG. If you are trying to build a standard bar chart where bars sit at the bottom of the screen, you actually have to calculate their Y position dynamically based on the total height of the SVG canvas minus the height of the specific bar. We will learn how to automate this math using D3 Scales later!

The Golden Rule of SVG Positioning

Always remember: To move an element RIGHT, increase X. To move an element DOWN, increase Y.

Example: Placing Circles in the Grid

Let's cement this with a quick practical example. If we create a 200x200 SVG canvas and place three circles, let's observe how cx (center X) and cy (center Y) affect their positions.

(0,0)(100,100)(180,40)

Notice how the rose circle at (0,0) is perfectly centered directly on the top-left corner itself. Because of how the canvas works, three-quarters of that circle is completely cut off out-of-bounds! The amber circle is at (100, 100), placing it dead center, and the emerald circle is at (180, 40), pushing it far right but keeping it near the top.