Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Learning D3.js Mapping
Learning D3.js Mapping

Learning D3.js Mapping: Build stunning maps and visualizations using D3.js

Arrow left icon
Profile Icon Newton Profile Icon Oscar Villarreal
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3 (7 Ratings)
Paperback Dec 2014 126 pages 1st Edition
eBook
€8.99 €16.99
Paperback
€20.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Newton Profile Icon Oscar Villarreal
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3 (7 Ratings)
Paperback Dec 2014 126 pages 1st Edition
eBook
€8.99 €16.99
Paperback
€20.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€8.99 €16.99
Paperback
€20.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Learning D3.js Mapping

Chapter 2. Creating Images from Simple Text

In this chapter, a high-level overview of Scalable Vector Graphics (SVG) will be presented by explaining how it operates and what elements it encompasses. In a browser context, SVG is very similar to HTML and is one of the means by which D3 expresses its power. Understanding the nodes and attributes of SVG will empower us to create many kinds of visualizations, not just maps. This chapter includes the following points:

  • A general SVG overview and key elements
  • The SVG coordinate system
  • The primary elements of SVG (lines, rectangles, circles, polygons, and paths)

Introduction – general knowledge

SVG, an XML markup language, is designed to describe two-dimensional vector graphics. The SVG markup language resides in the DOM as a node that describes exactly how to draw a shape (a curve, line, circle, and polygon). Just like HTML, SVG tags can also be styled from standard CSS. Note that, because all commands reside in the DOM, the more shapes you have, the more nodes you have and the more work for the browser. This is important to remember because, as SVG visualizations become more complex, the less fluidly they will perform.

The main SVG node is declared as follows:

<svg width="200" height="200"></svg>

This node's basic properties are width and height; they provide the primary container for the other nodes that make up a visualization. For example, if you wanted to create ten sequential circles in a 200 x 200 box, the tags would look like this:

<?xml version="1.0"?>
<svg width="200&quot...

Positioning elements

What about position? Where do these primitives draw inside the SVG element? What if you wanted to put a circle on the top left and another one on the bottom right? Where do you start?

SVG is positioned by a grid system, similar to the Cartesian coordinate system. However, in SVG (0,0) is the top-left corner. The x axis proceeds horizontally from left to right starting at 0. The y axis also starts at 0 and extends downward. See the following illustration:

Positioning elements

What about drawing shapes on top of each other? How do you control the z index? In SVG, there is no z coordinate. Depth is determined by the order in which the shape is drawn. If you were to draw a circle with coordinates (10,10) and then another one with coordinates (10,10), you would see the second circle drawn on top of the first.

The following sections will cover the basic SVG primitives for drawing shapes and some of their most common attributes.

Line

The SVG line is one of the simplest in the library. It draws a straight line from one point to another. The syntax is very straightforward and can be experimented with at http://localhost:8080/chapter-2/line.html, assuming the http-server is running:

<line x1="10" y1="10" x2="100" y2="100" stroke-width="1" stroke="red"/>

This will give you the following output:

Line

A description of the element's attributes is as follows:

  • x1 and y1: The starting x and y coordinates
  • x2 and y2: The ending x and y coordinates
  • stroke: This gives the line a red color
  • stroke-width: This denotes in pixels the width of the line to be drawn

The line tag also has the ability to change the style of the end of the line. For example, adding the following would change the image so it has round endings:

stroke-linecap: round;

As stated earlier, all SVG tags can also be styled with CSS elements. An alternative way of producing the same graphic would be to first...

Rectangle

The basic HTML code to create a rectangle is as follows:

<rect width="100" height="20" x="10" y="10"></rect>

Let's apply the following style:

      rect {
        stroke-width: 1;
        stroke:steelblue;
        fill:#888;
        fill-opacity: .5;
      }

We will create a rectangle that starts at the coordinates (10,10), and is 100 pixels wide and 20 pixels high. Based on the styling, it will have a blue outline, a gray interior, and will appear slightly opaque. See the following output and example http://localhost:8080/chapter-2/rectangle.html:

Rectangle

There are two more attributes that are useful when creating rounded borders (rx and ry):

<rect with="100" height="20" x="10" y="10" rx="5" ry="5"></rect>

