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
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Data Visualization with D3 4.x Cookbook
Data Visualization with D3 4.x Cookbook

Data Visualization with D3 4.x Cookbook: Visualization Strategies for Tackling Dirty Data , Second Edition

Arrow left icon
Profile Icon Nick Zhu
Arrow right icon
Can$12.99 Can$49.99
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.3 (3 Ratings)
eBook Feb 2017 380 pages 2nd Edition
eBook
Can$12.99 Can$49.99
Paperback
Can$61.99
Subscription
Free Trial
Arrow left icon
Profile Icon Nick Zhu
Arrow right icon
Can$12.99 Can$49.99
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.3 (3 Ratings)
eBook Feb 2017 380 pages 2nd Edition
eBook
Can$12.99 Can$49.99
Paperback
Can$61.99
Subscription
Free Trial
eBook
Can$12.99 Can$49.99
Paperback
Can$61.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Data Visualization with D3 4.x Cookbook

Chapter 2. Be Selective

In this chapter we will cover:

  • Selecting a single element
  • Selecting multiple elements
  • Iterating through a selection
  • Performing subselection
  • Function chaining
  • Manipulating raw selection

Introduction

One of the most fundamental tasks that you will need to perform with any data visualization project using D3 is selection. Selection helps you target certain visual elements on the page. If you are already familiar with the W3C-standardized CSS selector or other similar selector APIs provided by popular JavaScript libraries, such as jQuery and Zepto.js, then you will find yourself right at home with D3's selection API. Don't worry if you haven't used the selector API before; this chapter is designed to cover this topic in steps with the help of some very visual recipes. It will cover pretty much all common use cases for your data visualization needs.

Introducing selection

Selector support is standardized by W3C, so all modern web browsers have built-in support for the selector API. However, the basic W3C selector API has limitations when it comes to Web development, especially in the data visualization realm. The standard W3C selector API provides only the selector...

Selecting a single element

It is very common that at times you will need to select a single element on a page to perform some visual manipulation. This recipe will show you how to perform a targeted single element selection in D3 using a CSS selector.

Getting ready

Open your local copy of the following file in your web browser:

https://github.com/NickQiZhu/d3-cookbook-v2/blob/master/src/chapter2/single-selection.html .

How to do it...

Let's select something (a paragraph element perhaps) and produce the classic hello world on screen.

<p id="target"></p> <!-- A --> 
 
<script type="text/javascript"> 
    d3.select("p#target") // <-- B 
    .text("Hello world!"); // <-- C 
</script> 

This recipe simply produces text Hello world! on your screen.

How it works...

The d3.select command is used to perform a single-element selection in D3. This method accepts a string that represents a valid CSS3 selector or an element object...

Selecting multiple elements

Often selecting a single element is not good enough, but rather you want to apply a certain change to a set of elements on the page simultaneously. In this recipe, we will play with the D3 multi-element selector and its selection API.

Getting ready

Open your local copy of the following file in your web browser:

https://github.com/NickQiZhu/d3-cookbook-v2/blob/master/src/chapter2/multiple-selection.html .

How to do it...

This is what the d3.selectAll function is designed for. In the following code snippet, we will select three different div elements and enhance them with some CSS classes:

<div></div> 
<div></div> 
<div></div> 
 
<script type="text/javascript"> 
    d3.selectAll("div") // <-- A 
    .attr("class", "red box"); // <-- B 
</script> 

This code snippet produces the following visual:

How to do it...

Multi-element selection

How it works...

First thing you probably will notice in this...

Iterating through a selection

Sometimes it is handy to be able to iterate through each element within a selection and modify each element differently according to their position. In this recipe, we will show you how this can be achieved using D3 selection iteration API.

Getting ready

Open your local copy of the following file in your web browser:

https://github.com/NickQiZhu/d3-cookbook-v2/blob/master/src/chapter2/selection-iteration.html .

How to do it...

D3 selection object provides a simple iterator interface to perform iteration in a similar fashion to how you will iterate through a JavaScript array. In this example, we will iterate through the three selected div elements we worked with in the previous recipe and annotate them with an index number as follows:

<div></div> 
<div></div> 
<div></div> 
 
<script type="text/javascript"> 
d3.selectAll("div") // <-- A 
            .attr("class", "red box")...

Performing subselection

It is quite common that you will need to perform scoped selection when working on visualization. For example, selecting all div elements within a particular section element is one such use case of scoped selection. In this recipe, we will demonstrate how this can be achieved with different approaches and their advantages and disadvantages.

Getting ready

Open your local copy of the following file in your web browser:

https://github.com/NickQiZhu/d3-cookbook-v2/blob/master/src/chapter2/sub-selection.html .

How to do it...

The following code example selects two different div elements using two different styles of subselection supported by D3:

<section id="section1"> 
    <div> 
        <p>blue box</p> 
    </div> 
</section> 
<section id="section2"> 
    <div> 
        <p>red box</p> 
    </div> 
</section> 
 
