Book Image

Mastering SVG

By : Rob Larsen
Book Image

Mastering SVG

By: Rob Larsen

Overview of this book

SVG is the most powerful image format in use on the web. In addition to producing resolution-independent images for today's multi-device world, SVG allows you to create animations and visualizations to add to your sites and applications. The simplicity of cross-platform markup, mixed with familiar modern web languages, such as CSS and JavaScript, creates a winning combination for designers and developers alike. In this book, you will learn how to author an SVG document using common SVG features, such as elements and attributes, and serve SVG on the web using simple configuration tips for common web servers. You will also use SVG elements and images in HTML documents. Further, you will use SVG images for a variety of common tasks, such as manipulating SVG elements, adding animations using CSS, mastering the basic JavaScript SVG (API) using Document Object Model (DOM) methods, and interfacing SVG with common libraries and frameworks, such as React, jQuery, and Angular. You will then build an understanding of the Snap.svg and SVG.js APIs, along with the basics of D3, and take a look at how to implement interesting visualizations using the library. By the end of the book, you will have mastered creating animations with SVG.
Table of Contents (17 chapters)
Title Page

Creating an SVG data visualization

This first section is going to focus on putting together a basic data visualization using SVG and JavaScript. This particular visualization will focus on an illustration, the positive/negative variance from an average. In this case, it will illustrate the number of home runs hits, per season, by the baseball player David Ortiz in his career with the Boston Red Sox compared with his average number of home runs over his Red Sox career.  


From 2003 until 2016 David Ortiz hit a minimum of 23 and a maximum of 54 home runs in a season while playing for the Red Sox. He averaged 34.5 per season. This visualization will show the relative positive/negative variance of his home run totals for every year against that 34.5 average. Years in which he hit more than the average will be in green. Years, where he hit less, will be in red. 

The steps we'll need to go through are as follows:

  1. We will take the data and get the total number of years, the total number of home runs...