HTML Svg Css Js for Data Visualisation Sample
HTML Svg Css Js for Data Visualisation Sample
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean
Publishing process. Lean Publishing is the act of publishing an in-progress ebook using
lightweight tools and many iterations to get reader feedback, pivot until you have the
right book and build traction once you do.
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Setting up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 CodePen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Stay in touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Translators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3. HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.1 Headings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.4 <div> element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.5 Dropdown menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.6 <svg> element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.7 Wrapping up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4. SVG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.1 Adding SVG to a web page . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 SVG elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3 SVG Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5. CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.1 The Structure of CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.2 CSS Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.3 CSS Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.4 More CSS selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.5 CSS for HTML layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.6 Media queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
CONTENTS
6. JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.1 JavaScript variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.2 JavaScript data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.3 JavaScript arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.4 JavaScript objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.5 JavaScript operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.6 JavaScript conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.7 JavaScript functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.8 JavaScript iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
6.9 JavaScript functions (advanced) . . . . . . . . . . . . . . . . . . . . . . . 21
6.10 JavaScript advanced concepts . . . . . . . . . . . . . . . . . . . . . . . . . 22
8. Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
8.1 Installing Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
8.2 Writing a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
8.3 Node packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
9. JavaScript modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
9.1 CommonJS (Node.js) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
9.2 ES Modules (the new standard) . . . . . . . . . . . . . . . . . . . . . . . . 25
1.1 Setting up
In order to get started quickly I recommend using CodePen1 to look at and experiment
with code examples (see next section). At the end of this book, in the ‘Tools and Set-up’
chapter I show you how to set up tools so that you can write code locally (where the files
are stored on your computer instead of on CodePen). This takes a bit of time to set up
but I recommend this approach if you become more serious about web development.
1.2 CodePen
CodePen2 is a web site that lets you experiment with HTML, SVG, CSS and JavaScript
without having to set up any tools.
NOTES 2
At key points in the book there’ll be a link to a CodePen ‘pen’ that contains a code example.
If you visit the link you’ll be able to view and experiment with the code. I encourage you
to play around with the CodePen examples - there’s very little you can break and learning
through experimentation is very effective.
You can freely edit the CodePen examples from this book and your changes don’t affect
the original example. If you want to keep your changes, click Fork (at the bottom of the
page) and create a CodePen account (or log in). Save your work by clicking the Save
button at the top of the page.
1.4 Translators
If you’re interested in translating Fundamentals of HTML, SVG, CSS and JavaScript for
Data Visualisation to another language contact me at info@createwithdata.com and I’ll
be happy to discuss further.
Notes
1 https://codepen.io/
2 https://codepen.io/
3 https://www.createwithdata.com/
2. Web languages: HTML, SVG, CSS
& JavaScript
Most websites and web applications (including data visualisations) are built from 4
languages: HTML, SVG, CSS and JavaScript.
HTML stands for Hyper Text Markup Language and describes the content of a webpage.
SVG stands for Scalable Vector Graphics and describes shapes such as circles, lines and
rectangles. It’s often used when visualising data (to describe bars, lines, circles, axes,
legends etc.).
CSS stands for Cascading Style Sheets and describes the style (such as font weight, colour
or size) and position of HTML and SVG elements.
JavaScript is a programming language that can manipulate HTML and SVG, do general
computation (such as data manipulation) and handle interaction.
We look at each of these in the upcoming chapters.
3. HTML
HTML is a language that describes the content of a webpage using elements that define
headings, paragraphs, images, links etc. For example a paragraph element looks like:
The paragraph element consists of an opening tag <p>, some content and a closing tag
</p>. Tags usually come in pairs. An opening tag looks like <p> and a closing tag looks
like </p>.
When a web browser loads an HTML document it draws each element according to the
type of element (paragraph, heading, image etc.). For example, the paragraph element
looks like this when rendered in a web browser:
There’s a large number of HTML elements but you can achieve a lot with just a few of
them. This chapter looks at five common elements: headings, lists, images, div elements
and svg elements. (These five elements are usually sufficient when visualising data.)
3.1 Headings
Heading elements are used for titles. There are six levels ranging from large (<h1>) to
small (<h6>).
<h1>Main heading</h1>
<h2>Sub heading</h2>
<h3>Level 3 heading</h3>
<h4>Level 4 heading</h4>
<h5>Level 5 heading</h5>
<h6>Level 6 heading</h6>
The web browser determines how each of these elements is styled. For example
the <h1> is the largest and all the headings are rendered with bold text. You
can set the style yourself using CSS.
3.2 Lists
The list element <ul> represents lists of items. Each item is represented by a list item
element <li> and nested within the <ul> element:
<ul>
<li>List item one</li>
<li>List item two</li>
</ul>
Notice that we’ve indented the <li> elements. This is optional but is the usual way to
indicate nested elements. When displayed in a browser the list looks like:
By default list items are bulleted and displayed as a vertical list. However
you can change how a list looks using CSS. Often navigation menus are
represented using a list and CSS used to hide the bullet points and position
the items as desired.
HTML 6
3.3 Images
Images can be added to an HTML document using the <img> element. <img> elements
require an attribute named src that specifies the URL of the image.
Attributes can be added to the opening tag of any HTML element and
they specify additional information about the element. They take the form
name="value" where the value is always in quotes.
Image elements don’t have a closing tag. Here’s an image element that displays an image
located at https://picsum.photos/id/127/300/200.jpg:
<img src="https://picsum.photos/id/127/300/200.jpg">
Typically your image files will live alongside your source code. For example, you might
have an image file my-image.jpg within a directory named images. In this case you’d
write:
<img src="images/my-image.jpg">
By default images are displayed at their original size. You can set the display width
(or height) of an image using the width (or height) attribute. Let’s set the width of the
previous image to 200 pixels:
<div>
<h1>Main title</h1>
<h2>Secondary title</h2>
</div>
<div>
<h3>Article title</h3>
<div>
<p>First paragraph</p>
<p>Second paragraph</p>
</div>
</div>
Notice how the elements have been indented. This is optional and doesn’t affect how the
elements appear in the browser but it’s useful when reading and working with the code.
The above example looks like the following when rendered in a web browser:
See if you can relate each HTML element to each line of output.
By default the <div> elements don’t affect how the HTML is rendered. In other
words, the above HTML will look the same with or without the <div> elements.
However you can add CSS to style the <div> elements. For example you could
add margins to each <div> to add more space between the headings.
HTML 8
Inside the <svg> element are two attributes: width and height. These are similar to the
width and height attributes in <img> elements.
HTML 9
It’s quite important to specify the SVG element’s size (using width and height attributes)
because its default size is quite small and shapes are cropped to the SVG’s boundary.
SVG will be covered in greater depth in the SVG chapter.
3.7 Wrapping up
There are many other HTML elements but when visualising data the ones covered in this
section are the most common.
You can see a comprehensive list of HTML elements at https://developer.mozilla.org/en-
US/docs/Web/HTML/Element4 .
Most of the examples in this book are presented using CodePen which makes it
very easy to get started. However it doesn’t reveal the whole story. Web pages
are typically made up of individual HTML, CSS and JavaScript documents.
HTML documents typically contain extra information that CodePen doesn’t
show. We’ll look at this in more detail in the ‘Tools and set-up’ chapter.
3.8 Exercises
Here’s some HTML that includes most of the elements covered in this chapter:
<div>
<h1>Main header</h1>
<h2>Sub header</h2>
</div>
<div>
<h4>Article title</h4>
<div>
<p>First paragraph</p>
<img src="https://picsum.photos/id/127/300/200.jpg">
<p>Second paragraph</p>
<ul>
<li>List item one</li>
<li>List item two</li>
</ul>
<p>Third paragraph</p>
</div>
</div>
HTML 10
4.2.1 Line
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
4.2.2 Rectangle
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
4.2.3 Circle
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
4.2.4 Text
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
SVG 12
4.2.5 Group
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
4.2.6 Path
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
4.2.7 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
4.3.1 Translate
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
4.3.2 Rotate
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
4.4 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
5. CSS
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
5.3.2 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
5.3.4 Exercise
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
5.3.6 Example
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
5.4.1.1 id
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
CSS 15
5.4.1.2 class
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
5.4.2 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
5.5.2 Flexbox
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
CSS 16
6.1.0.1 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.2.1 Strings
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
JavaScript 18
6.2.1.3 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.2.2 Numbers
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.2.2.1 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.2.3 Booleans
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
JavaScript 19
6.3.3 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.4.1 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.5.3 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.5.3.1 Answers
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
JavaScript 20
6.6.1 if
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.6.2 switch
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.7.4 Exercise
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
JavaScript 21
6.8.2 forEach
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.8.3 map
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.8.4 filter
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.8.5 Exercises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.8.5.1 Answers
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
6.10.1 Promises
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
7. JavaScript and the Browser
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
Tools and Set-up 27
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
10.2.7 Summary
This content is not available in the sample book. The book can be purchased on Leanpub
at http://leanpub.com/html-svg-css-js-for-data-visualisation.
Notes
4 https://developer.mozilla.org/en-US/docs/Web/HTML/Element
5 https://codepen.io/createwithdata/pen/ExQGeeY