-
Notifications
You must be signed in to change notification settings - Fork 238
/
intro_d3js.html
1368 lines (1230 loc) · 47.8 KB
/
intro_d3js.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<!-- AdThrive Head Tag Manual -->
<script data-no-optimize="1" data-cfasync="false">
(function (w, d) {
w.adthrive = w.adthrive || {};
w.adthrive.cmd = w.adthrive.cmd || [];
w.adthrive.plugin = "adthrive-ads-manual";
w.adthrive.host = "ads.adthrive.com";
var s = d.createElement("script");
s.async = true;
s.referrerpolicy = "no-referrer-when-downgrade";
s.src =
"https://" +
w.adthrive.host +
"/sites/643436a4e6d20859e40a446b/ads.min.js?referrer=" +
w.encodeURIComponent(w.location.href) +
"&cb=" +
(Math.floor(Math.random() * 100) + 1);
var n = d.getElementsByTagName("script")[0];
n.parentNode.insertBefore(s, n);
})(window, document);
</script>
<!-- End of AdThrive Head Tag -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=UA-79254642-6"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "UA-79254642-6");
</script>
<script>
var getOutboundLink = function (url) {
gtag("event", "click", {
event_category: "outbound",
event_label: url,
transport_type: "beacon",
event_callback: function () {
document.location = url;
},
});
};
</script>
<meta charset="utf-8" />
<title>An introduction to d3.js in 10 basic examples</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta
name="description"
content="A set of 10 basic examples leading to a first chart made with d3.js. Discover the basics: html, css, svg, scale, data binding and more. Explanation and editable code provided"
/>
<meta
name="keywords"
content="Data,Dataviz,Datavisualization,Javascript, JS, d3.js, axis, x, y"
/>
<meta name="author" content="Yan Holtz" />
<link rel="icon" href="img/logo/D3_single_small.png" />
<meta
property="og:title"
content="An introduction to d3.js in 10 basic examples."
/>
<meta property="og:image" content="img/overview_RGG.png" />
<meta
property="og:description"
content="A set of 10 basic examples leading to a first chart made with d3.js. Discover the basics: html, css, svg, scale, data binding and more. Explanation and editable code provided."
/>
<!-- Bootstrap core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<!-- Custom fonts for this template -->
<link
href="vendor/font-awesome/css/font-awesome.min.css"
rel="stylesheet"
type="text/css"
/>
<!-- Custom styles for this template -->
<link href="css/agency.css" rel="stylesheet" />
<!-- PRISM -->
<script src="LIB/prism.js"></script>
<link href="LIB/prism.css" rel="stylesheet" />
<!-- D3.JS v4 -->
<script src="js/d3.v4.js"></script>
<!-- JQUERY -->
<script src="vendor/jquery/jquery.min.js"></script>
<!-- HTML TO CANVAS -->
<script src="js/html2canvas.js"></script>
<!-- Function to parse html and js code chunks made by Yan Holtz -->
<script src="js/myParser.js"></script>
<!-- Bootstrap js -->
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="js/agency.min.js"></script>
</head>
<body data-spy="scroll" data-target="#myScrollspy" data-offset="1">
<!-- THIS ALLOWS TO INSERT THE MENU THAT IS STORED IN A MENU.HTML FILE-->
<nav class="navbar navbar-expand-lg fixed-top" id="mainNav"></nav>
<script>
$(function () {
$("#mainNav").load("html_chunk/menu.html");
});
</script>
<!-- THIS ALLOWS TO INSERT THE MODAL OF THE MENU THAT IS STORED IN A MENU_MODAL.HTML FILE-->
<div id="modal_menu_insertion"></div>
<script>
$(function () {
$("#modal_menu_insertion").load("html_chunk/menu_modal.html");
});
</script>
<!-- Header -->
<header class="masthead" style="padding-top: 150px; padding-bottom: 80px">
<div class="textlanding">
<h1>An introduction to d3.js in 10 basic examples.</h1>
<hr class="short_hr" />
<br />
<ul class="list-inline social-buttons">
<li class="list-inline-item">
<a href="https://twitter.com/R_Graph_Gallery">
<i class="fa fa-twitter"></i>
</a>
</li>
<li class="list-inline-item social-buttons">
<a href="https://github.com/holtzy">
<i class="fa fa-github" style="color: white"></i>
</a>
</li>
<li class="list-inline-item social-buttons">
<a href="https://www.linkedin.com/in/yan-holtz-2477534a/">
<i class="fa fa-linkedin"></i>
</a>
</li>
</ul>
<br /><br />
<p style="max-width: 700px; margin: auto">
<a href="https://d3js.org">D3.js</a> is a
<u>JavaScript library</u> for manipulating documents based on data. It
allows to build absolutely
<a href="all.html">any type of data visualization</a>. This document
displays 10 interactive examples illustrating the key concepts of d3,
leading to a first basic scatterplot. Note that this
<a
href="https://click.linksynergy.com/deeplink?id=G7VKNifkCIY&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Flearn-d3js-for-data-visualization%2F"
>online course</a
>
is a great resource to get you started with d3.js.
</p>
</div>
</header>
<!-- TABLE of CONTENT -->
<div>
<nav class="col-sm-3 col-4" id="myScrollspy">
<ul class="nav nav-pills flex-column">
<li class="nav-item">
<a class="nav-link active" href="#html">HTML</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#css">CSS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#svg">SVG</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#d3">Javascript and d3.js</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#log">Console.log()</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#coord">Coordinate system</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#scale">Scale</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#axis">Axis</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#margin">Margin</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#data">Data binding</a>
</li>
</ul>
</nav>
</div>
<!-- ==================== HTML ==================== -->
<section id="html">
<div class="container">
<h1>What is <code>HTML</code>?</h1>
<hr />
<code>→ Explanation:</code>
<br />
<ul style="margin-top: 5px">
<li>
<a href="https://en.wikipedia.org/wiki/HTML">HTML</a> stands for
<u>Hypertext Markup Language</u>. Basically, it is the language
behind any <u>website</u>. Web browsers like Mozilla or Safari read
this kind of file and translate it in a webpage.
</li>
<li>
In a HTML file, elements composing the webpage are created,
delineated by <u>tags</u>. For instance a title of level 1 is
represented by the <code>h1</code> tag, a paragraph with the
<code>p</code> tag, an image by the <code>img</code> tag and so on.
</li>
<li>
It is impossible to create a d3.js visualization without basic
knowledge on html.
<a href="https://www.w3schools.com/html/">This tutorial</a> by
W3School can be a good starting point in my opinion.
</li>
</ul>
<br />
<code>→ Example:</code>
<div class="row">
<div
class="col-lg-5 bg-light"
style="padding: 50px"
id="res-allType"
></div>
<div class="col-lg-7">
<aside
style="position: -webkit-sticky; position: sticky; top: 120px"
>
← Edit me!
</aside>
<pre
class="language-html"
><code id="code-allType" contenteditable="true"><xmp><!DOCTYPE html>
<!-- Add a title -->
<h1>First html document</h1>
<!-- Add a bit of text -->
<p>This is my first sentence</p>
<!-- Add a link -->
<p>This is <a href="https://www.d3-graph-gallery.com">a link to the d3 graph gallery</a></p>
</xmp></code></pre>
</div>
</div>
<br /><br />
<code>→ Try:</code>
<ul>
<li>
Copy and paste the code above in a local file. Call it something
like <code>test.html</code>. Open it with a browser. You've created
your first website!
</li>
</ul>
</div>
</section>
<script>
// Read the JS fragment at the beginning. Read it again if it changes
myHtmlParser("code-allType", "res-allType");
document
.getElementById("code-allType")
.addEventListener("input", function () {
d3.select("#res-allType").html("");
myHtmlParser("code-allType", "res-allType");
});
</script>
<!-- ================================================================================= -->
<!-- ==================== CSS ==================== -->
<section id="css">
<div class="container">
<h1>Custom document style with <code>CSS</code></h1>
<hr />
<code>→ Explanation:</code>
<br />
<ul style="margin-top: 5px">
<li>
<a href="https://en.wikipedia.org/wiki/Cascading_Style_Sheets"
>CSS</a
>
stands for <u>Cascading Style Sheet</u>. It allows to apply specific
styles to the elements created using <code>html</code> before.
</li>
<li>
As for html, it is impossible to create a d3.js visualization
without basic knowledge on <code>css</code>. If it is new for you,
check this
<a href="https://www.w3schools.com/css/default.asp">tutorial</a>.
</li>
</ul>
<br />
<code>→ Example:</code>
<div class="row">
<div
class="col-lg-5 bg-light"
style="padding: 50px"
id="res-css"
></div>
<div class="col-lg-7">
<aside
style="position: -webkit-sticky; position: sticky; top: 120px"
>
← Edit me!
</aside>
<pre
class="language-html"
><code id="code-css" contenteditable="true"><xmp><!DOCTYPE html>
<!-- Apply specific style to the elements that have the class `inGreen` -->
<style>
.inGreen { color: green; }
</style>
<!-- Add a title. Note that the class 'inGreen' is given to this title -->
<h1 class="inGreen">First html document</h1>
<!-- Add a bit of text -->
<p>This is my first sentence</p>
<!-- Add a link -->
<p>This is <a href="https://www.d3-graph-gallery.com">a link to the d3 graph gallery</a></p>
</xmp></code></pre>
</div>
</div>
<br />
<code>→ Try:</code>
<ul style="margin-top: 5px">
<li>
Give the class <code>inGreen</code> to one of the paragraph
<code>p</code>
</li>
<li>
Create a new class for the first sentence of the document. Change
its font size with <code>font-size: 20px</code> in css.
</li>
</ul>
</div>
</section>
<script>
// Read the JS fragment at the beginning. Read it again if it changes
myHtmlParser("code-css", "res-css");
document
.getElementById("code-css")
.addEventListener("input", function () {
d3.select("#res-css").html("");
myHtmlParser("code-css", "res-css");
});
</script>
<!-- ================================================================================= -->
<!-- ==================== SVG ==================== -->
<section id="svg">
<div class="container">
<h1>Build shapes with <code>SVG</code></h1>
<hr />
<code>→ Explanation:</code>
<br />
<ul style="margin-top: 5px">
<li>
<a href="https://en.wikipedia.org/wiki/Scalable_Vector_Graphics"
>SVG</a
>
stands for <u>Scalable Vector Graphic</u>. It is a vector image
format. Basically, it is a language that allows to build shapes with
code.
</li>
<li>
A d3.js chart is actually a set of <code>svg</code> shapes put
together. For instant, a scatterplot is just composed by several
circles as the one shown below.
</li>
<li>
This <a href="graph/shape.html">document</a> of the d3 graph gallery
showcases the different shapes offered by Svg.
</li>
</ul>
<br />
<code>→ Example:</code>
<div class="row">
<div
class="col-lg-5 bg-light"
style="padding: 50px"
id="res-svg"
></div>
<div class="col-lg-7">
<aside
style="position: -webkit-sticky; position: sticky; top: 120px"
>
← Edit me!
</aside>
<pre
class="language-html"
><code id="code-svg" contenteditable="true"><xmp><!DOCTYPE html>
<!-- Add a title -->
<h1>First html document</h1>
<!-- Add a bit of text -->
<p>This is my first sentence</p>
<!-- Add a svg shape -->
<svg>
<circle style="fill: #69b3a2" stroke="black" cx=50 cy=50 r=40></circle>
</svg>
</xmp></code></pre>
</div>
</div>
<br />
<code>→ Try:</code>
<br />
<ul style="margin-top: 5px">
<li>
Modify the arguments of in the <code>svg</code> call to understand
what feature they control.
</li>
<li>Try to draw another type of shape using the cheat sheet below</li>
</ul>
<pre
class="language-html"
><code><xmp><line x1="0" y1="0" x2="10" y2="10" stroke="black"></line>
<rect x="0" y="0" width="10" height="10"></rect>
<circle cx="5" cy="5" r="5"></circle>
<ellipse cx="10" cy="5" rx="10" ry="5"></ellipse>
<polygon points="0,0 10,5 20,0 20,20 10,15 0,20"></polygon>
<polyline points="0,0 10,5 20,0 20,20 10,15 0,20" stroke="black"></polyline>
<path d="M65,10 a50,25 0 1,0 50,25"></path>
</xmp></code></pre>
<p style="text-align: right">
<a
style="font-size: 9px"
href="https://codepen.io/chriscoyier/pen/dXKxvv"
>Example source</a
>
</p>
</div>
</section>
<script>
// Read the JS fragment at the beginning. Read it again if it changes
myHtmlParser("code-svg", "res-svg");
document
.getElementById("code-svg")
.addEventListener("input", function () {
d3.select("#res-svg").html("");
myHtmlParser("code-svg", "res-svg");
});
</script>
<!-- ================================================================================= -->
<!-- ==================== JAVASSCRIPT AND D3 ==================== -->
<section id="d3" class="bg">
<div class="container">
<h1>
Modify elements with <code>Javascript</code> and <code>D3.js</code>
</h1>
<hr />
<code>→ Explanation:</code>
<br />
<ul style="margin-top: 5px">
<li>
<a href="https://en.wikipedia.org/wiki/JavaScript">JavaScript</a> is
one of the three core technologies of the World Wide Web. It enables
<u>interactivity</u> in webpages.
</li>
<li>
<a href="">D3.js</a> is a javascript library particularly useful for
data visualization. It allows to create, select and modify elements.
</li>
<li>
In the example below, d3 is used to select the circle with a class
<code>target</code> and modify its <code>stroke-width</code>.
</li>
<li>
It is not very impressive yet. But we will use the same kind of
process to set the position of hundreds of circle and get a
scatterplot.
</li>
</ul>
<br />
<code>→ Example:</code>
<div class="row">
<!-- ==================== GRAPH SECTION = WHERE THE GRAPH APPEARS ==================== -->
<div class="col-lg-5 .align-middle">
<div style="position: -webkit-sticky; position: sticky; top: 120px">
<div class="bg-light" id="res-basicTrigger"></div>
</div>
</div>
<!-- ==================== CODE SECTION = WHERE THE CODE APPEARS ==================== -->
<div class="col-lg-7 text-center .align-middle">
<!-- ========= show html code ============== -->
<aside
style="position: -webkit-sticky; position: sticky; top: 120px"
>
← Edit me!
</aside>
<pre
class="language-html"
><code id="html-basicTrigger" contenteditable="true"><xmp><!DOCTYPE html>
<h1>First html document</h1>
<!-- Add a bit of text -->
<p>This is my first sentence</p>
<!-- Add a svg shape. Note that the 'target' class is attributed to the circle -->
<svg>
<circle class="target" style="fill: #69b3a2" stroke="black" cx=50 cy=50 r=40></circle>
</svg>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
</xmp></code></pre>
<!-- ==================================== -->
<!-- ========= show JS code ============== -->
<pre
class="language-js"
><code id="js-basicTrigger" contenteditable="true"><xmp><script>
d3
.select(".target") // select the elements that have the class 'target'
.style("stroke-width", 8) // change their style: stroke width is not equal to 8 pixels
</script></xmp></code></pre>
<!-- ==================================== -->
<!-- ==================== JAVASCRIPT SECTION : EXECUTE THIS CODE AND MAKE IT INTERACTIVE ==================== -->
<script>
// At the beginning, I read the html and JS fragments
myHtmlParser("html-basicTrigger", "res-basicTrigger");
myJSParser("js-basicTrigger", "res-basicTrigger");
// If the user change the JS fragment, I run it again:
document
.getElementById("js-basicTrigger")
.addEventListener("input", function () {
d3.select("#res-basicTrigger").html("");
myHtmlParser("html-basicTrigger", "res-basicTrigger");
myJSParser("js-basicTrigger", "res-basicTrigger");
});
// If the user change the HTML fragment, I run it again:
document
.getElementById("html-basicTrigger")
.addEventListener("input", function () {
d3.select("#res-basicTrigger").html("");
myHtmlParser("html-basicTrigger", "res-basicTrigger");
myJSParser("js-basicTrigger", "res-basicTrigger");
});
</script>
</div>
</div>
<code>→ Try:</code>
<br />
<ul style="margin-top: 5px">
<li>
Change the circle opacity with the <code>opacity</code> style that
goes between 0 and 1.
</li>
</ul>
</div>
</section>
<br />
<!-- ==================== CONSOLE.LOG ==================== -->
<section id="coord" class="bg">
<div class="container">
<h1><code>Console.log()</code> is your friend.</h1>
<hr />
<code>→ Explanation:</code>
<br />
<ul style="margin-top: 5px">
<li>
So the browser runs <code>Html</code>, <code>Css</code> and
<code>Javascript</code> code and shows the result as a webpage.
</li>
<li>
If there is something wrong, it will give notification in the
<u>console</u>.
</li>
<li>
Usually you can open the console with <u>right click</u> ->
<u>inspect element</u>
</li>
<li>
You can output stuff in the console using the
<code>console.log("sometext")</code> in your javascript code
</li>
<li>
This is obvious for people coming from web development. But isn't
for people coming from other field (R?). It is essential to be able
to debug your code.
</li>
</ul>
</div>
</section>
<!-- ==================== COORDINATE SYSTEM ==================== -->
<section id="coord" class="bg">
<div class="container">
<h1>The coordinate system</h1>
<hr />
<code>→ Explanation:</code>
<br />
<ul style="margin-top: 5px">
<li>
Building a d3.js chart starts by creating a
<code>svg</code> element. This element has a <code>width</code> and
a <code>height</code>, given in pixels.
</li>
<li>
It is important to understand that the top left corner has the
coordinate <code>x=0</code> and <code>y=0</code>. The bottom left
corner has the coordinate <code>x=0</code> and
<code>y=height</code>. The top right corner has the coordinate
<code>x=width</code> and <code>height=0</code>
</li>
</ul>
<br />
<code>→ Example:</code>
<br />
<div class="row">
<!-- ==================== GRAPH SECTION = WHERE THE GRAPH APPEARS ==================== -->
<div class="col-lg-5 .align-middle">
<div style="position: -webkit-sticky; position: sticky; top: 120px">
<div class="bg-light" id="res-coord"></div>
</div>
</div>
<!-- ==================== CODE SECTION = WHERE THE CODE APPEARS ==================== -->
<div class="col-lg-7 text-center .align-middle">
<!-- ========= show html code ============== -->
<aside
style="position: -webkit-sticky; position: sticky; top: 120px"
>
← Edit me!
</aside>
<pre
class="language-html"
><code id="html-coord" contenteditable="true"><xmp><!DOCTYPE html>
<!-- Add a svg area, empty -->
<svg id="dataviz_area" height=200 width=450></svg>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
</xmp></code></pre>
<!-- ==================================== -->
<!-- ========= show JS code ============== -->
<pre
class="language-js"
><code id="js-coord" contenteditable="true"><xmp><script>
var svg = d3.select("#dataviz_area")
svg.append("circle")
.attr("cx", 2).attr("cy", 2).attr("r", 40).style("fill", "blue");
svg.append("circle")
.attr("cx", 140).attr("cy", 70).attr("r", 40).style("fill", "red");
svg.append("circle")
.attr("cx", 300).attr("cy", 100).attr("r", 40).style("fill", "green");
</script></xmp></code></pre>
<!-- ==================================== -->
<!-- ==================== JAVASCRIPT SECTION : EXECUTE THIS CODE AND MAKE IT INTERACTIVE ==================== -->
<script>
// At the beginning, I read the html and JS fragments
myHtmlParser("html-coord", "res-coord");
myJSParser("js-coord", "res-coord");
// If the user change the JS fragment, I run it again:
document
.getElementById("js-coord")
.addEventListener("input", function () {
d3.select("#res-coord").html("");
myHtmlParser("html-coord", "res-coord");
myJSParser("js-coord", "res-coord");
});
// If the user change the HTML fragment, I run it again:
document
.getElementById("html-coord")
.addEventListener("input", function () {
d3.select("#res-coord").html("");
myHtmlParser("html-coord", "res-coord");
myJSParser("js-coord", "res-coord");
});
</script>
</div>
</div>
<code>→ Try:</code>
<br />
<ul style="margin-top: 5px">
<li>
Change the coordinate of the 3 circles to make sure you understood
how it works
</li>
</ul>
</div>
</section>
<br />
<!-- ==================== Scale ==================== -->
<section id="scale" class="bg">
<div class="container">
<h1>From data to pixel: Scales</h1>
<hr />
<code>→ Explanation:</code>
<br />
<ul style="margin-top: 5px">
<li>
Position of element is set in <u>pixel</u>. But input dataset is
<u>not</u>.
</li>
<li>
We thus need a function that <u>translate</u> a numeric variable to
a position in pixel. It is called a <code>scale</code>
</li>
<li>
If my data are percentages and my <code>svg</code> area is 400px
width. 0% → 0px. 100% → 400px. 50% → 200px.
</li>
<li>
Scale always have a <code>domain</code> (0 to 100% here) and a
<code>range</code> (0 to 400px here)
</li>
<li>
Usually the scale for the X axis is called <code>x</code>. If you
run <code>x(10)</code>, d3 returns the position in px for this value
</li>
</ul>
<br />
<code>→ Example:</code>
<div class="row">
<!-- ==================== GRAPH SECTION = WHERE THE GRAPH APPEARS ==================== -->
<div class="col-lg-5 .align-middle">
<div style="position: -webkit-sticky; position: sticky; top: 120px">
<div class="bg-light" id="res-scale"></div>
</div>
</div>
<!-- ==================== CODE SECTION = WHERE THE CODE APPEARS ==================== -->
<div class="col-lg-7 text-center .align-middle">
<!-- ========= show html code ============== -->
<aside
style="position: -webkit-sticky; position: sticky; top: 120px"
>
← Edit me!
</aside>
<pre
class="language-html"
><code id="html-scale" contenteditable="true"><xmp><!DOCTYPE html>
<!-- Add a svg area, empty -->
<svg id="viz_area" height=200 width=450></svg>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
</xmp></code></pre>
<!-- ==================================== -->
<!-- ========= show JS code ============== -->
<pre
class="language-js"
><code id="js-scale" contenteditable="true"><xmp><script>
// Select the svg area
var svg = d3.select("#viz_area")
// Create a scale: transform value in pixel
var x = d3.scaleLinear()
.domain([0, 100]) // This is the min and the max of the data: 0 to 100 if percentages
.range([0, 400]); // This is the corresponding value I want in Pixel
// Try console.log( x(25) ) to see what this x function does.
// Add 3 dots for 0, 50 and 100%
svg.append("circle")
.attr("cx", x(10)).attr("cy", 100).attr("r", 40).style("fill", "blue");
svg.append("circle")
.attr("cx", x(50)).attr("cy", 100).attr("r", 40).style("fill", "red");
svg.append("circle")
.attr("cx", x(100)).attr("cy", 100).attr("r", 40).style("fill", "green");
</script></xmp></code></pre>
<!-- ==================================== -->
<!-- ==================== JAVASCRIPT SECTION : EXECUTE THIS CODE AND MAKE IT INTERACTIVE ==================== -->
<script>
// At the beginning, I read the html and JS fragments
myHtmlParser("html-scale", "res-scale");
myJSParser("js-scale", "res-scale");
// If the user change the JS fragment, I run it again:
document
.getElementById("js-scale")
.addEventListener("input", function () {
d3.select("#res-scale").html("");
myHtmlParser("html-scale", "res-scale");
myJSParser("js-scale", "res-scale");
});
// If the user change the HTML fragment, I run it again:
document
.getElementById("html-scale")
.addEventListener("input", function () {
d3.select("#res-scale").html("");
myHtmlParser("html-scale", "res-scale");
myJSParser("js-scale", "res-scale");
});
</script>
</div>
</div>
<code>→ Try:</code>
<br />
<ul style="margin-top: 5px">
<li>
Change <code>domain</code> and <code>range</code> values to
understand how it works.
</li>
<li>
Add a Y scale to move the circles up or down. Remember 0px is on
top!
</li>
</ul>
</div>
</section>
<br />
<!-- ==================== Axis ==================== -->
<section id="axis" class="bg">
<div class="container">
<h1>Add axis</h1>
<hr />
<code>→ Explanation:</code>
<ul style="margin-top: 5px">
<li>D3 offers a few function to draw <u>axis</u> automatically.</li>
<li>
These axis are always drawn on top of a <code>scale</code>. This
scale specifies <u>where</u> the axis must be placed, and
<u>what range</u> it should indicate.
</li>
<li>
The function <code>axisBottom()</code> creates a horizontal axis,
with ticks and labels at the bottom. <code>axisLeft()</code> will be
used later for the Y axis
</li>
</ul>
<br />
<code>→ Example:</code>
<div class="row">
<!-- ==================== GRAPH SECTION = WHERE THE GRAPH APPEARS ==================== -->
<div class="col-lg-5 .align-middle">
<div style="position: -webkit-sticky; position: sticky; top: 120px">
<div class="bg-light" id="res-axis"></div>
</div>
</div>
<!-- ==================== CODE SECTION = WHERE THE CODE APPEARS ==================== -->
<div class="col-lg-7 text-center .align-middle">
<!-- ========= show html code ============== -->
<aside
style="position: -webkit-sticky; position: sticky; top: 120px"
>
← Edit me!
</aside>
<pre
class="language-html"
><code id="html-axis" contenteditable="true"><xmp><!DOCTYPE html>
<!-- Add a svg area, empty -->
<svg id="Viz_area" height=200 width=450></svg>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
</xmp></code></pre>
<!-- ==================================== -->
<!-- ========= show JS code ============== -->
<pre
class="language-js"
><code id="js-axis" contenteditable="true"><xmp><script>
// Select the svg area
var svg = d3.select("#Viz_area")
// Create a scale: transform value in pixel
var x = d3.scaleLinear()
.domain([0, 100]) // This is the min and the max of the data: 0 to 100 if percentages
.range([0, 400]); // This is the corresponding value I want in Pixel
// Show the axis that corresponds to this scale
svg.call(d3.axisBottom(x));
// Add 3 dots for 0, 50 and 100%
svg.append("circle")
.attr("cx", x(10)).attr("cy", 100).attr("r", 40).style("fill", "blue");
svg.append("circle")
.attr("cx", x(50)).attr("cy", 100).attr("r", 40).style("fill", "red");
svg.append("circle")
.attr("cx", x(100)).attr("cy", 100).attr("r", 40).style("fill", "green");
</script></xmp></code></pre>
<!-- ==================================== -->
<!-- ==================== JAVASCRIPT SECTION : EXECUTE THIS CODE AND MAKE IT INTERACTIVE ==================== -->
<script>
// At the beginning, I read the html and JS fragments
myHtmlParser("html-axis", "res-axis");
myJSParser("js-axis", "res-axis");
// If the user change the JS fragment, I run it again:
document
.getElementById("js-axis")
.addEventListener("input", function () {
d3.select("#res-axis").html("");
myHtmlParser("html-axis", "res-axis");
myJSParser("js-axis", "res-axis");
});
// If the user change the HTML fragment, I run it again:
document
.getElementById("html-axis")
.addEventListener("input", function () {
d3.select("#res-axis").html("");
myHtmlParser("html-axis", "res-axis");
myJSParser("js-axis", "res-axis");
});
</script>
</div>
</div>
<code>→ Try:</code>
<br />
<ul style="margin-top: 5px">
<li>
Change <code>domain</code> and <code>range</code> values to
understand how it works.
</li>
</ul>
</div>
</section>
<br />
<!-- ==================== MARGIN ==================== -->
<section id="margin" class="bg">
<div class="container">
<h1>Margin & translation</h1>
<hr />
<code>→ Explanation:</code>
<ul style="margin-top: 5px">
<li>
The axis position often needs to be <u>adjusted</u>. For instance,
the X axis is usually placed at the <u>bottom</u> of the chart.
</li>
<li>
This is made possible thanks to <u>translation</u>. Basically,
applying <code>.attr("transform", "translate(20,50)")</code> to an
element with translate it 20px to the right and 50px to the bottom.
</li>
<li>
A very common strategy is to apply a translation to the general
<code>svg</code> area, creating a bit of margin around the chart