-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpremchord.html
446 lines (386 loc) · 15.3 KB
/
premchord.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<!DOCTYPE html>
<html>
<head>
<!--Chord - Python wrapper around d3-chord
This package enables the generation of Chord diagrams. They can be saved
directly to HTML files or displayed in a Jupyter Notebook output cell.
Copyright 2020, Dr. Shahin Rostami
http://shahinrostami.com
https://github.com/shahinrostami/chord
https://pypi.org/project/chord/
-->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Chord Diagram</title>
<!-- Google Fonts -->
<script src="https://d3js.org/d3.v5.min.js"></script>
<link
href="https://fonts.googleapis.com/css?family=Lato:400,900"
rel="stylesheet"
type="text/css"
/>
<style>
.tippy-content {
font-family: "Lato", sans-serif;
}
#chart-1ab1bf6c {
font-size: 14px;
font-family: "Lato", sans-serif;
text-align: center;
fill: #454545;
}
#chart-1ab1bf6c svg {
max-width: 700px;
}
@media (min-width: 600px) {
#chart-1ab1bf6c{
font-size: 14px;
}
}
</style>
</head>
<body>
<div id="chart-1ab1bf6c" class="chord">
</div>
<script src="https://unpkg.com/@popperjs/core@2"></script>
<script src="https://unpkg.com/tippy.js@6"></script>
<script>
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://d3js.org/d3.v5.min.js";
script.onload = function () {
var script2 = document.createElement("script");
script2.type = "text/javascript";
script2.src = "https://shahinrostami.com/assets/chord/script.js";
script2.onload = function () {
margin = {
left: 50,
top: 50,
right: 50,
bottom: 50
};
width = Math.min(window.innerWidth, 700) - margin.left - margin.right;
height = Math.min(window.innerWidth, 700) - margin.top - margin.bottom;
innerRadius = Math.min(width, height) * 0.39;
outerRadius = innerRadius * 1.1;
tag_id = "chart-1ab1bf6c";
padding = 0.01;
Names = ['Bournemouth', 'Arsenal', 'Aston Villa', 'Brighton', 'Burnley', 'Chelsea', 'Palace', 'Everton', 'Leicester', 'Liverpool', 'Man City', 'Man United', 'Newcastle', 'Norwich', 'Sheffield', 'Southampton', 'Spurs', 'Watford', 'West Ham', 'Wolves'];
colors = ['#B50E12', '#EF0107', '#670E36', '#0057B8', '#99D6EA', '#034694', '#A7A5A6', '#003399', '#FDBE11', '#C8102E', '#6CABDD', '#DA291C', '#241F20', '#00A650', '#0D171A', '#D71920',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, 1, 1, 1, 0, 1, 0, 4, 1, 2, 3, 0, 3, 2, 0], [2, 3, 3, 0, 5, 4, 1, 3, 2, 1, 1, 4, 3, 1, 0, 1, 3, 0, 2, 0], [3, 3, 2, 0, 1, 1, 3, 0, 1, 3, 0, 1, 0, 3, 1, 1, 2, 2, 0, 0], [0, 1, 2, 0, 0, 0, 1, 0, 1, 3, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0]];
wrap_labels = false;
credit = false
////////////////////////////////////////////////////////////
/////////// Create scale and layout functions //////////////
////////////////////////////////////////////////////////////
var colors = d3
.scaleOrdinal()
.domain(d3.range(Names.length))
.range(colors);
//A "custom" d3 chord function that automatically sorts the order of the chords in such a manner to reduce overlap
var chord = customChordLayout()
.padding(padding)
.sortChords(d3.descending) //which chord should be shown on top when chords cross. Now the biggest chord is at the bottom
.matrix(matrix);
var arc = d3
.arc()
.innerRadius(innerRadius * 1.01)
.outerRadius(outerRadius);
var path = d3.ribbon().radius(innerRadius);
////////////////////////////////////////////////////////////
////////////////////// Create SVG //////////////////////////
///////////////////////////////////////////////////////////
var svg = d3
.select("#" + tag_id)
.append("svg")
.attr(
"viewBox",
"0 0 " +
(width + margin.left + margin.right) +
" " +
(height + margin.top + margin.bottom)
)
.attr("preserveAspectRatio", "xMinYMin meet")
.append("g")
.attr(
"transform",
"translate(" +
(width / 2 + margin.left) +
"," +
(height / 2 + margin.top) +
")"
);
d3.select("#" + tag_id)
.append("span")
.style("display", "block")
.style("font-size", "12px")
.style("text-align", "right")
.style("font-family", '"Arial", sans-serif')
.html(
'made with <a href="https://github.com/shahinrostami/chord">chord</a></span>'
);
if(credit){
d3.select("#" + tag_id).select("span")
.append("span")
.style("font-size", "12px")
.style("font-family", '"Arial", sans-serif')
.html(
' [<a href="https://twitter.com/shahinrostami">@ShahinRostami</a>]</span>'
);
}
////////////////////////////////////////////////////////////
/////////////// Create the gradient fills //////////////////
////////////////////////////////////////////////////////////
//Function to create the id for each chord gradient
function getGradID(d) {
return (
"linkGrad-" + tag_id + "-" + d.source.index + "-" + d.target.index
);
}
//Create the gradients definitions for each chord
var grads = svg
.append("defs")
.selectAll("linearGradient")
.data(chord.chords())
.enter()
.append("linearGradient")
.attr("id", getGradID)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", function (d, i) {
return (
innerRadius *
Math.cos(
(d.source.endAngle - d.source.startAngle) / 2 +
d.source.startAngle -
Math.PI / 2
)
);
})
.attr("y1", function (d, i) {
return (
innerRadius *
Math.sin(
(d.source.endAngle - d.source.startAngle) / 2 +
d.source.startAngle -
Math.PI / 2
)
);
})
.attr("x2", function (d, i) {
return (
innerRadius *
Math.cos(
(d.target.endAngle - d.target.startAngle) / 2 +
d.target.startAngle -
Math.PI / 2
)
);
})
.attr("y2", function (d, i) {
return (
innerRadius *
Math.sin(
(d.target.endAngle - d.target.startAngle) / 2 +
d.target.startAngle -
Math.PI / 2
)
);
});
//Set the starting color (at 0%)
grads
.append("stop")
.attr("offset", "0%")
.attr("stop-color", function (d) {
return colors(d.source.index);
});
//Set the ending color (at 100%)
grads
.append("stop")
.attr("offset", "100%")
.attr("stop-color", function (d) {
return colors(d.target.index);
});
////////////////////////////////////////////////////////////
////////////////// Draw outer Arcs /////////////////////////
////////////////////////////////////////////////////////////
var outerArcs = svg
.selectAll("g.group")
.data(chord.groups)
.enter()
.append("g")
.attr("class", "group")
.on("mouseover", fade(0.1, 1))
.on("mouseout", fade(opacityDefault, opacityDefault));
outerArcs
.append("path")
.style("fill", function (d) {
return colors(d.index);
})
.attr("d", arc)
.each(function (d, i) {
//Search pattern for everything between the start and the first capital L
var firstArcSection = /(^.+?)L/;
//Grab everything up to the first Line statement
var newArc = firstArcSection.exec(d3.select(this).attr("d"))[1];
//Replace all the comma's so that IE can handle it
newArc = newArc.replace(/,/g, " ");
//If the end angle lies beyond a quarter of a circle (90 degrees or pi/2)
//flip the end and start position
if (
(d.endAngle > (90 * Math.PI) / 180) &
(d.startAngle < (270 * Math.PI) / 180)
) {
var startLoc = /M(.*?)A/, //Everything between the first capital M and first capital A
middleLoc = /A(.*?)0 0 1/, //Everything between the first capital A and 0 0 1
endLoc = /0 0 1 (.*?)$/; //Everything between the first 0 0 1 and the end of the string (denoted by $)
//Flip the direction of the arc by switching the start en end point (and sweep flag)
//of those elements that are below the horizontal line
var newStart = endLoc.exec(newArc)[1];
var newEnd = startLoc.exec(newArc)[1];
var middleSec = middleLoc.exec(newArc)[1];
//Build up the new arc notation, set the sweep-flag to 0
newArc = "M" + newStart + "A" + middleSec + "0 0 0 " + newEnd;
} //if
//Create a new invisible arc that the text can flow along
svg
.append("path")
.attr("class", "hiddenArcs")
.attr("id", "arc-" + tag_id + "-" + i)
.attr("d", newArc)
.style("fill", "none");
});
////////////////////////////////////////////////////////////
////////////////// Append Names ////////////////////////////
////////////////////////////////////////////////////////////
//Append the label names on the outside
if (wrap_labels) {
outerArcs
.append("text")
.attr("class", "titles")
.attr("dy", function (d, i) {
return (d.endAngle > (90 * Math.PI) / 180) &
(d.startAngle < (270 * Math.PI) / 180)
? 25
: -16;
})
.append("textPath")
.attr("startOffset", "50%")
.style("text-anchor", "middle")
.attr("xlink:href", function (d, i) {
return "#arc-" + tag_id + "-" + i;
})
.text(function (d, i) {
return Names[i];
});
} else {
//Append the label names on the outside
outerArcs
.append("text")
.each(function (d) {
d.angle = (d.startAngle + d.endAngle) / 2;
})
.attr("dy", ".35em")
.attr("class", "titles")
.attr("text-anchor", function (d) {
return d.angle > Math.PI ? "end" : null;
})
.attr("transform", function (d) {
return (
"rotate(" +
((d.angle * 180) / Math.PI - 90) +
")" +
"translate(" +
(outerRadius + 10) +
")" +
(d.angle > Math.PI ? "rotate(180)" : "")
);
})
.text(function (d, i) {
return Names[i];
});
}
////////////////////////////////////////////////////////////
////////////////// Draw inner chords ///////////////////////
////////////////////////////////////////////////////////////
svg
.selectAll("path.chord")
.data(chord.chords)
.enter()
.append("path")
.attr("class", "chord")
.style("fill", function (d) {
return "url(#" + getGradID(d) + ")";
})
.style("opacity", opacityDefault)
.attr("d", path)
.on("mouseover", mouseoverChord())
.on("mouseout", mouseoutChord(opacityDefault, opacityDefault));
////////////////////////////////////////////////////////////
////////////////// Extra Functions /////////////////////////
////////////////////////////////////////////////////////////
//Returns an event handler for fading a given chord group.
function fade(opacityIn, opacityOut) {
return function (d, i) {
d3.select(this.ownerSVGElement)
.selectAll("path.chord")
.filter(function (d) {
return d.source.index !== i && d.target.index !== i;
})
.transition()
.style("opacity", opacityIn);
d3.select(this.ownerSVGElement)
.selectAll("path.chord")
.filter(function (d) {
return d.source.index == i || d.target.index == i;
})
.transition()
.style("opacity", opacityOut);
};
} //fade
//Highlight hovered over chord
function mouseoverChord() {
return function (d, i) {
d3.select(this.ownerSVGElement)
.selectAll("path.chord")
.transition()
.style("opacity", 0.1);
//Show hovered over chord with full opacity
d3.select(this).transition().style("opacity", 1);
tippy_content = "<span style='font-weight:900'>" +
Names[d.source.index] +
"</span> and <span style='font-weight:900'>" +
Names[d.target.index] +
"</span><br>occur together in <span style='font-weight:900'>" +
d.source.value +
"</span> instances";
tippy(this, {
allowHTML: true,
followCursor: true,
content: tippy_content,
size: "large",
arrow: true,
});
/*d3.select("#list")
.html(
details[d.source.index][d.target.index]
);*/
};
} //fade
//Bring all chords back to default opacity
function mouseoutChord(opacityIn, opacityOut) {
return function (d, i) {
d3.select(this.ownerSVGElement)
.selectAll("path.chord")
.transition()
.style("opacity", opacityOut);
};
//Set opacity back to default for all
} //function mouseoutChord
};
document.body.appendChild(script2);
};
document.body.appendChild(script);
</script>
<script></script>
</body>
</html>