-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement_port.html
207 lines (148 loc) · 3.61 KB
/
movement_port.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pie Chart Transition</title>
<style>
.chart-container {
/*border: 1px solid pink;*/
margin: 0 auto;
}
svg {
/*border: 2px solid brown;*/
}
.label{
font-family: 'Helvetica';
font-weight: 800;
}
</style>
</head>
<body>
<div class='chart-container'>
<svg width='1050px'height='575px'></svg>
</div>
</body>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script>
// let width = 500;
// let height = 500;
// let margin = 40;
const width = 600,
height = 600,
margin = 40;
const pie = d3.pie()
.value(function(d) {return d[1]})
const radius = Math.min(width, height) / 2 - margin;
// let radius = Math.min(width, height)/2
let svg = d3.select('svg')
.attr('width', width)
.attr('class', 'pop')
.attr('height', height)
.append('g')
.attr('transform', `translate(${width/2}, ${height/2})`);
let popData = {'Datum1': 8483267-2718215, 'Datum 2':2718215}
let data = {'Datum 1':749-90, 'Datum 2':90}
let arcGenerator = d3.arc()
.innerRadius(0)
.outerRadius(radius)
let color = d3.scaleOrdinal()
.range(['#6dad4f', '#008fd5'])
let dataReady = pie(Object.entries(popData))
////////// TRYING SMOOTH TRANSITIONS ///////////
let readyData = pie(Object.entries(popData))
let u = svg.selectAll('path')
.data(readyData)
u
.join('path')
.transition()
.duration(2000)
.attr('d',arcGenerator)
.attr('fill', d => color(d.data[0]))
.attr('stroke', 'white')
.style('stroke-width', '2px')
.style('opacity', 1)
let uText = svg.selectAll('text')
.data(readyData)
uText
.join('text')
.transition()
.duration(2000)
.text(d => d.data[0])
.attr('transform', function(d) {
let translation = arcGenerator.centroid(d)
if (translation[1] < 0) {
// Pop in EDAs
translation[1] -= 45
translation[0] -= 30
} else {
// Pop all
translation[1] +=0
}
return `translate(${translation})`
// console.log(translation[1])
// console.log(translation[1])
// console.log(translation)
})
.attr('class', 'label')
.style('text-anchor', 'middle')
.style('font-size', 100)
function change() {
if (svg.classed('pop')) {
svg.classed('pop', false)
var state = data
} else {
svg.classed('pop', true)
var state = popData
}
let datadata = pie(Object.entries(state))
let u = svg.selectAll('path')
.data(datadata)
u = u.data(datadata);
// console.log(u)
u
.transition()
// .delay(2000)
.ease(d3.easeQuadInOut)
.duration(1500).attrTween('d', arcTween)
let uText = svg.selectAll('text')
.data(readyData)
uText = uText.data(datadata)
uText
.transition()
// .delay(2000)
.ease(d3.easeQuadInOut)
.duration(1500)
.attrTween('transform',labelArcTween)
.text(d => d.data[0])
}
function arcTween(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t) {
return arcGenerator(i(t));
};
}
function labelArcTween(a) {
var i = d3.interpolate(this._current, a);
this._current = i(0);
return function(t){
let translation = arcGenerator.centroid(i(t))
// console.log(translation[1])
if (translation[1] < 0) {
// Pop in EDAs
translation[1] -= 45
translation[0] -= 30
} else {
// Pop all
translation[1] +=0
}
return `translate(${translation})`
// return `translate(${arcGenerator.centroid(i(t))})`
}
}
let timer = d3.interval(change, 3000)
// let btn = d3.select('button')
// btn.on('click', change)
</script>
</html>