-
Notifications
You must be signed in to change notification settings - Fork 34
/
transitions.Rmd
169 lines (122 loc) · 3.98 KB
/
transitions.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
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
# Transitions <i class="far fa-paper-plane"></i>
<script src="https://d3js.org/d3.v7.js"></script>
Read *IDVW2*, Chapter 9: transitions section (pp. 158-178)
<svg width="300" height="200">
<rect x="0" y="0" width="300" height="200" fill="lightblue"></rect>
<circle cx="150" cy="40" r="15" fill="red"></circle>
<circle cx="150" cy="80" r="15" fill="red"></circle>
<circle id="henry" cx="150" cy="120" r="15" fill="red"></circle>
<circle class="apple" cx="150" cy="160" r="15" fill="red"></circle>
</svg>
## Examples
Open Developer Tools and try in the Console:
``` js
d3.select("svg")
.selectAll("circle")
.transition()
.duration(2000)
.attr("cx", "275");
```
``` js
d3.select("svg")
.selectAll("circle")
.transition()
.duration(2000)
.attr("cx", "25")
.attr("fill", "green");
```
## Do this
Run simultaneous transitions on *different* selections:
``` js
d3.select("svg").selectAll("circle#henry").transition()
.duration(2000).attr("cx", "275");
d3.select("svg").selectAll("circle.apple").transition()
.duration(2000).attr("cx", "25");
```
Run sequential transitions on the same selection in one chain:
``` js
d3.select("svg").selectAll("circle")
.transition().duration(2000).attr("cx", "275")
.transition().duration(2000).attr("cx", "25");
```
Transition from *something* to *something*:
``` js
d3.select("svg").append("circle")
.attr("cx", "200")
.attr("cy", "100")
.attr("r", "5")
.attr("fill", "lightblue")
.transition()
.duration(4000)
.attr("r", "25")
.attr("fill", "blue");
```
## Not this
DO NOT run two transitions on the same selection at the same time (see p. 172).
(What works in the Console *will not work* in a script.)
``` js
d3.select("svg").selectAll("circle").transition()
.duration(2000).attr("cx", "250");
d3.select("svg").selectAll("circle").transition()
.duration(2000).attr("cx", "75");
```
DO NOT transition from *nothing* to something:
``` js
d3.select("svg").append("circle")
.transition()
.duration(2000)
.attr("cx", "200")
.attr("cy", "100")
.attr("r", "25")
.attr("fill", "red");
```
DO NOT store a selection with a transition (it's no longer a selection with the transition):
Try this:
``` js
const circ = d3.select("svg")
.selectAll("circle")
.data([50, 95, 100, 200, 50, 150, 250])
.enter()
.append("circle")
.attr("cx", d => d)
.attr("cy", "100")
.attr("fill", "blue")
.attr("r", "0")
.transition()
.duration(2000)
.attr("r", "25");
```
And then this:
``` js
circ.attr("fill", "green");
```
DO NOT put a transition before a merge:
``` js
d3.select("svg")
.selectAll("circle")
.transition()
.duration(2000)
.attr("cx", "300")
.merge("oldcirc")
.attr("fill", "green");
```
BE AWARE that not everything transitions (for example, text doesn't.)
## Strategy
**Example 1**
Think carefully about what you want to happen, and then decide what goes before and after the transition.
<svg id="transitions"></svg>
<p></p>
<button type="button" onclick="add()">Add bar</button>
<button type="button" onclick="remove()">Remove bar</button>
<script src="scripts/bar_transition.js"></script>
Plan what you want to happen:
1. new bars appear on the right side with orange fill
2. new bars slide into place from the right as old bars are repositioned
3. new bars transition to blue
4. removed bars transition to right before disappearing
## Exercise <i class="fas fa-dumbbell"></i>: Bar chart with transitions
Download and make changes to [bar_transition.html](https://raw.githubusercontent.com/jtr13/d3book/main/code/bar_transition.html){target="_blank"} in a text editor so the transitions work as shown below.
**Solution**
[code for download](https://raw.githubusercontent.com/jtr13/d3book/main/solutions/bar_transition_solution.html){target="_blank"}
[rendered version](solutions/bar_transition_solution.html){target="_blank"}
Further reading: [Working with Transitions](https://bost.ocks.org/mike/transition/){target="_blank"}.