Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning D3.js Mapping

You're reading from   Learning D3.js Mapping Build stunning maps and visualizations using D3.js

Arrow left icon
Product type Paperback
Published in Dec 2014
Publisher
ISBN-13 9781783985609
Length 126 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Thomas Newton Thomas Newton
Author Profile Icon Thomas Newton
Thomas Newton
Oscar Villarreal Oscar Villarreal
Author Profile Icon Oscar Villarreal
Oscar Villarreal
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

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

Creating basic SVG elements


A common operation in D3 is to select a DOM element and append SVG elements. Subsequent calls will then set the SVG attributes, which we learned about in Chapter 2, Creating Images from Simple Text. D3 accomplishes this operation through an easy-to-read functional syntax called method chaining. Let's walk through a very simple example to illustrate how this is accomplished (go to http://localhost:8080/chapter-3/example-1.html if you have the http-server running):

var svg = d3.select("body")
    .append("svg")
    .attr("width", 200)
    .attr("height", 200)

First, we select the body tag and append an SVG element to it. This SVG element has a width and height of 200 pixels. We also store the selection in a variable:

svg.append('rect')
    .attr('x', 10)
    .attr('y', 10)
    .attr("width",50)
    .attr("height",100);

Next, we use the svg variable and append a <rect> item to it. This rect item will start at (10,10) and will have a width of 50 and a height of...

lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image