-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathchord.js
122 lines (106 loc) · 4.27 KB
/
chord.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
import {max, tau} from "./math.js";
function range(i, j) {
return Array.from({length: j - i}, (_, k) => i + k);
}
function compareValue(compare) {
return function(a, b) {
return compare(
a.source.value + a.target.value,
b.source.value + b.target.value
);
};
}
export default function() {
return chord(false, false);
}
export function chordTranspose() {
return chord(false, true);
}
export function chordDirected() {
return chord(true, false);
}
function chord(directed, transpose) {
var padAngle = 0,
sortGroups = null,
sortSubgroups = null,
sortChords = null;
function chord(matrix) {
var n = matrix.length,
groupSums = new Array(n),
groupIndex = range(0, n),
chords = new Array(n * n),
groups = new Array(n),
k = 0, dx;
matrix = Float64Array.from({length: n * n}, transpose
? (_, i) => matrix[i % n][i / n | 0]
: (_, i) => matrix[i / n | 0][i % n]);
// Compute the scaling factor from value to angle in [0, 2pi].
for (let i = 0; i < n; ++i) {
let x = 0;
for (let j = 0; j < n; ++j) x += matrix[i * n + j] + directed * matrix[j * n + i];
k += groupSums[i] = x;
}
k = max(0, tau - padAngle * n) / k;
dx = k ? padAngle : tau / n;
// Compute the angles for each group and constituent chord.
{
let x = 0;
if (sortGroups) groupIndex.sort((a, b) => sortGroups(groupSums[a], groupSums[b]));
for (const i of groupIndex) {
const x0 = x;
if (directed) {
const subgroupIndex = range(~n + 1, n).filter(j => j < 0 ? matrix[~j * n + i] : matrix[i * n + j]);
if (sortSubgroups) subgroupIndex.sort((a, b) => sortSubgroups(a < 0 ? -matrix[~a * n + i] : matrix[i * n + a], b < 0 ? -matrix[~b * n + i] : matrix[i * n + b]));
for (const j of subgroupIndex) {
if (j < 0) {
const chord = chords[~j * n + i] || (chords[~j * n + i] = {source: null, target: null});
chord.target = {index: i, startAngle: x, endAngle: x += matrix[~j * n + i] * k, value: matrix[~j * n + i]};
} else {
const chord = chords[i * n + j] || (chords[i * n + j] = {source: null, target: null});
chord.source = {index: i, startAngle: x, endAngle: x += matrix[i * n + j] * k, value: matrix[i * n + j]};
}
}
groups[i] = {index: i, startAngle: x0, endAngle: x, value: groupSums[i]};
} else {
const subgroupIndex = range(0, n).filter(j => matrix[i * n + j] || matrix[j * n + i]);
if (sortSubgroups) subgroupIndex.sort((a, b) => sortSubgroups(matrix[i * n + a], matrix[i * n + b]));
for (const j of subgroupIndex) {
let chord;
if (i < j) {
chord = chords[i * n + j] || (chords[i * n + j] = {source: null, target: null});
chord.source = {index: i, startAngle: x, endAngle: x += matrix[i * n + j] * k, value: matrix[i * n + j]};
} else {
chord = chords[j * n + i] || (chords[j * n + i] = {source: null, target: null});
chord.target = {index: i, startAngle: x, endAngle: x += matrix[i * n + j] * k, value: matrix[i * n + j]};
if (i === j) chord.source = chord.target;
}
if (chord.source && chord.target && chord.source.value < chord.target.value) {
const source = chord.source;
chord.source = chord.target;
chord.target = source;
}
}
groups[i] = {index: i, startAngle: x0, endAngle: x, value: groupSums[i]};
}
x += dx;
}
}
// Remove empty chords.
chords = Object.values(chords);
chords.groups = groups;
return sortChords ? chords.sort(sortChords) : chords;
}
chord.padAngle = function(_) {
return arguments.length ? (padAngle = max(0, _), chord) : padAngle;
};
chord.sortGroups = function(_) {
return arguments.length ? (sortGroups = _, chord) : sortGroups;
};
chord.sortSubgroups = function(_) {
return arguments.length ? (sortSubgroups = _, chord) : sortSubgroups;
};
chord.sortChords = function(_) {
return arguments.length ? (_ == null ? sortChords = null : (sortChords = compareValue(_))._ = _, chord) : sortChords && sortChords._;
};
return chord;
}