These attributes indicate that the x and y corners will have 5-pixel curves.

Circle

A circle is positioned with the cx and cy attributes. These indicate the x and y coordinates of the center of the circle. The radius is determined by the r attribute. The following is an example you can experiment with: http://localhost:8080/chapter-2/circle.html:

<circle cx="62" cy="62" r="50"></circle>

Now type in the following code:

   circle {
        stroke-width: 5;
        stroke:steelblue;
        fill:#888;
        fill-opacity: .5;
   }

This will create a circle with the familiar blue outline, a gray interior, and half-way opaque:

Circle

Introduction – general knowledge


SVG, an XML markup language, is designed to describe two-dimensional vector graphics. The SVG markup language resides in the DOM as a node that describes exactly how to draw a shape (a curve, line, circle, and polygon). Just like HTML, SVG tags can also be styled from standard CSS. Note that, because all commands reside in the DOM, the more shapes you have, the more nodes you have and the more work for the browser. This is important to remember because, as SVG visualizations become more complex, the less fluidly they will perform.

The main SVG node is declared as follows:

<svg width="200" height="200"></svg>

This node's basic properties are width and height; they provide the primary container for the other nodes that make up a visualization. For example, if you wanted to create ten sequential circles in a 200 x 200 box, the tags would look like this:

<?xml version="1.0"?>
<svg width="200" height="200">
  <circle cx="60" cy="60" r="50...

Positioning elements


What about position? Where do these primitives draw inside the SVG element? What if you wanted to put a circle on the top left and another one on the bottom right? Where do you start?

SVG is positioned by a grid system, similar to the Cartesian coordinate system. However, in SVG (0,0) is the top-left corner. The x axis proceeds horizontally from left to right starting at 0. The y axis also starts at 0 and extends downward. See the following illustration:

What about drawing shapes on top of each other? How do you control the z index? In SVG, there is no z coordinate. Depth is determined by the order in which the shape is drawn. If you were to draw a circle with coordinates (10,10) and then another one with coordinates (10,10), you would see the second circle drawn on top of the first.

The following sections will cover the basic SVG primitives for drawing shapes and some of their most common attributes.

Line


The SVG line is one of the simplest in the library. It draws a straight line from one point to another. The syntax is very straightforward and can be experimented with at http://localhost:8080/chapter-2/line.html, assuming the http-server is running:

<line x1="10" y1="10" x2="100" y2="100" stroke-width="1" stroke="red"/>

This will give you the following output:

A description of the element's attributes is as follows:

  • x1 and y1: The starting x and y coordinates

  • x2 and y2: The ending x and y coordinates

  • stroke: This gives the line a red color

  • stroke-width: This denotes in pixels the width of the line to be drawn

The line tag also has the ability to change the style of the end of the line. For example, adding the following would change the image so it has round endings:

stroke-linecap: round;

As stated earlier, all SVG tags can also be styled with CSS elements. An alternative way of producing the same graphic would be to first create a CSS style, as shown in the following code:

      line ...

Rectangle


The basic HTML code to create a rectangle is as follows:

<rect width="100" height="20" x="10" y="10"></rect>

Let's apply the following style:

      rect {
        stroke-width: 1;
        stroke:steelblue;
        fill:#888;
        fill-opacity: .5;
      }

We will create a rectangle that starts at the coordinates (10,10), and is 100 pixels wide and 20 pixels high. Based on the styling, it will have a blue outline, a gray interior, and will appear slightly opaque. See the following output and example http://localhost:8080/chapter-2/rectangle.html:

There are two more attributes that are useful when creating rounded borders (rx and ry):

<rect with="100" height="20" x="10" y="10" rx="5" ry="5"></rect>

These attributes indicate that the x and y corners will have 5-pixel curves.

Circle


A circle is positioned with the cx and cy attributes. These indicate the x and y coordinates of the center of the circle. The radius is determined by the r attribute. The following is an example you can experiment with: http://localhost:8080/chapter-2/circle.html:

<circle cx="62" cy="62" r="50"></circle>

Now type in the following code:

   circle {
        stroke-width: 5;
        stroke:steelblue;
        fill:#888;
        fill-opacity: .5;
   }

