Reader small image

You're reading from  D3.js 4.x Data Visualization - Third Edition

Product typeBook
Published inApr 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787120358
Edition3rd Edition
Languages
Tools
Right arrow
Authors (2):
Aendrew Rininsland
Aendrew Rininsland
author image
Aendrew Rininsland

<p>Aendrew Rininsland is a developer and journalist who has spent much of the last half a decade building interactive content for newspapers such as The Financial Times, The Times, Sunday Times, The Economist, and The Guardian. During his 3 years at The Times and Sunday Times, he worked on all kinds of editorial projects, ranging from obituaries of figures such as Nelson Mandela to high-profile, data-driven investigations such as The Doping Scandal the largest leak of sporting blood test data in history. He is currently a senior developer with the interactive graphics team at the Financial Times.</p>
Read more about Aendrew Rininsland

Swizec Teller
Swizec Teller
author image
Swizec Teller

Swizec Teller is a geek with a hat. Founding his first startup at 21, he is now looking for the next big idea as a full-stack web generalist focusing on freelancing for early-stage startup companies. When he isn't coding, he's usually blogging, writing books, or giving talks at various non-conference events in Slovenia and nearby countries. He is still looking for a chance to speak at a big international conference. In November 2012, he started writing Why Programmers Work at Night, and set out on a quest to improve the lives of developers everywhere.
Read more about Swizec Teller

View More author details
Right arrow

Chapter 2. A Primer on DOM, SVG, and CSS

You might already be used to manipulating DOM and CSS with libraries such as jQuery; if so, much of this will seem very familiar, as D3 has a full suite of manipulation tools. If not, don't worry, as this chapter exists to get everyone up to speed.

Very similar to HTML's DOM is the SVG namespace, which we'll use for most of the examples in this book. SVG is at the core of building truly great visualizations, so we'll take special care to understand it, starting out by manually drawing shapes and then doing the same using D3's path generators in Chapter 3, Shape Primitives of D3.

In this chapter, we'll take a look at the core technologies that make D3 tick. They are as follows:

  • Document Object Model (DOM)
  • Scalable Vector Graphics (SVG)
  • Cascading Style Sheets (CSS)

DOM


The Document Object Model (DOM) is a language-agnostic model for representing structured documents built in HTML, XML, or similar standards. You can think of it as a tree of nodes that closely resembles the document parsed by the browser.

 

At the top, there is an implicit document node, which represents the <html> tag; browsers create this tag even if you don't specify it and then build the tree off this root node according to what your document looks like. Consider a simple HTML file to be like the following:

<!DOCTYPE html> 
<title>A title</title> 
<div> 
  <p>A paragraph of text</p> 
</div> 
<ul> 
  <li>List item</li> 
  <li>List item 2, <em><strong>italic</strong></em></li> 
</ul>

Note how we don't have the <html>, <head> or <body> tags. Chrome will parse the preceding code to DOM, as follows:

Type document into the Chrome JavaScript console to get this tree view...

What exactly did we do here?


The key is in the three for...each statements that we used. One loops through the array of table header strings, and appends a table cell (td) element with each value into the thead row. There are then two nested .forEach() statements that do the same for each row in the body. We technically only have one row in the body right now, so probably didn't need that messy double for...each, but now all we have to do to add another row to the table is simply append another data array to the rows variable. We'll talk more about Array.prototype.forEach and other array functions in the next chapter.

This might seem like a lot of work for such a simple table, but the advantages of doing it this way are huge. Instead of wasting a bunch of time typing out a totally static table that you'll never use again, you've effectively created a basic JavaScript library that will produce a basic table for you whenever you need it. You could even extend your tableFactory function to do...

Scalable Vector Graphics


Scalable Vector Graphics (SVG) is a vector graphics format that describes images with XML. It has been around since 1999 and is supported by all major browsers nowadays.

Vector images can be rendered in any size without becoming fuzzy. This means that you can render the same image on a large retina display or a small mobile phone, and it will look great in both cases. SVG images are made up of shapes you can create from scratch using paths or put together from basic shapes (for example, lines and circles) defined in the standard. The format itself represents shapes with XML elements and attributes. Since it's an XML-style standard like HTML, quite a lot of what you may already know about HTML also applies to SVG.

As such, SVG code is just a bunch of text you can edit manually, inspect with your browser's normal debugging tools, and compress with standard text compression algorithms. Being text based also means that you can use D3 to create an image in your browser...

CSS


Cascading Stylesheets (CSS) have been with us since 1996, making them one of the oldest staples of the Web, even though they only reached widespread popularity with the tables versus CSS wars of the early 2000s.

You're probably familiar with using CSS for styling HTML. So, this section will be a refreshing breeze after all that weird-looking SVG stuff.

My favorite thing about CSS is its simplicity; consider the following code:

selector { 
  attribute: value; 
}

That describes CSS better than I can--you use selectors to modify properties using values. Although there's a bit more to it, particularly in terms of how properties cascade down the DOM tree, the above is pretty much it.

We've been using selectors all this time. A selector is any string that describes one or more elements in a DOM tree.

Note

Although you can get fancy with selectors, there's been a lot written about how to intelligently name classes recently so that most CSS is just simply just declarative. One such approach is BEM...

Summary


That was an intense chapter. Go have a nice beverage of some kind if you're still with me, you deserve it. If not, don't get too discouraged--that was a lot of material to get through, feel free to come back later if you need a refresher.

We've gone through DOM manipulation and looked at SVG in great detail, covering everything from drawing shapes manually to using transformations. Finally, we looked at CSS as an alternative for making things pretty.

Everything we look at from now on is going to build on these basics, but you now have the tools to draw anything you can think of.[footnote]

lock icon
The rest of the chapter is locked
You have been reading a chapter from
D3.js 4.x Data Visualization - Third Edition
Published in: Apr 2017Publisher: PacktISBN-13: 9781787120358
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Aendrew Rininsland

<p>Aendrew Rininsland is a developer and journalist who has spent much of the last half a decade building interactive content for newspapers such as The Financial Times, The Times, Sunday Times, The Economist, and The Guardian. During his 3 years at The Times and Sunday Times, he worked on all kinds of editorial projects, ranging from obituaries of figures such as Nelson Mandela to high-profile, data-driven investigations such as The Doping Scandal the largest leak of sporting blood test data in history. He is currently a senior developer with the interactive graphics team at the Financial Times.</p>
Read more about Aendrew Rininsland

author image
Swizec Teller

Swizec Teller is a geek with a hat. Founding his first startup at 21, he is now looking for the next big idea as a full-stack web generalist focusing on freelancing for early-stage startup companies. When he isn't coding, he's usually blogging, writing books, or giving talks at various non-conference events in Slovenia and nearby countries. He is still looking for a chance to speak at a big international conference. In November 2012, he started writing Why Programmers Work at Night, and set out on a quest to improve the lives of developers everywhere.
Read more about Swizec Teller