<script type="text/javascript"> 
    d3.select("#section1...

Introduction


One of the most fundamental tasks that you will need to perform with any data visualization project using D3 is selection. Selection helps you target certain visual elements on the page. If you are already familiar with the W3C-standardized CSS selector or other similar selector APIs provided by popular JavaScript libraries, such as jQuery and Zepto.js, then you will find yourself right at home with D3's selection API. Don't worry if you haven't used the selector API before; this chapter is designed to cover this topic in steps with the help of some very visual recipes. It will cover pretty much all common use cases for your data visualization needs.

Introducing selection

Selector support is standardized by W3C, so all modern web browsers have built-in support for the selector API. However, the basic W3C selector API has limitations when it comes to Web development, especially in the data visualization realm. The standard W3C selector API provides only the selector, but not the...

Selecting a single element


It is very common that at times you will need to select a single element on a page to perform some visual manipulation. This recipe will show you how to perform a targeted single element selection in D3 using a CSS selector.

Getting ready

Open your local copy of the following file in your web browser:

https://github.com/NickQiZhu/d3-cookbook-v2/blob/master/src/chapter2/single-selection.html .

How to do it...

Let's select something (a paragraph element perhaps) and produce the classic hello world on screen.

<p id="target"></p> <!-- A --> 
 
<script type="text/javascript"> 
    d3.select("p#target") // <-- B 
    .text("Hello world!"); // <-- C 
</script> 

This recipe simply produces text Hello world! on your screen.

How it works...

The d3.select command is used to perform a single-element selection in D3. This method accepts a string that represents a valid CSS3 selector or an element object if you already...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • • Learn about D3 4.0 from the inside out and master its new features
  • • Utilize D3 packages to generate graphs, manipulate data, and create beautiful presentations
  • • Solve real-world visualization problems with the help of practical recipes

Description

Master D3.js and create amazing visualizations with the Data Visualization with D3 4.x Cookbook. Written by professional data engineer Nick Zhu, this D3.js cookbook features over 65 recipes. ? Solve real-world visualization problems using D3.js practical recipes ? Understand D3 fundamentals ? Includes illustrations, ready-to-go code samples and pre-built chart recipes

Who is this book for?

Who is this book for? úÿÿDevelopers and analysts familiar with HTML, CSS and JavaScript who want to get the most out of D3.js ? Great as a quick-reference guide for experienced data visualization developers

What you will learn

  • With Nick?s expertise, discover how to create data-driven dynamic visualizations which take advantage of JavaScript?s latest capabilities. This cookbook helps you to fully understand D3.js fundamentals and advanced techniques, so that you can make dynamic and interactive charts and graphs.

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 28, 2017
Length: 380 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786469960
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Feb 28, 2017
Length: 380 pages
Edition : 2nd
Language : English
ISBN-13 : 9781786469960
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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 Can$6 each
Feature tick icon Exclusive print discounts
$279.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 Can$6 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Can$ 283.97
Data Visualization with D3 4.x Cookbook
Can$61.99
D3.js 4.x Data Visualization
Can$55.99
Introduction to D3
Can$165.99
Total Can$ 283.97 Stars icon
Banner background image

Table of Contents

13 Chapters
1. Getting Started with D3.js Chevron down icon Chevron up icon
2. Be Selective Chevron down icon Chevron up icon
3. Dealing with Data Chevron down icon Chevron up icon
4. Tipping the Scales Chevron down icon Chevron up icon
5. Playing with Axes Chevron down icon Chevron up icon
6. Transition with Style Chevron down icon Chevron up icon
7. Getting into Shape Chevron down icon Chevron up icon
8. Chart Them Up Chevron down icon Chevron up icon
9. Lay Them Out Chevron down icon Chevron up icon
10. Interacting with Your Visualization Chevron down icon Chevron up icon
11. Using Force Chevron down icon Chevron up icon
12. Knowing Your Map Chevron down icon Chevron up icon
13. Test Drive Your Visualization Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.3
(3 Ratings)
5 star 33.3%
4 star 0%
3 star 0%
2 star 0%
1 star 66.7%
007 Nov 07, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Wow, this is the best book about d3js so far. I have very little knowledge about javascript programming and have zero knowledge about d3js library. This book explained the topic very well. I have read one other book about d3js library but this book is way better.
Amazon Verified review Amazon
lid6j86 Jan 23, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
First off let me say that the book is dense with example projects that get to the point. That being said... With the way they write the book they must not expect you to follow any of the examples or write your own practice code because they leave out swathes in each example.If you're looking for an extremely hands-off "hey just download the code we wrote and read it" then this book is for you. If you're like me and want to actually write the code as you are following along, then you will be disappointed because more than a little code, whether it be html, css, or javascript, is missing from the book. They either leave it out or just skim over it with partial code. It comes off as extremely lazy authorship in my opinion. It's not like it would have added that many extra pages to the book to add the little bits that were left out, and it would have made it far easier to follow the code and results.I'm not saying any focus should be placed on the extra code. The focus of discussion should stay specifically on the d3 content, but at least add the code.They shouldn't force you to download code from their crappy website that has all kinds of pop ups, forces registration, and attempts to track you.
Amazon Verified review Amazon
AM Aug 17, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Delivered a damaged book!!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.