This will create a circle with the familiar blue outline, a gray interior, and half-way opaque:

Polygon


To create a polygon, use the polygon tag. The best way to think about an SVG polygon is to compare it to a child's dot-to-dot game. You can imagine a series of dots and a pen connecting each (x,y) coordinate with a straight line. The series of dots is identified in the points attribute. Take the following as an example (http://localhost:8080/chapter-2/polygon.html):

<polygon points="60,5 10,120 115,120"/>

First, we start at 60,5 and we move to 10,120. Then, we proceed to 115,120 and finally return to 60,5 (note that the pen returns to the starting position automatically.)

Left arrow icon Right arrow icon

Description

If you are interested in creating maps for the web GIS data, this book is for you. Familiarity with D3.js will be helpful but is not necessary.

What you will learn

  • Access data resources to make maps and learn how to modify structures
  • Render your maps on a browser
  • Style your maps according to your needs and bind events to maps to make them interactive
  • Tie paths to the geospatial data to outline an SVG map
  • Use Chrome Dev Tools in order to inspect created code
  • Fetch data through AJAX calls with the assistance of the D3.js library
  • Work with data structures and compose blocks of logic into reusable functions
  • Troubleshoot your code

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Dec 29, 2014
Length: 126 pages
Edition : 1st
Language : English
ISBN-13 : 9781783985609
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Dec 29, 2014
Length: 126 pages
Edition : 1st
Language : English
ISBN-13 : 9781783985609
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 103.97
Data Visualization with D3.js Cookbook
€36.99
Learning D3.js Mapping
€20.99
Mastering D3.js
€45.99
Total 103.97 Stars icon
Banner background image

Table of Contents

8 Chapters
1. Gather Your Cartographer's Toolbox Chevron down icon Chevron up icon
2. Creating Images from Simple Text Chevron down icon Chevron up icon
3. Producing Graphics from Data – the Foundations of D3 Chevron down icon Chevron up icon
4. Creating a Map Chevron down icon Chevron up icon
5. Click-click Boom! Applying Interactivity to Your Map Chevron down icon Chevron up icon
6. Finding and Working with Geographic Data Chevron down icon Chevron up icon
7. Testing Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(7 Ratings)
5 star 71.4%
4 star 14.3%
3 star 0%
2 star 0%
1 star 14.3%
Filter icon Filter
Top Reviews

Filter reviews by




MD Jan 26, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Making maps is about clearly describing a terra incognita. As such this book is a crisp and concise exploration of making maps using D3, which itself is unknown landscape to many developers. I thoroughly enjoyed this book, especially since it was not a monolithic tome. It is just long enough to cover the subject matter in detail, and not so verbose that you need to carve out a week to read it.
Amazon Verified review Amazon
Han Jong In Feb 09, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent & Very Good..!!
Amazon Verified review Amazon
Christopher Parker Jan 12, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I wish more tech books were written like this. I have a ton of books just sitting on my shelf that are just collecting dust - I read them once and I put them aside.I was skeptical that such a short book would be if any use, but my favorite tech book are short and concise. So, I bought this on a whim. I needed to learn d3.This book is awesome. I spent the weekend working through the whole thing. The code is really well written. My goodness - it is well written! And there are test cases! The book is actually practical. Tech books like this are never practical, but this one is.Anyways, call me impressed. Super happy with this purchase.
Amazon Verified review Amazon
bdw Mar 03, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Interesting introduction to d3 mapping which provides a nice ramp up for those just beginning and advances into the more advanced topics such as data file types and map interactivity. The first part of the book discusses how to setup a development environment suitable for working with the provided code examples. Next, you'll learn to create basic maps followed by more interactive examples, which take advantage of d3's enter-update-exit lifecycle. Finally, you'll learn about transitions, filetypes and testing methodologies for use in creating quality geographic visuals. I really enjoyed the book and highly recommend it.
Amazon Verified review Amazon
Nicholas Roberts May 14, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is great! It makes something that seemed intimidating a breeze. Thomas and Oscar do a great job explaining map making in D3 and give you the tools to go out and continue learning. A little bonus is that it has the best explanation of D3's enter, update, and exit concepts that I've seen so far! Keep it up!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.