Thông tin tài liệu
www.it-ebooks.info
Scott Murray
Interactive Data Visualization for
the Web
www.it-ebooks.info
ISBN: 978-1-449-33973-9
[?]
Interactive Data Visualization for the Web
by Scott Murray
Copyright © 2010 Scott Murray. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editor: Meghan Blanchette
Production Editor:
Copyeditor:
Proofreader: FIX ME!
Indexer:
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
March 2013: First Edition
Revision History for the First Edition:
2012-11-20 Early Release Version 1
See http://oreilly.com/catalog/errata.csp?isbn=9781449339739 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. !!FILL THIS IN!! and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
www.it-ebooks.info
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
1.
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Why data visualization? 1
Why write code? 2
Why interactive? 2
Why on the web? 3
What This Book Is 3
Who You Are 4
What This Book Is Not 5
Using Sample Code 5
Thank You 6
2.
Introducing D3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
What it Does 7
What it Doesn’t Do 8
Origins and Context 9
Alternatives 10
Easy Charts 10
Graph Visualizations 11
Geomapping 11
Almost From Scratch 11
Three-Dimensional 12
Tools built on D3 12
3.
Technology Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
The Web 13
HTML 15
Content + Structure 16
Adding Structure with Tags 17
iii
www.it-ebooks.info
Common Tags 17
Attributes 19
Classes and IDs 19
Comments 20
DOM 21
Developer Tools 21
Rendering and the Box Model 23
CSS 24
Selectors 25
Properties and Values 26
Comments 27
Referencing Styles 27
Inheritance, Cascading, and Specificity 29
JavaScript 30
Hello, Console 31
Variables 31
Mathematical Operators 36
Comparison Operators 37
Control Structures 37
Functions 39
Comments 40
Referencing Scripts 40
JavaScript Gotchas 41
SVG 45
The SVG Element 45
Simple Shapes 46
Styling SVG Elements 48
Layering and Drawing Order 50
Transparency 50
A Note on Compatibility 53
4.
Setup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Downloading D3 55
Referencing D3 56
Setting up a Web Server 57
1. MAMP, WAMP, & LAMP 57
2. Terminal + Python 58
Diving In 58
5.
Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Generating Page Elements 59
Chaining Methods 61
iv | Table of Contents
www.it-ebooks.info
One Link at a Time 61
The Hand-off 62
Going Chainless 62
Binding data 62
In a Bind 63
Data 63
Please Make Your Selection 66
Bound and Determined 67
Using your data 69
High-functioning 70
Data Wants to be Held 71
Beyond Text 72
6. Drawing with Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Drawing divs 75
Setting Attributes 76
A Note on Classes 77
Back to the Bars 77
Setting Styles 78
The Power of data() 79
Random Data 81
Drawing SVGs 83
Create the SVG 83
Data-driven Shapes 84
Pretty Colors, Oooh! 86
Making a Bar Chart 86
The Old Chart 87
The New Chart 87
Color 93
Labels 94
Making a Scatterplot 97
The Data 97
The Scatterplot 98
Size 99
Labels 100
Next Steps 101
7.
Scales. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Apples and Pixels 103
Domains and Ranges 104
Normalization 105
Creating a Scale 105
Table of Contents | v
www.it-ebooks.info
Scaling the Scatterplot 106
d3.min() and d3.max() 106
Seting up Dynamic Scales 107
Incorporating Scaled Values 108
Refining the Plot 109
Other Methods 112
Other Scales 113
8.
Axes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Introducing Axes 116
Setting up an Axis 116
Cleaning it Up 118
Check for Ticks 121
Y Not? 122
Final Touches 124
Formatting Tick Labels 126
9.
Updates, Transitions, and Motion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Modernizing the Bar Chart 129
Ordinal Scales, Explained 130
Round Bands Are All the Range These Days 132
Referencing the Ordinal Scale 133
Other Updates 133
Updating Data 133
Interaction via Event Listeners 134
Changing the Data 135
Updating the Visuals 136
Transitions 139
duration(), or How Long is This Going to Take? 140
ease()-y Does It 141
Please Do Not delay() 142
Randomizing the Data 144
Updating Scales 146
Updating Axes 149
each() Transition Starts and Ends 150
Other Kinds of Data Updates 158
Adding Values (and Elements) 158
Removing Values (and Elements) 163
Data Joins With Keys 167
Add and Remove: Combo Platter 173
Recap 174
vi | Table of Contents
www.it-ebooks.info
Preface
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements
such as variable or function names, databases, data types, environment variables,
statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter‐
mined by context.
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
vii
www.it-ebooks.info
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this
book in your programs and documentation. You do not need to contact us for permis‐
sion unless you’re reproducing a significant portion of the code. For example, writing a
program that uses several chunks of code from this book does not require permission.
Selling or distributing a CD-ROM of examples from O’Reilly books does require per‐
mission. Answering a question by citing this book and quoting example code does not
require permission. Incorporating a significant amount of example code from this book
into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title,
author, publisher, and ISBN. For example: “Book Title by Some Author (O’Reilly). Copy‐
right 2012 Some Copyright Holder, 978-0-596-xxxx-x.”
If you feel your use of code examples falls outside fair use or the permission given above,
feel free to contact us at permissions@oreilly.com.
Safari® Books Online
Safari Books Online is an on-demand digital library that delivers ex‐
pert content in both book and video form from the world’s leading
authors in technology and business.
Technology professionals, software developers, web designers, and business and creative
professionals use Safari Books Online as their primary resource for research, problem
solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organi‐
zations, government agencies, and individuals. Subscribers have access to thousands of
books, training videos, and prepublication manuscripts in one fully searchable database
from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Pro‐
fessional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John
Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT
Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technol‐
ogy, and dozens more. For more information about Safari Books Online, please visit us
online.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
viii | Preface
www.it-ebooks.info
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at http://www.oreilly.com/catalog/<catalog page>.
To comment or ask technical questions about this book, send email to bookques
tions@oreilly.com.
For more information about our books, courses, conferences, and news, see our website
at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
Preface | ix
www.it-ebooks.info
[...]... JavaScript library for creating data visuali‐ zations But that kind of undersells it The abbreviation D3 references the tool’s full name, Data- Driven Documents The da‐ ta is provided by you, and the documents are web- based documents, meaning anything that can be rendered by a web browser, like HTML and SVG D3 does the driving, in the sense that it connects the data to the documents Of course, the name also... www.it-ebooks.info | 17 • — The standard document type declaration Must be the first thing in the document • html — Surrounds all HTML content in a document • head — The document head contains all metadata about the document, such as its title and any references to external stylesheets and scripts • title — The title of the document Browsers typically display this at the top of the browser window,... representations For that reason, data visualization is a powerful exercise Visualizing data is the fastest way to communicate it to others Of course, visualizations, like words, can be used to lie, mislead, or distort the truth But when practiced honestly and with care, the process of visualization can help us see the world in a new way, revealing unexpected patterns and trends in the otherwise-hidden information... same server again, once per file, requesting the additional infor‐ mation 7 The server responds, dispatching each file as needed 8 Finally, all the web documents have been transferred over Now the client performs its most arduous task, which is to render the content It first parses through the HTML, to understand the structure of the content Then it reviews the CSS selectors, applying any properties... interfaces, some visualiza‐ tions can make exploring data feel more like playing a game Interactive visualization can be a great medium for reaching those who don’t consider learning to be a fun task Why on the web? Visualizations aren’t truly visual unless they are seen Getting your work out there for others to see is critical, and publishing on the web is the quickest way to reach a global audience Best... • The domain name of the resource, such as calmingmanatee.com • Any additional locating information, such as the path of the requested file, or any query parameters A complete URL, then, may look like: http://alignedleft.com/tutorials/d3/ Note that the protocol is separated from the domain name by a colon and two forward (regular) slashes Why two slashes? No reason The inventor of the web regrets the. .. doesn’t hide your original data Since D3 code is executed on the client-side (meaning, in the user’s web browser, as opposed to on the web server), the data you want visualized must be sent to the client If your data can’t be shared, then don’t use D3 Alternatives include using proprietary tools (like Flash) or pre-rendering visualizations as static images and sending those to the browser (If you’re not... its website is one of the most beautiful on the Internet, and their demos are unbelievable (Go play with them now.) Raphaël — Another library for drawing vector graphics to canvas Popular due to its friendly syntax and support for older browsers Three-Dimensional D3 is not the best at 3D, simply because web browsers are historically two-dimensional beasts But with increased support for WebGL, there... stands for HyperText Transfer Protocol, and it’s the most common protocol for transferring web content from server to client The “S” on the end of HTTPS stands for Secure HTTPS connections are used whenever information should be encrypted in transit, such as for online banking or e-commerce Let’s briefly step through the process of what happens when a normal person goes to visit a website 1 User runs the. .. acknowledges the connection and is taking requests (“I’ll be here all night.”) 4 The browser sends a request for the page that lives at /tutorials/d3/ 5 The server sends back the HTML content for that page 6 As the client browser receives the HTML, it discovers references to other files needed to assemble and display the entire page, including CSS stylesheets and image files So it contacts the same server . www.it-ebooks.info
Scott Murray
Interactive Data Visualization for
the Web
www.it-ebooks.info
ISBN: 978-1-449-33973-9
[?]
Interactive Data Visualization for the Web
by Scott. practices — data visualization,
interactive design, and web development — using D3, a powerful tool for custom, web-
based visualization.
Why on the web? |
Ngày đăng: 23/03/2014, 02:20
Xem thêm: Interactive Data Visualization for the Web doc, Interactive Data Visualization for the Web doc, MAMP, WAMP, & LAMP, Chapter 9. Updates, Transitions, and Motion