Scalable Vector Graphics or SVG for short is a XML markup language that is the standard for creating vector-based graphics in web browsers. The true power of d3.js is unlocked through its use of svg elements. Employing vectors allows for the image that are created to be various sizes based on scale.
One unique characteristic of svg is that the coordinate system starts in the upper left with the x axis increases as normal from left to right. However, the y axis starts from the top and increases going down rather than the opposite of increasing from the bottom up. Below is a visual of this.
You can see that (0,0) is in the top left corner. As you go to the right the x-axis increases and as you go down the y axis increases. By changing the x, y axis values you are able to position your image where you want it. If you’re curious the visual above was made using d3.js.
For the rest of post, we will look at different svg elements that can be used in making visuals. We will look at the following
- Lines & Text
Below is the code for making circle followed by the output
To make a shape such as the circles above, you first must specify the size of the svg element as shown in line 6. Then you make a circle element. Inside the circle element you must indicate the x and y position (cx, cy) and also the radius (r). The default color is black however you can specify other colors as shown below.
To change the color simply add the style argument and indicate the fill and color in quotations.
Below is the code for making rectangles/squares. The arguments are slightly different but this should not be too challenging to figure out.
The x, y arguments indicate the position and the width and height arguments determine the size of the rectangle, square.
Lines & Text
Lines are another tool in d3.js. Below is the code for drawing a line.
The code should not be too hard to understand. You now need to separate coordinates. This is because the line needs to start in one place and draw until it reaches another. You can also control the color of the line and the thickness.
You can also add text using svg. In the code below we combine the line element with the text element.
With the text element after you set the position, font, font size, and color, you have to also add your text in-between the tags of the element.
The path element is slightly trickier but also more powerful when compared to the elements we have used previously. Below is the code and output from using the path element.
The path element has a mini-language all to its self. “M” is where the drawing begins and is followed by the xy coordinate. “L” draw a line. Essentially, it takes the original position and draws a line to next position. “V” indicates a vertical line. Lastly, “Z” means to close the path.
In the code below here is what it literally means
- Start at 40, 40
- Make a line from 40, 40 to 250, 40
- Make another line from 250, 40 to 140, 40
- Make a vertical line from 140,40 to 4,40
- Close the path
Using path can be much more complicated than this. However, this is enough for an introduction
This was just a teaser of what is possible with d3.js. The ability to make various graphics based on data is something that we have not even discussed yet. As such, there is much more to look forward to when using this visualization tool.