forked from jtr13/d3book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_constancy.Rmd
135 lines (95 loc) · 3.19 KB
/
object_constancy.Rmd
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
# Object Constancy <i class="fa fa-object-ungroup"></i> {#object}
## No object constancy
<script src="https://d3js.org/d3.v6.js"></script>
<table>
<tr><td>
<div id="chart"></div>
</td><td>
<p>**Transitions**</p>
<input type="radio" name="duration" class="noc" value="0" checked> Off</input>
<input type="radio" name="duration" class = "noc" value="750"> On</input>
<p></p>
<p id="add" class="noc">Add bar</p>
<p id="remove_right" class="noc">Remove bar (right)</p>
<p id="remove_left" class="noc">Remove bar (left)</p>
</td></tr></table>
<script src="scripts/no_obj_const.js"></script>
<br>
**Of note:**
* Rather than smoothly transitioning off to the left, all bars are resized when "Remove bar (left)" is clicked
* When "Remove bar (right) is clicked, the bar on the right immediately disappears, and then the remaining bars transition to their new places to the right.
Standalone version: [no_object_constancy.html](code/no_object_constancy.html)
## Object constancy
<table>
<tr><td>
<div id="chart2"></div>
</td><td>
<p>**Transitions**</p>
<input type="radio" name="duration2" class="oc" value="0" checked="true"> Off</input>
<input type="radio" name="duration2" class = "oc" value="750"> On</input>
<p></p>
<p id="add" class="oc">Add bar</p>
<p id="remove_right" class="oc">Remove bar (right)</p>
<p id="remove_left" class="oc">Remove bar (left)</p>
</td></tr></table>
<script src="scripts/obj_const.js"></script>
**Of note:**
* Bars now smoothly transition off to the left and right
Standalone version: [object_constancy.html](code/object_constancy.html)
Slides: [object_constancy.pdf](pdfs/object_constancy.pdf){target="_blank"}
### Practice joining data by key
<div id="key"></div>
> <i class="fa fa-hand-o-right"></i> *Open the JavaScript Console* <i class="fa fa-terminal"></i>
Try the following:
1. Create an svg with four text elements:
``` js
var dataset = [{key: 12, x: 163, y: 200},
{key: 14, x: 206, y: 304},
{key: 16, x: 452, y: 152},
{key: 18, x: 321, y: 254}];
var svg = d3.select("#key").append("svg")
.attr("width", "600").attr("height", "400");
svg.append("rect").attr("width", "600")
.attr("height", "400").attr("fill", "aliceblue");
svg.selectAll("text")
.data(dataset, d => d.key)
.enter()
.append("text")
.attr("x", d => d.x)
.attr("y", d => d.y)
.text(d => `key: ${d.key}`);
```
2. Bind a new dataset by key:
``` js
var svg = d3.select("#key").select("svg");
var dataset = [{key: 12, x: 100, y: 200},
{key: 16, x: 250, y: 300}];
svg.selectAll("text")
.data(dataset, d => d.key)
.exit()
.remove();
```
Then:
``` js
svg.selectAll("text")
.attr("x", d => d.x)
.attr("y", d => d.y);
```
3. And another one:
``` js
var svg = d3.select("#key").select("svg");
var dataset = [{key: 23, x: 300, y: 150},
{key: 5, x: 450, y: 270},
{key: 16, x: 200, y: 250}];
var databind = svg.selectAll("text")
.data(dataset, d => d.key);
databind
.enter()
.append("text")
.merge(databind)
.attr("x", d => d.x)
.attr("y", d => d.y)
.text(d => `key: ${d.key}`);
databind.exit().remove();
```
4. Experiment with other data binds.