-
Notifications
You must be signed in to change notification settings - Fork 4
/
colinsmith-viz-example.html
963 lines (835 loc) · 32.3 KB
/
colinsmith-viz-example.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
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>d3.js ~ Three Little Circles</title>
<script type="text/javascript" src="css/d3.v2.js?2.8.1"></script>
<style type="text/css">
@import url("css/style.css?1.10.0");
@import url("css/syntax.css?1.6.0");
</style>
</head>
<body>
<div class="body">
<div class="content">
<div class="topbar">
<a href="">Overview</a>
<a href="../ex/">Examples</a>
<b><a href="../api/">Documentation</a></b>
<a href="https://github.com/mbostock/d3/archives/master">Download</a>
</div>
<div class="sidebar">
<h1>d3.js</h1>
</div>
<h1 id='three_little_circles'>Three Little Circles</h1>
<style type='text/css'>
.chart {
background: #fff;
border: solid 1px #ddd;
box-shadow: 0 0 4px rgba(0, 0, 0, .2);
font: 10px sans-serif;
height: 180px;
position: relative;
width: 720px;
}
.chart svg {
border-left: solid 2px #ddd;
left: 360px;
position: absolute;
top: 0;
}
.chart pre {
font: 12px monospace;
height: 60px;
left: 10px;
position: absolute;
top: 0;
width: 340px;
}
.chart circle.little {
fill: #aaa;
stroke: #666;
stroke-width: 1.5px;
}
.chart button {
left: 275px;
position: absolute;
top: 145px;
width: 80px;
}
.chart .data rect {
fill: #eee;
stroke: #ccc;
}
</style>
<script type='text/javascript'>
var data = [32, 57, 112, 114, 215, 489],
dataEnter = data.concat(293),
dataExit = data.slice(0, 2),
w = 360,
h = 180,
x = d3.scale.ordinal().domain([57, 32, 112]).rangePoints([0, w], 1),
y = d3.scale.ordinal().domain(data).rangePoints([0, h], 2);
</script>
<p>Once upon a time, there were three little circles. This tutorial shows you how to manipulate them using
selections.</p>
<h2 id='selecting_elements'>Selecting Elements</h2>
<p>The <a href='https://github.com/mbostock/d3/wiki/Selections#selectAll'>selectAll</a> operator takes a <a
href='http://www.w3.org/TR/CSS2/selector.html'>selector</a> string, such as “circle”, and returns a
selection:</p>
<div class='chart' id='chart-2'>
<pre class='code'>
var circle = svg.selectAll("circle");
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-2").append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll(".little")
.data(data)
.enter().append("circle")
.attr("class", "little")
.attr("cx", x)
.attr("cy", y)
.attr("r", 12);
d3.select("#chart-2 button").on("click", function () {
svg.selectAll(".select").remove();
svg.selectAll(".select")
.data(data)
.enter().append("circle")
.attr("class", "select")
.attr("cx", x)
.attr("cy", y)
.attr("r", 60)
.style("fill", "none")
.style("stroke", "red")
.style("stroke-opacity", 1e-6)
.style("stroke-width", 3)
.transition()
.duration(750)
.attr("r", 12)
.style("stroke-opacity", 1);
});
})();
</script>
<p>Once we have a selection, we can apply various operators to the selected elements. For example, we might change the
fill color using <a href='https://github.com/mbostock/d3/wiki/Selections#style'>style</a>, and the radius and the
<em>y</em>-position using <a href='https://github.com/mbostock/d3/wiki/Selections#attr'>attr</a>:</p>
<div class='chart' id='chart-3'>
<pre class='code'>
circle.style("fill", "steelblue");
circle.attr("cy", 90);
circle.attr("r", 30);
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-3").append("svg")
.attr("width", w)
.attr("height", h);
var circle = svg.selectAll(".little")
.data(data)
.enter().append("circle")
.attr("class", "little")
.attr("cx", x)
.attr("cy", y)
.attr("r", 12);
d3.select("#chart-3 button").on("click", function () {
circle.style("fill", "#aaa").attr("r", 12).attr("cy", y);
circle.transition().duration(500).delay(0).style("fill", "steelblue");
circle.transition().duration(500).delay(500).attr("cy", 90);
circle.transition().duration(500).delay(1000).attr("r", 30);
});
})();
</script>
<p>We can also compute the attribute values dynamically, using functions rather than constants. For example, maybe we
want to set the <em>x</em>-coordinate to a random value:</p>
<div class='chart' id='chart-4'>
<pre class='code'>
circle.attr("cx", function() {
return Math.random() * w;
});
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-4").append("svg")
.attr("width", w)
.attr("height", h);
var circle = svg.selectAll(".little")
.data(data)
.enter().append("circle")
.attr("class", "little")
.attr("cx", x)
.attr("cy", y)
.attr("r", 12);
d3.select("#chart-4 button").on("click", function () {
circle.transition()
.duration(750)
.attr("cx", function () {
return Math.random() * w;
});
});
})();
</script>
<p>If you run this example multiple times, you’ll see that the attribute is recomputed as a number random number
each time. Unlike Protovis, D3 doesn’t stash these functions internally; they are run once, immediately, and
then your code continues. So you can run them again or redefine them however you like.</p>
<h2 id='binding_data'>Binding Data</h2>
<p>This is beginning to look a lot like <a href='http://jquery.com/'>jQuery</a>. More commonly, though, we want to use
<em>data</em> to drive the appearance of our circles. To do that, we need some data. For the sake of example, let’s
imagine that each of these circles represents a number: 32, 57 and 112. The <a
href='https://github.com/mbostock/d3/wiki/Selections#data'>data</a> operator binds these numbers to the
circles:</p>
<div class='chart' id='chart-5'>
<pre class='code'>
circle.data([32, 57, 112]);
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-5").append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll(".little")
.data(data)
.enter().append("circle")
.attr("class", "little")
.attr("cx", x)
.attr("cy", y)
.attr("r", 12);
var g = svg.selectAll(".data")
.data(data)
.enter().append("g")
.attr("class", "data")
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
});
g.append("rect")
.attr("x", -10)
.attr("y", -10)
.attr("width", 20)
.attr("height", 20);
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
d3.select("#chart-5 button").on("click", function () {
g
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
})
.select("rect")
.style("opacity", 1);
g.transition()
.duration(750)
.attr("transform", function (d) {
return "translate(" + x(d) + "," + y(d) + ")";
})
.select("rect")
.style("opacity", 1e-6);
});
})();
</script>
<p>All data in D3 is specified as an array of values. Conveniently, this mirrors the concept of a selection, which is
just an array of elements. Notice then how the first number (the first <em>datum</em>, 32) is bound to the first
circle (the first <em>element</em>, on top), the second number is bound to the second circle, and so on.</p>
<p>Once data is bound, that data is accessible as an argument to our attribute and style functions. This means we
visually encode data, or in other words, create a visualization! For example, here we set the <em>x</em>-position
and radius using the data:</p>
<div class='chart' id='chart-6'>
<pre class='code'>
circle.attr("cx", function(d) {
return d;
});
circle.attr("r", function(d) {
return Math.sqrt(d);
});
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-6").append("svg")
.attr("width", w)
.attr("height", h);
var g = svg.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function (d) {
return "translate(" + x(d) + "," + y(d) + ")";
});
g.append("circle")
.attr("class", "little")
.attr("r", 12);
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
d3.select("#chart-6 button").on("click", function () {
g
.attr("transform", function (d) {
return "translate(" + x(d) + "," + y(d) + ")";
})
.select("circle")
.attr("r", 12);
g.transition()
.duration(750)
.attr("transform", function (d) {
return "translate(" + d + "," + y(d) + ")";
})
.select("circle")
.attr("r", Math.sqrt);
});
})();
</script>
<p>There’s a second argument to each function you can use: it specifies the <em>index</em> of the element within
its selection. This is a <a href='http://en.wikipedia.org/wiki/Zero-based_numbering'>zero-based</a> index, and it’s
useful for computing offsets or as a simple way of identifying individual elements. The argument is optional; if you
don’t specify it when declaring your function, it will be ignored. For example:</p>
<div class='chart' id='chart-6b'>
<pre class='code'>
circle.attr("cx", function(d, i) {
return i * 100 + 30;
});
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-6b").append("svg")
.attr("width", w)
.attr("height", h);
var g = svg.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function (d) {
return "translate(" + x(d) + "," + y(d) + ")";
});
g.append("circle")
.attr("class", "little")
.attr("r", 12);
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function (d, i) {
return i;
});
d3.select("#chart-6b button").on("click", function () {
g
.attr("transform", function (d) {
return "translate(" + x(d) + "," + y(d) + ")";
})
.transition()
.duration(750)
.attr("transform", function (d, i) {
return "translate(" + (i * 100 + 30) + "," + y(d) + ")";
});
});
})();
</script>
<p>Here we use the index <em>i</em> to position the elements sequentially only the <em>x</em>-dimension. Each element is
separated by 100 pixels, with an offset of 30 pixels from the left side. In SVG, the origin is in the top-left
corner.</p>
<h2 id='creating_elements'>Creating Elements</h2>
<p>But what if we had <em>four</em> numbers to display, rather than three? We wouldn’t have enough circles to
display all the numbers. When joining data to elements, D3 stores the leftover data in the <a
href='https://github.com/mbostock/d3/wiki/Selections#enter'>enter</a> selection. (The terms “enter”
and “exit” are adopted from stage terminology.) Here, the fourth number 293 remains in the enter
selection, because we only have three circle elements:</p>
<div class='chart' id='chart-7'>
<pre class='code'>
var circle = svg.selectAll("circle")
.data([32, 57, 112, 293]);
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-7").append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll(".little")
.data(data)
.enter().append("circle")
.attr("class", "little")
.attr("cx", x)
.attr("cy", y)
.attr("r", 12);
var g = svg.selectAll(".data")
.data(dataEnter)
.enter().append("g")
.attr("class", "data")
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
});
g.append("rect")
.attr("x", -10)
.attr("y", -10)
.attr("width", 20)
.attr("height", 20);
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
d3.select("#chart-7 button").on("click", function () {
g
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
})
.select("rect")
.style("opacity", 1);
g.filter(
function (d, i) {
return i != 3;
}).transition()
.duration(750)
.attr("transform", function (d) {
return "translate(" + x(d) + "," + y(d) + ")";
})
.select("rect")
.style("opacity", 1e-6);
g.select("rect").filter(function (d, i) {
return i == 3;
})
.style("fill", "#eee")
.style("stroke", "#ccc")
.transition()
.duration(750)
.style("fill", "lightgreen")
.style("stroke", "green");
});
})();
</script>
<p>Using the enter selection, we can create new circles for any missing data. Each new circle is already bound to the
data, so we can use data to compute attributes and styles:</p>
<div class='chart' id='chart-8'>
<pre class='code'>
var enter = circle.enter().append("circle");
enter.attr("cy", 90);
enter.attr("cx", 160);
enter.attr("r", function(d) {
return Math.sqrt(d);
});
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-8").append("svg")
.attr("width", w)
.attr("height", h);
var g = svg.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function (d) {
return "translate(" + x(d) + "," + y(d) + ")";
});
g.append("circle")
.attr("class", "little")
.attr("r", 12);
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
var g = svg.selectAll(".data")
.data(dataEnter)
.enter().append("g")
.attr("class", "data")
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
})
.filter(function (d, i) {
return i == 3;
});
g.append("circle")
.attr("class", "little")
.attr("r", 1e-6);
g.append("rect")
.attr("x", -10)
.attr("y", -10)
.attr("width", 20)
.attr("height", 20)
.style("fill", "lightgreen")
.style("stroke", "green");
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
d3.select("#chart-8 button").on("click", function () {
g.attr("transform", "translate(80,20)");
g.select("circle").attr("r", 1e-6);
g.select("rect").style("opacity", 1);
var t = g.transition().duration(750);
t.attr("transform", "translate(160,100)");
t.select("circle").attr("r", Math.sqrt);
t.select("rect").style("opacity", 1e-6);
});
})();
</script>
<p>Taking this to the next logical step, then, what if we have <em>no</em> existing elements? Meaning, what if the
document is empty? Say we start with an empty page, and we want to create new circles that correspond to our data?
Then we’re joining data to an empty selection, and all of the data ends up in <em>enter</em>:</p>
<div class='chart' id='chart-9'>
<pre class='code'>
var enter = circle.enter().append("circle");
enter.attr("cy", 90);
enter.attr("cx", function(d) {
return d;
});
enter.attr("r", function(d) {
return Math.sqrt(d);
});
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-9").append("svg")
.attr("width", w)
.attr("height", h);
var g = svg.selectAll(".data")
.data(dataEnter)
.enter().append("g")
.attr("class", "data")
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
});
g.append("circle")
.attr("class", "little")
.attr("r", 1e-6);
g.append("rect")
.attr("x", -10)
.attr("y", -10)
.attr("width", 20)
.attr("height", 20)
.style("fill", "lightgreen")
.style("stroke", "green");
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
d3.select("#chart-9 button").on("click", function () {
g.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
});
g.select("rect").style("opacity", 1);
g.select("circle").attr("r", 1e-6);
var t = g.transition().duration(750);
t.attr("transform", function (d, i) {
return "translate(" + d + ",90)";
});
t.select("circle").attr("r", Math.sqrt);
t.select("rect").style("opacity", 1e-6);
});
})();
</script>
<p>This pattern is so common, you’ll often see the <a
href='https://github.com/mbostock/d3/wiki/Selections#selectAll'>selectAll</a> + <a
href='https://github.com/mbostock/d3/wiki/Selections#data'>data</a> + <a
href='https://github.com/mbostock/d3/wiki/Selections#enter'>enter</a> + <a
href='https://github.com/mbostock/d3/wiki/Selections#append'>append</a> operators called sequentially, one
immediately after the other. Despite it being common, keep in mind that this is just one special case of a data
join; we’ve already seen another common case (selecting elements for update) and we’ll see other
interesting cases to consider in a bit.</p>
<p>Another technique you can use to make your code more concise is <a
href='http://en.wikipedia.org/wiki/Method_chaining'>method chaining</a>. Each operator in D3 returns the current
selection, so you can apply multiple operators sequentially. For example, the above code can be rewritten:</p>
<div class='chart' id='chart-10'>
<pre class='code'>
svg.selectAll("circle")
.data([32, 57, 112, 293])
.enter().append("circle")
.attr("cy", 90)
.attr("cx", String)
.attr("r", Math.sqrt);
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-10").append("svg")
.attr("width", w)
.attr("height", h);
var g = svg.selectAll(".data")
.data(dataEnter)
.enter().append("g")
.attr("class", "data")
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
});
g.append("circle")
.attr("class", "little")
.attr("r", 1e-6);
g.append("rect")
.attr("x", -10)
.attr("y", -10)
.attr("width", 20)
.attr("height", 20)
.style("fill", "lightgreen")
.style("stroke", "green");
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
d3.select("#chart-10 button").on("click", function () {
g.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
});
g.select("rect").style("opacity", 1);
g.select("circle").attr("r", 1e-6);
var t = g.transition().duration(750);
t.attr("transform", function (d, i) {
return "translate(" + d + ",90)";
});
t.select("circle").attr("r", Math.sqrt);
t.select("rect").style("opacity", 1e-6);
});
})();
</script>
<p>As you can see, the code is made even smaller using built-in JavaScript functions, rather than defining anonymous
ones. The built-in <a href='https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String'>String</a>
method, for example, is a shorthand way of using JavaScript’s default string conversion to compute the
attribute value from the associated data. Similarly, we can plug in <a
href='https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/sqrt'>Math.sqrt</a> to set
the radius attribute as the square root of the associated data. This technique of plugging in reusable functions to
compute attribute values is used extensively in D3, particularly in conjunction with <a
href='https://github.com/mbostock/d3/wiki/Quantitative-Scales'>scales</a> and <a
href='https://github.com/mbostock/d3/wiki/SVG-Shapes'>shapes</a>.</p>
<h2 id='destroying_elements'>Destroying Elements</h2>
<p>Sometimes you have the opposite problem from creation: you have too <em>many</em> existing elements, and you want to
remove them. You can select nodes and <a href='https://github.com/mbostock/d3/wiki/Selections#remove'>remove</a>
them, but more commonly, you’ll use the <a href='https://github.com/mbostock/d3/wiki/Selections#exit'>exit</a>
selection to let D3 determine which elements are exiting the stage. The exit selection is the opposite of the enter
selection: it contains all elements for which there is no corresponding data.</p>
<div class='chart' id='chart-11'>
<pre class='code'>
var circle = svg.selectAll("circle")
.data([32, 57]);
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-11").append("svg")
.attr("width", w)
.attr("height", h);
var circle = svg.selectAll(".little")
.data(data)
.enter().append("circle")
.attr("class", "little")
.attr("cx", x)
.attr("cy", y)
.attr("r", 12)
.filter(function (d, i) {
return i == 2;
});
var g = svg.selectAll(".data")
.data(dataExit)
.enter().append("g")
.attr("class", "data")
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
});
g.append("rect")
.attr("x", -10)
.attr("y", -10)
.attr("width", 20)
.attr("height", 20);
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
d3.select("#chart-11 button").on("click", function () {
g
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
})
.select("rect")
.style("opacity", 1);
g.transition()
.duration(750)
.attr("transform", function (d) {
return "translate(" + x(d) + "," + y(d) + ")";
})
.select("rect")
.style("opacity", 1e-6);
circle
.style("fill", "#aaa")
.style("stroke", "#666")
.transition()
.duration(750)
.style("fill", "lightcoral")
.style("stroke", "red");
});
})();
</script>
<p>All that’s left to do, then, is to remove the exiting elements:</p>
<div class='chart' id='chart-12'>
<pre class='code'>
circle.exit().remove();
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-12").append("svg")
.attr("width", w)
.attr("height", h);
var g = svg.selectAll(".data")
.data(data)
.enter().append("g")
.attr("class", "data")
.attr("transform", function (d) {
return "translate(" + x(d) + "," + y(d) + ")";
});
g.append("circle")
.attr("class", "little")
.attr("r", 12);
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
g = g.filter(function (d, i) {
return i == 2;
});
g.select("circle")
.style("fill", "lightcoral")
.style("stroke", "red");
d3.select("#chart-12 button").on("click", function () {
g.select("circle").attr("r", 12);
g.select("text").style("opacity", 1);
var t = g.transition().duration(750);
t.select("circle").attr("r", 1e-6);
t.select("text").style("opacity", 1e-6);
});
})();
</script>
<p>The enter, update and exit selections are computed by the <a
href='https://github.com/mbostock/d3/wiki/Selections#data'>data</a> operator, and don’t change when you
append or remove elements—at least until you call <a
href='https://github.com/mbostock/d3/wiki/Selections#selectAll'>selectAll</a> again. So, if you keep
variables around that point to selections (such as <code>circle</code>, above), you’ll probably want to
reselect after adding or removing elements.</p>
<h2 id='all_together_now'>All Together Now</h2>
<p>Putting everything together, consider the three possible outcomes that result from joining data to elements:</p>
<ol>
<li>enter - incoming actors, entering the stage.</li>
<li>update - persistent actors, staying on stage.</li>
<li>exit - outgoing actors, exiting the stage.</li>
</ol>
<p>When we use the default join-by-index, either the enter or exit selection will be empty (or both): if there are more
data than elements, the extra data are in the enter selection; if there are fewer data than elements, the extra
elements are in the exit selection. However, by specifying a key function to the data operator, we can control
exactly how data is bound to elements. And in this case, we have both enter and exit.</p>
<div class='chart' id='chart-13'>
<pre class='code'>
var circle = svg.selectAll("circle")
.data([32, 57, 293], String);
circle.enter().append("circle")
.attr("cy", 90)
.attr("cx", String)
.attr("r", Math.sqrt);
circle.exit().remove();
</pre>
<button>Run</button>
</div>
<script type='text/javascript'>
(function () {
var svg = d3.select("#chart-13").append("svg")
.attr("width", w)
.attr("height", h);
var gd = svg.selectAll(".data")
.data([32, 57, 293])
.enter().append("g")
.attr("class", "data")
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
});
var ed = gd.filter(function (d, i) {
return i == 2;
}),
ud = gd.filter(function (d, i) {
return i != 2;
});
ed.append("circle")
.attr("class", "little")
.attr("r", 1e-6);
gd.append("rect")
.attr("x", -10)
.attr("y", -10)
.attr("width", 20)
.attr("height", 20);
gd.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
var ge = svg.selectAll(".element")
.data(data)
.enter().append("g")
.attr("class", "element")
.attr("transform", function (d) {
return "translate(" + d + ",90)";
});
ge.append("circle")
.attr("class", "little")
.attr("r", Math.sqrt);
ge.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(String);
ed.select("rect")
.style("fill", "lightgreen")
.style("stroke", "green");
var xe = ge.filter(function (d, i) {
return i == 2;
});
xe.select("circle")
.style("fill", "lightcoral")
.style("stroke", "red");
d3.select("#chart-13 button").on("click", function () {
gd
.attr("transform", function (d, i) {
return "translate(" + 20 * (i + 1) + ",20)";
})
.transition()
.duration(750)
.attr("transform", function (d) {
return "translate(" + d + ",90)";
});
gd.select("rect")
.style("opacity", 1)
.transition()
.duration(750)
.style("opacity", 1e-6);
ed.select("circle")
.attr("r", 1e-6)
.transition()
.duration(750)
.attr("r", Math.sqrt);
xe.select("circle")
.attr("r", Math.sqrt)
.transition()
.duration(750)
.attr("r", 1e-6);
xe.select("text")
.style("opacity", 1)
.transition()
.duration(750)
.style("opacity", 1e-6);
});
})();
</script>
<p>Want to learn more about selections and transitions? Read <a href='bar-2.html'>A Bar Chart, Part 2</a> for a
practical example of using enter and exit to display realtime data.</p>
</div>
<div class="foot">Copyright © 2011 <a href="http://bost.ocks.org/mike">Mike Bostock</a></div>
</div>
</div>
</body>
</html>