-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
314 lines (289 loc) · 9.27 KB
/
index.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
<!DOCTYPE html>
<html lang="en" class="mdc-typography">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Jedi Organizational Chart</title>
<link rel="stylesheet" href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css">
<style>
html, body {
background-color: #fafafa;
margin: 0;
}
main {
width: 100vw;
height: 100vh;
}
.mdc-card {
background-color: #ffffff;
height: 124px;
width: 224px;
margin: 5px;
}
.ac-chart {
position: relative;
}
.ac-chart__nodes, .ac-chart__links {
position: absolute;
top: 0;
left: 0;
overflow: visible;
}
.ac-chart__links {
z-index: 1;
}
.ac-chart__nodes {
z-index: 2;
}
.ac-chart__node {
position: absolute;
}
[data-depth='0'] {
z-index: 15;
}
[data-depth='1'] {
z-index: 14;
}
[data-depth='2'] {
z-index: 13;
}
[data-depth='3'] {
z-index: 12;
}
[data-depth='4'] {
z-index: 11;
}
[data-depth='5'] {
z-index: 10;
}
[data-depth='6'] {
z-index: 9;
}
[data-depth='7'] {
z-index: 8;
}
[data-depth='8'] {
z-index: 7;
}
[data-depth='9'] {
z-index: 6;
}
[data-depth='10'] {
z-index: 5;
}
[data-depth='11'] {
z-index: 4;
}
[data-depth='12'] {
z-index: 3;
}
[data-depth='13'] {
z-index: 2;
}
[data-depth='14'] {
z-index: 1;
}
[data-depth='15'] {
z-index: 0;
}
</style>
</head>
<body>
<main>
<div class="ac-chart"></div>
</main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
<script>
const width = document.getElementsByTagName('main')[0].clientWidth;
const height = document.getElementsByTagName('main')[0].clientHeight;
const animationDuration = 600;
const animationEasement = d3.easePoly;
const nodeWidth = 234;
const nodeHeight = 134;
const nodePadding = 5;
const nodePositioningHelper = {
horizontalCenter: nodeWidth / 2,
verticalCenter: nodeHeight / 2,
topCenter: 0 + nodePadding,
bottomCenter: nodeHeight - nodePadding,
linkHeight: 50
};
const treemapDefault = d3.tree()
.nodeSize([nodeWidth * 1.2, nodeHeight * 1.3]) // multiplier creates buffer between nodes
.separation((a, b) => a.parent == b.parent ? 1 : 2);
const acChart = d3.select('.ac-chart');
const acChartNodes = acChart.append('div').attr('class', 'ac-chart__nodes');
const acChartLinks = acChart.append('svg').attr('class', 'ac-chart__links');
let root = [];
d3.json('./data/jedi.json', data => {
root = d3.stratify()
.id(d => d._id)
.parentId(d => d.parentId)
(data);
// function collapse(d) {
// if (d.children) {
// d._children = d.children;
// d._children.forEach(collapse);
// d.children = null;
// }
// }
// root.children.forEach(collapse);
renderChart(root);
});
function renderChart(source) {
treemapDefault(root); // used to find horizontal width
let minMax = findXMinMax(root, root.x, root.y);
const chartWidth = (minMax.max - minMax.min) + nodeWidth;
root._x = (chartWidth / 2) - nodePositioningHelper.horizontalCenter;
root._y = 0;
let treemap = d3.tree()
.size([chartWidth, 1])
.nodeSize([nodeWidth * 1.2, nodeHeight * 1.3]) // multiplier creates buffer between nodes
.separation((a, b) => a.parent == b.parent ? 1 : 2);
let treeData = treemap(root);
let nodes = treeData.descendants();
let links = treeData.descendants().slice(1);
/**
* Nodes
*/
nodes.forEach(d => {
// normalize node position
d.x = d.x + (chartWidth / 2) - nodePositioningHelper.horizontalCenter;
});
let node = acChartNodes.selectAll('div.ac-chart__node')
.data(nodes, d => d.data._id);
let nodeEnter = node.enter()
.append('div')
.style('width', nodeWidth)
.style('height', nodeHeight)
.attr('class', 'ac-chart__node')
.attr('data-depth', d => d.depth)
.attr('style', d => `transform: translate(${source._x}px, ${source._y}px`)
.on('click', seatEventHandler)
.html(d => generateNode(d));
let nodeUpdate = nodeEnter.merge(node);
nodeUpdate.transition()
.ease(animationEasement)
.duration(animationDuration)
.attr('style', d => `transform: translate(${d.x}px, ${d.y}px`);
let nodeExit = node.exit()
.transition()
.ease(animationEasement)
.duration(animationDuration)
.attr('style', d => `transform: translate(${source.x}px, ${source.y}px`)
.remove();
/**
* Links
*/
let link = acChartLinks.selectAll('.ac-chart__link')
.data(links, d => d.data._id);
let linkEnter = link.enter()
.insert('path', 'g')
.attr('d', d => {
const coordinates = [
{x: source._x + nodePositioningHelper.horizontalCenter, y: source._y},
{x: source._x + nodePositioningHelper.horizontalCenter, y: source._y},
{x: source._x + nodePositioningHelper.horizontalCenter, y: source._y},
{x: source._x + nodePositioningHelper.horizontalCenter, y: source._y}
];
return generateLine(coordinates);
})
.attr('class', 'ac-chart__link')
.attr('fill', 'none')
.attr('stroke-width', 2)
.attr('stroke', '#dcdcdc');
let linkUpdate = linkEnter.merge(link);
linkUpdate.transition()
.ease(animationEasement)
.duration(animationDuration)
.attr('d', d => {
const coordinates = [
{x: d.parent.x + nodePositioningHelper.horizontalCenter, y: d.parent.y + nodePositioningHelper.bottomCenter},
{x: d.parent.x + nodePositioningHelper.horizontalCenter, y: d.parent.y + nodePositioningHelper.bottomCenter + (nodePositioningHelper.linkHeight / 2)},
{x: d.x + nodePositioningHelper.horizontalCenter, y: d.y + nodePositioningHelper.topCenter - (nodePositioningHelper.linkHeight / 2)},
{x: d.x + nodePositioningHelper.horizontalCenter, y: d.y + nodePositioningHelper.topCenter}
];
return generateLine(coordinates);
});
let linkExit = link.exit()
.transition()
.ease(animationEasement)
.duration(animationDuration)
.attr('d', d => {
const coordinates = [
{x: source.x + nodePositioningHelper.horizontalCenter, y: source.y},
{x: source.x + nodePositioningHelper.horizontalCenter, y: source.y},
{x: source.x + nodePositioningHelper.horizontalCenter, y: source.y},
{x: source.x + nodePositioningHelper.horizontalCenter, y: source.y}
];
return generateLine(coordinates);
})
.remove();
nodes.forEach(d => {
d._x = d.x;
d._y = d.y;
});
}
function findXMinMax(node, min, max) {
const { children = [] } = node;
return children.reduce((result, childNode)=> {
if (childNode.children) {
return findXMinMax(childNode, result.min, result.max);
}
return { min: Math.min(childNode.x, result.min), max: Math.max(childNode.x, result.max) }
}, { min, max });
}
function generateNode(d) {
return `
<div class="mdc-card">
<section class="mdc-card__primary">
<h3 class="mdc-card__title mdc-card__title--large">${ d.data.name }</h3>
</section>
<section class="mdc-card__supporting-text">
</section>
<section class="mdc-card__actions">
<button class="mdc-button mdc-button--compact ac__collapse-seat">Toggle</button>
<!--<button class="mdc-button mdc-button--compact mdc-button--primary mdc-card__action ac__add-seat">Add Seat</button>-->
<!--<button class="mdc-button mdc-button--compact mdc-card__action">Remove Seat</button>-->
</section>
</div>
`;
}
function generateLine(path) {
return (d3.line()
.x(d => d.x)
.y(d => d.y))(path);
}
function seatEventHandler(d) {
collapseSeatEventHandler(d);
}
function collapseSeatEventHandler(d) {
if (d3.event.target.getAttribute('class') && d3.event.target.getAttribute('class').indexOf('ac__collapse-seat') >= 0) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
renderChart(d);
}
}
// document.onclick = e => {
// addSeatEventHandler(e);
// removeSeatEventHandler(e);
// collapseSeatEventHandler(e);
// }
function addSeatEventHandler(e) {
if (e.target.getAttribute('class') && e.target.getAttribute('class').indexOf('ac__add-seat') >= 0) {
console.log('add');
}
}
function removeSeatEventHandler(e) {
if (e.target.getAttribute('class') && e.target.getAttribute('class').indexOf('ac__remove-seat') >= 0) {
console.log('remove');
}
}
</script>
</body>
</html>