-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3_transitions.js
140 lines (125 loc) · 3.62 KB
/
d3_transitions.js
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
/*
A transition is a selection-like interface for animating changes to the DOM. Instead of applying changes instantaneously, transitions smoothly interpolate the DOM from its current state to the desired target state over a given duration.
*/
const SVG_WIDTH = document.querySelector('svg').clientWidth;
const SVG_HEIGHT = document.querySelector('svg').clientHeight;
const rectData =
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130];
let allRect =
d3.select('svg')
.selectAll('rect')
.data(rectData)
.join('rect')
.attr('x', (d, i) => (i * SVG_WIDTH / rectData.length))
.attr('y', d => SVG_HEIGHT - d * 3)
.attr('width', SVG_WIDTH / rectData.length - 2)
// .attr('height', d => d * 3)
.attr('rx', '5')
.attr('ry', '5')
// CREATE A TRANSITION ON RECT ELEMENTS
// There are 2 ways to set transitions
// A. using the selection.transition(), followed by other transition methods (delay, ease and duration)
// B. create a transition object using d3.transition(), followed by other transition methods and then call the transition() method on the selection and then pass the transition object into the transition() method
// Just duration is enough to see the transitions
// If delay needed then delay --> duration
// If duartion and ease needed then ease --> duration
// If all three needed then delay --> ease --> duration
// Process A
// 1. Just duration
// allRect
// .transition()
// .duration(2000)
// .attr('height', d => d * 3)
// .transition()
// .duration(2000)
// .style('fill', 'pink');
// 2. duartion and delay
// allRect
// .transition()
// .delay(1000)
// .duration(2000)
// .attr('height', d => d * 3)
// .transition()
// .delay(1000)
// .duration(2000)
// .style('fill', 'salmon');
// 3. duartion and ease
// allRect
// .transition()
// .ease(d3.easeBounce)
// .duration(2000)
// .attr('height', d => d * 3)
// .transition()
// .ease(d3.easeBounce)
// .duration(5000)
// .style('fill', 'crimson');
// 4. all 3
// allRect
// .transition()
// .delay(1000)
// .ease(d3.easeBounce)
// .duration(2000)
// .attr('height', d => d * 3)
// .transition()
// .delay(1000)
// .ease(d3.easeBounce)
// .duration(5000)
// .style('fill', 'slategrey');
// Process B
// 1. only duration
// let T1 = d3.transition()
// .duration(2000);
// console.log(T1);
// allRect.transition(T1)
// .attr('height', d => d * 3)
// .attr('fill', 'khaki');
// 2. duration and delay
// let T1 = d3.transition()
// .delay(1000)
// .duration(2000);
// let T2 = d3.transition()
// .delay(1000)
// .duration(2000);
// allRect
// .transition(T1)
// .attr('height', d => d * 3)
// //.transition(T2) // can comment this
// .style('fill', 'khaki');
// 3. duration and ease
// let T1 = d3.transition()
// .ease(d3.easeExp)
// .duration(2000);
// let T2 = d3.transition()
// .ease(d3.easeSinOut)
// .duration(2000);
// allRect
// .transition(T1)
// .attr('height', d => d * 3)
// //.transition(T2) // can comment this
// .style('fill', 'khaki');
// 4. all 3
// let T1 = d3.transition()
// .delay(1000)
// .ease(d3.easeCircleIn)
// .duration(2000);
// let T2 = d3.transition()
// .delay(1000)
// .ease(d3.easeSinOut)
// .duration(2000);
// allRect
// .transition(T1)
// .attr('height', d => d * 3)
// .transition(T2) // can comment this
// .style('fill', 'khaki');
// to control elements individually
allRect.each((d, i, n) => {
d3.select(n[i])
.transition()
.delay(i * d)
.duration(i * d)
.attr('height', d => d * 3)
.transition()
.delay(i * d)
.duration(i * d)
.style('fill', d3.interpolateBlues((i + 1) / rectData.length))
})