-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathd3-01.html
546 lines (480 loc) · 20.5 KB
/
d3-01.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Tutorial</title>
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
<meta name="author" content="Hakim El Hattab">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="reveal.js/css/theme/black.css" id="theme">
<link rel="stylesheet" href="style.css">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="highlight.js/src/styles/obsidian.css" id="highlight-theme">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write('<link rel="stylesheet" href="reveal.js/css/print/' + (window.location.search.match(/print-pdf/gi) ? 'pdf' : 'paper') + '.css" type="text/css" media="print">');
</script>
<script src="js/jquery.js"></script>
<script src="js/d3.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-55386037-1', 'auto');
ga('send', 'pageview');
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section class="center">
<h1>
<small>D3.js Tutorial</small>
</h1>
<h4>First Part</h4>
<br/>
<p>
<small>
A tutorial by <a href="http://homes.cs.washington.edu/~domoritz/">Dominik Moritz</a> (<a href="http://twitter.com/domoritz">@domoritz</a>) and <a href="http://kanitw.yellowpigz.com/">Kanit "Ham" Wongsuphasawat</a> (<a href="http://twitter.com/kanitw">@kanitw</a>)
</small>
</p>
<p>
<small>
<a href="http://idl.cs.washington.edu">Interactive Data Lab</a>,
<a href="http://uw.edu">University of Washington</a>
</small>
</p>
<p><small><small>(Based on <a href="http://vadim.ogievetsky.com/IntroD3">Vadim Ogievetsky</a> and <a href="http://alignedleft.com/tutorials/d3/">Scott Murray</a>'s work)</small></small></p>
</section>
<section data-markdown>
## Outline
* Examples
* Selections & Data
* Transitions
* Nested Selections
Part 2: Interactions, scales, axes, data, layouts
</section>
<section>
<section>
<h2>Data-Driven Documents (D3)</h2>
</section>
<section>
<h2>What D3 is <span class="red">not</span></h2>
<ul>
<li><span class="red">Not</span> a chart library (but it can make charts)</li>
<li><span class="red">Not</span> a compatibility layer</li>
<li><span class="red">Not</span> (only) about SVG or HTML or Canvas</li>
</ul>
</section>
<section>
<h2>D3 is Data ↦ Elements</h2>
<ul>
<li>Visualizing Data with Web Standards (HTML/SVG)</li>
<li>Data ↦ Elements – constructing the DOM from Data</li>
<li>In visualization, each data point has a corresponding element (graphical marks). D3 helps you maintain this mapping!</li>
</ul>
</section>
<section class="center">
<h3>Show Reels</h3>
<img src="img/d3.png" style="max-width: 600px; max-height: 400px;">
<p>
<a href="http://vis.stanford.edu/jheer/talks/VisWeek2012/#1">See</a>
</p>
</section>
</section>
<section>
<section>
<h2>Selections</h2>
<p>D3 uses <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors">CSS Selectors</a></p>
<small>Single selector:</small>
<pre><code class="css" data-trim>
#foo // <any id="foo"> </any>
foo // <foo> </foo>
.foo // <any class="foo"> </any>
[foo=bar] // <any foo="bar"> </any>
foo bar // <foo><bar> </foo></bar>
</code></pre>
<br>
<small>Multiple selectors:</small>
<pre><code class="css" data-trim>
foo.bar // <foo class="bar"> </foo>
foo#bar // <foo id="bar"> </foo>
</code></pre>
</section>
<section>
<h3><code>.select()</code></h3>
<div class="left">
<p data-height="400" data-theme-id="0" data-slug-hash="GJKxYZ" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="GJKxYZ" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3><code>.select()</code> + <code>.attr()</code>, <code>.style()</code></h3>
<p>Select and modify element properties</p>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="LVPdgK" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="LVPdgK" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
<p><small>Note: notice chained syntax, object as argument</small></p></footer>
</section>
<section>
<h3>but <code>.select()</code> only selects one item!</h3>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="RPbMdr" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="RPbMdr" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3><code>.selectAll()</code></h3>
<!-- <p><code>.attr()</code>'s value can accept <code>function</code> with arguments <code>data, index</code></p> -->
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="XbrEOy" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="XbrEOy" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
<p class="see-also">
<small>See also: http://mbostock.github.io/d3/tutorial/circle.html</small>
</p>
</section>
</section>
<section>
<section>
<h2><code>.data()</code></h2>
<h4>Data ↦ Attributes</h4>
</section>
<section>
<h3><code>.data()</code></h3>
<p>Tell D3 that you want the selected elements to correspond to data!</p>
<div class="leftright">
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="bdbvJr" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="bdbvJr" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
</div>
<div class="note">
<small>See in console that the selected rect selection object is an array of array that embed data in <code>__data__</code> property!</small>
</div>
</section>
<section>
<h3>Selection Object</h3>
<ul>
<li>An array of arrays</li>
<li>if <code>.data()</code> is called, <code>__data__</code> of each element will contain the data mapped to that element.</li>
</ul>
<img src="img/__data__.png" height="60" width="155" alt="">
<p><small>You should have seen this from last slide!</small></p>
</section>
<section>
<h3><div class="code">.data()</div></h3>
<p>Data can be objects too!</p>
<div class="left">
<p data-height="510" data-theme-id="0" data-slug-hash="jPNzRZ" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="jPNzRZ" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3><code>.data()</code></h3>
<h4>What if number of data doesn't match number of items selected?</h4>
<div class="left">
<p data-height="510" data-theme-id="0" data-slug-hash="JdPLwq" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="JdPLwq" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section class="center">
<h3>Thinking with Join</h3>
<style>
#join-svg circle {
fill: none;
fill-opacity: .6;
stroke: black;
stroke-width: 1.5px;
}
#join-svg text{
font-size: 20px;
fill:white;
}
</style>
<br/><br/>
<svg id="join-svg" width="720" height="240"><g transform="translate(0,128)"><g transform="translate(300)"><circle r="110" style="fill: #318be1;"></circle><text y="-120" dy=".35em" text-anchor="middle" style="font-weight: bold;">Data</text><text x="-50" dy=".35em" text-anchor="middle">Enter</text></g><text x="360" dy=".35em" text-anchor="middle">Update</text><g transform="translate(420)"><circle r="110" style="fill: #FF7A00;"></circle><text y="-120" dy=".35em" text-anchor="middle" style="font-weight: bold;">Elements</text><text x="50" dy=".35em" text-anchor="middle">Exit</text></g></g></svg>
<div style="text-align: left">
<p>Calling <code>.data()</code> creates three arrays:</p>
<ul>
<li><code>enter</code> (Data without corresponding DOM elements)</li>
<li><code>update</code> (DOM elements mapped to data)</li>
<li><code>exit</code> (DOM elements now missing data)</li>
</ul>
</p><code>.data()</code> returns the update.</p>
<br/>
<p class="see-also">
<small><a href="http://bost.ocks.org/mike/join/">http://bost.ocks.org/mike/join/</a></small>
</p>
</div>
</section>
<section>
<h3><code>.enter()</code></h3>
<div class="left">
<p data-height="510" data-theme-id="0" data-slug-hash="Fawcj" data-user="kanitw" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="Fawcj" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3><code>.enter()</code></h3>
<p>Shorter: append, then update all together!</p>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="kvCjf" data-user="kanitw" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="kvCjf" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3><code>.enter()</code> from scratch</h3>
<p>This is a common way that people build visualization in D3!</p>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="kvCjf" data-user="kanitw" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="kvCjf" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3><code>.exit()</code></h3>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="maGjx" data-user="kanitw" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="maGjx" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
</section>
<section>
<section>
<h2>Transitions</h2>
</section>
<section>
<h3><code>.transition()</code></h3>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="zGOjOb" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="zGOjOb" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
<p><small>Note: Transitions in D3 use <i>cubic-in-out</i> as default easing functions. But there are <a href="https://github.com/mbostock/d3/wiki/Transitions#wiki-d3_ease">others</a>.</small></p>
</section>
<section>
<h3>Nested <code>.transition()</code> & <code>.delay()</code></h3>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="xGKjKo" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="xGKjKo" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>Mapping Data to Elements</h3>
<p>The default is index mapping.</p>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="DwyLs" data-user="kanitw" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="DwyLs" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h3>Join key – <code>.data(...,key)</code></h3>
<p>A key should be a unique string.</p>
<div class="left">
<p data-height="400" data-theme-id="0" data-slug-hash="FHtjf" data-user="kanitw" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="FHtjf" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
<p class="see-also">
<small>http://bost.ocks.org/mike/selection/#key</small>
</p>
</section>
<section>
<h3>Join key – <code>.data(...,key)</code></h3>
<p>Notice how you can save lines of code with <code>selection.call()</code></p>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="mJbxGV" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="mJbxGV" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
<p class="see-also">
<small>http://bost.ocks.org/mike/selection/#key</small>
</p>
</section>
<section>
<h3>Join key</h3>
<p>New Data?</p>
<div class="left">
<p data-height="400" data-theme-id="0" data-slug-hash="GsKnv" data-user="kanitw" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="GsKnv" data-user="kanitw" data-default-tab="result" class='codepen'>
</p>
</div>
<p class="see-also"><small>See even better <a href="http://mbostock.github.io/d3/tutorial/bar-2.html">bars transition</a>.</small></p>
</section>
<section>
<h3>See also</h3>
<ul>
<li>
General Update Pattern <a href="http://bl.ocks.org/mbostock/3808218">I</a>, <a href="http://bl.ocks.org/mbostock/3808221">II</a>, <a href="http://bl.ocks.org/3808234">III</a></h4>
</li>
<li>
<a href="http://mbostock.github.io/d3/tutorial/circle.html">Three Little Circles</a>
</li>
<li><a href="http://bost.ocks.org/mike/transition/">Working with Transitions</a></li>
</ul>
</section>
</section>
<section>
<section>
<h2>Nested Selections</h2>
</section>
<section>
<h3>Nested Selections</h3>
<div class="left">
<p data-height="500" data-theme-id="0" data-slug-hash="PqYeMB" data-user="domoritz" data-default-tab="js" class='codepen'>
</p>
</div>
<div class="right">
<p data-height="400" data-theme-id="0" data-slug-hash="PqYeMB" data-user="domoritz" data-default-tab="result" class='codepen'>
</p>
</div>
</section>
<section>
<h4>See also: <a href="http://bost.ocks.org/mike/selection/">How Selection Works</a></h4>
<iframe src="http://bost.ocks.org/mike/selection/" width="100%" height='450'></iframe>
</section>
<section>
<h3>"<a href="http://prcweb.co.uk/lab/selection/">Two Tables</a>"</h3>
<iframe class="white" src="http://prcweb.co.uk/lab/selection/" width="100%" height='400'></iframe>
</section>
<section data-markdown>
### More examples
* [bl.ocks.org/mbostock/3887051](http://bl.ocks.org/mbostock/3887051)
* [bost.ocks.org/mike/miserables/](http://bost.ocks.org/mike/miserables/)
</section>
</section>
<section>
<h2><a href="d3-02.html">2nd part of D3.js tutorial</a></h2>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
slideNumber: true,
transitionSpeed: 'fast',
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'slide', // default/cube/page/concave/zoom/linear/fade/none
// Parallax scrolling
// parallaxBackgroundImage: 'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg',
// parallaxBackgroundSize: '2100px 900px',
// Optional libraries used to extend on reveal.js
dependencies: [{
src: 'reveal.js/lib/js/classList.js',
condition: function() {
return !document.body.classList;
}
}, {
src: 'reveal.js/plugin/markdown/marked.js',
condition: function() {
return !!document.querySelector('[data-markdown]');
}
}, {
src: 'reveal.js/plugin/markdown/markdown.js',
condition: function() {
return !!document.querySelector('[data-markdown]');
}
}, {
src: 'reveal.js/plugin/highlight/highlight.js',
async: true,
callback: function() {
hljs.initHighlightingOnLoad();
}
}, {
src: 'reveal.js/plugin/zoom-js/zoom.js',
async: true,
condition: function() {
return !!document.body.classList;
}
}, {
src: 'reveal.js/plugin/notes/notes.js',
async: true,
condition: function() {
return !!document.body.classList;
}
}]
});
</script>
<script async src="//codepen.io/assets/embed/ei.js"></script>
<!-- <script src="http://static.jsbin.com/js/embed.js"></script> -->
</body>
</html>