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
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
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
 Newton Newton
Author Profile Icon Newton
Newton
Oscar Villarreal Oscar Villarreal
Author Profile Icon Oscar Villarreal
Oscar Villarreal
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Learning D3.js Mapping
Credits
About the Authors
About the Reviewers
www.PacktPub.com
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

Experiment 6 – dragging orthographic projections


For our last example, we will add the ability to drag our globe so that the user can spin it to the left or right. Open http://localhost:8080/chapter-5/example-6.html from the code samples and let's get started:

var dragging = function(d) {
var c = projection.rotate();
projection.rotate([c[0] + d3.event.dx/2, c[1], c[2]])

world.attr('d', path);
mexico.attr('d', path)
        .style('fill', 'lightyellow').style('stroke', 'orange');
};

Our first piece of new code is our dragging event handler. This function will be executed every time the user drags the mouse on the screen. The algorithm executes the following steps:

  1. Store the current rotation value.

  2. Update the projection's rotation based on the amount dragged.

  3. Update all the paths in the world map.

  4. Update all the paths in the map of Mexico.

The second step deserves a little more explanation. Just like the d3.behavior.zoom event handler, d3.behavior.drag exposes information about the performed action...

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 $15.99/month. Cancel anytime
Visually different images