Cascading Style Sheets (CSS)
Explore how to use CSS in D3.js to style simple graphs, focusing on selectors, properties, and values to change line width, color, and fill. Understand how CSS rules influence the appearance of graph elements and practice modifying styles for interactive visuals.
We'll cover the following...
Let’s take a look at the CSS part of the simple graph code to understand why our graph looks the way it does.
The CSS portion of the code
The CSS code is as follows:
Understanding the CSS portion
CSS gives us control over the look, feel, and presentation of web content. The idea is to define a set of properties to objects on the web page.
They are made up of “rules.” Each rule has a “selector” and one or more “declarations.” Each declaration has a property and a value (or a group of properties and values).
For instance, in the CSS code above, we have the following:
-
lineis the selector. The period (.) in front oflineindicates that the selector is a “class.” This tells us that on the web page, any particular elementline(and we are going to apply this rule to the line of our graph), which we decorate with the “class,” will have the various declarations applied to it. -
There are three declarations as part of the rule. These are contained within curly braces and separated by semicolons.
-
One of the declarations is for the width of the graph line (
stroke-width: 2px;). The property isstroke-width:, and the value is2px(2 pixels). This tells the web page that any element in the web page that has the classlinewill have lines drawn that are (amongst other things) 2 pixels wide.
Sure enough, if we look at the line of the graph:
That looks as if the line might actually be 2 pixels wide!
Let’s try a test. We can change that particular declaration (line 4) to the following:
stroke-width: 20px;
And the result is:
Ahh, 20 pixels of goodness!
Because we’re getting the hang of things now, let’s change the color declaration to:
stroke: red;
And we get:
Awesome! I think we can safely say that this has had the desired effect. So what else is there?
Since there’s only one declaration left, it seems like a shame not to try something different with it:
fill: blue;
We’ll get:
So the fill property (line 2) looks like it will change the color of the area that would be closed by the line. Nice.
One thing to take away from this small exercise is that there is a good deal of flexibility in adjusting the properties of elements on the web page via CSS.
You can test these changes by editing the CSS portion (highlighted) below:
date,close 1-May-12,58.13 30-Apr-12,53.98 27-Apr-12,67.00 26-Apr-12,89.70 25-Apr-12,99.00 24-Apr-12,130.28 23-Apr-12,166.70 20-Apr-12,234.98 19-Apr-12,345.44 18-Apr-12,443.34 17-Apr-12,543.70 16-Apr-12,580.13 13-Apr-12,605.23 12-Apr-12,622.77 11-Apr-12,626.20 10-Apr-12,628.44 9-Apr-12,636.23 5-Apr-12,633.68 4-Apr-12,624.31 3-Apr-12,629.32 2-Apr-12,618.63 30-Mar-12,599.55 29-Mar-12,609.86 28-Mar-12,617.62 27-Mar-12,614.48 26-Mar-12,606.98