-
Notifications
You must be signed in to change notification settings - Fork 1
/
week6b.html
462 lines (404 loc) · 19.4 KB
/
week6b.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>6b: Intro to D3</title>
<meta name="description" content="02.526 Intro to D3">
<meta name="author" content="Chan Chi-Loong, V/R">
<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="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/night.css" id="theme">
<link rel="stylesheet" href="css/style.css">
<style>
</style>
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section data-state="alert" data-transition="zoom">
<h1 class="shadowfont white">6b: Intro to D3</h1>
</section>
<section>
<h2>Objectives</h2>
<ul>
<li>Discuss libraries beyond D3.</li>
<li>Go through selection and data binding. Also: axes, domains and ranges.</li>
<li>Put together a simple bar / scatterplot chart.</li>
</ul>
</section>
<section>
<h3>Why <a href="https://d3js.org/" target="_blank">D3</a>?</h3>
<h4>(Data Driven Documents)</h4>
<p>It is a comprehensive framework for building any sort (almost literally) of interactive visualizations for the web.</p>
<p>It is however, not a simple high-level graphing template library.</p>
<p>One of the main design patterns in this library is <b>binding</b> the data to DOM elements.</p>
<hr/>
<p class="smallfont">Reading: <a href="https://medium.com/@Elijah_Meeks/d3-is-not-a-data-visualization-library-67ba549e8520" target="_blank">D3 is not a Data Visualization Library</a>, Elijah Meeks</p>
</section>
<section>
<h3>How to learn D3</h3>
<p><a href="https://wattenberger.com/blog/d3" target="_blank">How to learn D3.js</a>, Amelia Watterberger</p>
<p><a href="https://www.d3-graph-gallery.com/" target="_blank">D3 Graph Gallery</a>, Yan Holtz</p>
<p><a href="https://observablehq.com/" target="_blank">ObservableHQ</a></p>
</section>
<section>
<h3>Sample libraries built on top of D3</h3>
<ul>
<li><a href="https://vega.github.io/vega-lite/" target="_blank">Vega 5.6.1</a></li>
<li><a href="https://nvd3.org/" target="_blank">NVD3</a></li>
<li><a href="https://c3js.org/" target="_blank">C3</a></li>
<li><a href="https://observablehq.com/@observablehq/plot" target="_blank">Observable Plot</a> (<a href="https://github.com/observablehq/plot/blob/main/CHANGELOG.md" target="_blank">v0.6.13</a>)</li>
</ul>
</section>
<section>
<h3>Other charting libraries</h3>
<ul>
<li><a href="https://echarts.apache.org/en/index.html" target="_blank">Apache e-charts</a></li>
<li><a href="https://www.chartjs.org/" target="_blank">Chart.js</a></li>
<li><a href="https://developers.google.com/chart/" target="_blank">Google Charts</a></li>
</ul>
</section>
<section>
<h3>Animation and 3D libraries</h3>
<p>Animation</p>
<ul>
<li><a href="https://greensock.com/gsap/" target="_blank">GSAP</a></li>
<li><a href="https://animejs.com/" target="_blank">Anime.js</a></li>
</ul>
<hr/>
<p>3D</p>
<ul>
<li><a href="https://threejs.org/" target="_blank">three.js</a></li>
<li><a href="https://www.babylonjs.com/" target="_blank">Babylon</a></li>
</ul>
</section>
<section>
<h3>Utility Libraries</h3>
<ul>
<li><a href="https://lodash.com/" target="_blank">Lodash / Underscore</a></li>
<li><a href="https://pbeshai.github.io/tidy/" target="_blank">Tidy.js</a></li>
<li><a href="https://www.papaparse.com/" target="_blank">Papaparse</a></li>
</ul>
</section>
<section>
<h3>D3 installation / data binding</h3>
<p>I'll be including D3 the easiest way - embedding as a script pulled from a CDN. You can of course use npm / <b>import</b> and design a more modern way of scaffolding your projects.</p>
<p>One thing to note is that for <b>reactivity</b>, we'll be using D3's default data binding functions.</p>
<p>If you use a JS framework / compiler — Vue, React, Angular, Svelte, etc. — it is a good idea that your data binding does not clash with D3's one.</p>
</section>
<section data-state="alert" data-transition="zoom">
<h1 class="shadowfont white">D3 code workshop</h1>
</section>
<section>
<h2>Hello World</h2>
<p>Let's start with a simple template. We'll go through <b>selections</b>, which is a core component of D3.</p>
<pre><code>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
d3.select("body").append("p").text("Hello World!");
</script>
</body>
</html>
</code></pre>
<hr/>
<p class="smallfont">Note: We're appending a paragraph tag of "Hello World" to the body.</p>
</section>
<section>
<h2>Hello Worlds</h2>
<pre><code>
<div id="hellos">
<p>Hello World!</p>
<p>Hello World 2!</p>
<p>Hello World 3!</p>
</div>
<script>
d3.select("#hellos p").style("color", "green");
d3.selectAll("#hellos p").style("color", "green");
</script>
</code></pre>
<hr/>
<p class="smallfont">Notice the difference between the select and selectAll statements.</p>
<p class="smallfont">It's similar to JQuery (or other modern frameworks) in that you can manipulate any DOM element. Try d3.select("body").style("background-color", "red")</p>
</section>
<section>
<h3>Data Binding</h3>
<pre><code>
let data = [10, 20, 30];
let hellos = d3.select("#hellos");
hellos.selectAll("p")
.data(data)
.enter()
.append("p")
.text("Hello World");
</code></pre>
<hr/>
<p class="smallfont">Notice the design pattern: Select a bunch of elements (that don't exist), bind that to the data, and then append said elements.</p>
<p class="smallfont">One style to write javascript is to use <a href="https://medium.com/backticks-tildes/understanding-method-chaining-in-javascript-647a9004bd4f" target="_blank">method chaining</a>, which is a design pattern you will see in many javascript libraries, like JQuery or Lodash. D3 also follows this style, and it makes your code more readable.</p>
</section>
<section>
<h3>Slightly more complex example</h3>
<pre><code>
let data = [
{text: "Hello World 1", size: 10},
{text: "Hello World 2", size: 20}
]
let hellos = d3.select("#hellos");
hellos.selectAll("p")
.data(data)
.enter()
.append("p")
.style("font-size", d => d.size + "px")
.text(d => d.text)
</code></pre>
<hr/>
<p class="smallfont">Notice the use of arrow notation (ES6) to write anonymous functions.</p>
</section>
<section>
<h3>Exercise 1</h3>
<pre><code>
let data = [
{text: "Hello World 1", size: 10, color: "green", font: "Courier New"},
{text: "Hello World 2", size: 20, color: "blue", font: "Arial"},
{text: "Hello World 3", size: 30, color: "red", font: "Times New Roman"}
];
</code></pre>
<p class="smallfont">Given this dataset, can you print out each individual text, and style the elements?</p>
</section>
<section>
<h3>Fun with transitions</h3>
<pre><code>
hellos.selectAll("p")
.data(data)
.enter()
.append("p")
.text(d => d.text)
.transition()
.duration((d,i) => i * 1000)
.style("font-size", d => d.size + "px")
.style("color", d => d.color);
</code></pre>
</section>
<section>
<section>
<h3>Recap: SVG</h3>
<h4>Exercise 1b</h4>
<p class="smallfont">Remember how to draw shapes using SVGs?</p>
<pre><code>
<svg width="1000" height="1000"></svg>
</code></pre>
<hr/>
<pre><code>
let data = [
{cx: 400, cy: 600, r:150, fill: "green", stroke: "black"},
{cx: 100, cy: 200, r:50, fill: "blue", stroke: "purple"},
{cx: 650, cy: 100, r:100, fill: "red", stroke: "orange"}
]
</code></pre>
<p class="smallfont">Given this dataset, can you add these as circles to the svg, with the appropriate theming?</p>
</section>
<section>
<h4>Exercise 1b hints</h4>
<pre><code>
d3.select("svg").selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", d => d.cx)
...
</code></pre>
<hr/>
<p class="smallfont">Fill in the rest.</p>
<p class="smallfont">Set stroke-width to be 5 so the stroke is more visible.</p>
</section>
</section>
<section>
<h3>Coordinate systems: Chart to SVG</h3>
<img src="img/chart_to_svg.jpg">
</section>
<section>
<h3>Domains and ranges</h3>
<p class="smallfont">Let's say we want to draw our 1000 x 1000 chart data size onto an SVG that is 600 x 600</p>
<p class="smallfont">How do we map our quantitative data scale to the SVG drawing scale? Use <b>domain / range</b> to automatically calculate the mapping.</p>
<pre><code>
let xScale = d3.scaleLinear()
.domain([0,1000])
.range([0,600]);
let yScale = d3.scaleLinear()
.domain([0,1000])
.range([600,0]);
</code></pre>
<hr/>
<p class="smallfont">More on scales and domain/range <a href="https://medium.com/@sahilaug/line-graphs-using-d3-drawing-the-axes-8ffc0076a8be" target="_blank">here</a>.</p>
<p class="smallfont">Note that the my yScale is inverted here. Because for standard charts, we draw from the bottom-left up rather than top-left down.</p>
</section>
<section>
<h3>Setup Dimensions / Margins</h3>
<pre><code>
let margin = {top: 20, right: 20, bottom: 40, left: 40},
width = 600 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
let chart = d3.select("svg")
.attr("width", 600)
.attr("height", 600)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
let xScale = d3.scaleLinear()
.domain([0,1000])
.range([0,width]);
let yScale = d3.scaleLinear()
.domain([0,1000])
.range([height,0]);
chart.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", d => xScale(d.cx))
.attr("cy", d => yScale(d.cy))
.attr("r", d => d.r)
.attr("fill", d => d.fill);
</code></pre>
</section>
<section>
<h3>Add axes</h3>
<pre><code>
chart
.append("g")
.call(d3.axisBottom(xScale));
chart
.append("g")
.call(d3.axisLeft(yScale));
</code></pre>
<hr/>
<p class="smallfont">Axes are added to origin point (0,0). Use a transform to translate the xAxis to the bottom of the chart.</p>
<pre><code>
chart
.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale));
</code></pre>
</section>
<section>
<h3>Add labels</h3>
<pre><code>
chart
.append("text")
.attr("transform", "translate(" + (width / 2) + "," + (height + 40) + ")")
.text("X-axis label")
.attr("text-align", "middle");
chart
.append("text")
.attr("transform", "translate(-40, " + (height / 2) + ") rotate(-90)")
.text("Y-axis label")
.attr("text-align", "middle");
</code></pre>
</section>
<section>
<h3>Add colorscale</h3>
<p>Let's map radius to a sequential colorscale.</p>
<pre><code>
let colorScale = d3.scaleLinear()
.domain([0, 300])
.range([0,1]);
</code></pre>
<hr/>
<pre><code>
...
.attr("fill", d => d3.interpolateViridis(colorScale(d.r)));
</code></pre>
</section>
<section>
<h3>Event firing</h3>
<h4>Mouseover / mouseout</h4>
<pre><code>
...
.on("mouseover", (event, d) => {
d3.select(event.currentTarget)
.attr("stroke", "black")
.attr("stroke-width", 2);
})
.on("mouseout", (event, d) => {
d3.select(event.currentTarget)
.attr("stroke", "none");
})
</code></pre>
<hr/>
<p class="smallfont">Do note that this event firing is syntax for the latest D3 v7. <a href="https://observablehq.com/@d3/d3v6-migration-guide" target="_blank">Migration docs here</a>. Syntax for event firing is different for previous versions.</p>
</section>
<section>
<h3>Clean-up / Ideas</h3>
<h4>Things to consider</h4>
<ol>
<li>Group, order, and label things for you to understand if you need to inspect the SVG</li>
<li>Consider moving your theming to CSS as this is easier to change.</li>
<li>Consider adding legends, labels, etc.</li>
<li>Consider adding interactivity for tooltips and feedback.</li>
<li>You can make the SVG form factor responsive if needed.</li>
<li>Transitions for the bling.</li>
</ol>
</section>
<section>
<h2>Basic D3 bar chart</h2>
<a href="examples/barchart1.html" target="_blank"><img width="600" src="img/preview1.png"/></a>
</section>
<section>
<h2>D3 bar chart 2</h2>
<p>Transitions. Enter, update and exit design pattern</p>
<a href="examples/barchart2.html" target="_blank"><img width="600" src="img/preview2.png"/></a>
</section>
<section>
<h2>Final thoughts</h2>
<p>D3 is probably a little overkill for standard charts, and there are tons of higher-order, easier to use libraries out there.</p>
<p>Please note that you can use any charting library to create the charts required for the project and assignments, and D3 is not specifically required.</p>
<p>However, D3 gives you a ton of high-quality design patterns for interaction, data hierarchies and chart types.</p>
<p>From unusual chart types / interaction to code art pieces, D3 is useful. Here are <a href="https://showcase.vslashr.com/moire/" target="_blank">some</a> <a href="https://showcase.vslashr.com/virus/" target="_blank">samples</a>.</p>
</section>
<section>
<h2>Questions?</h2>
<div class="plain">
<div class="rotate-icon"><a href="http://www.vslashr.com"><img width="100" src="img/VslashR_logo_white.svg"></a></div>
</div>
<p><small>Chi-Loong | V/R</small></p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="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,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>