Skip to content

Commit

Permalink
Treat the larger as the source.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 4, 2019
1 parent 6c431bb commit 538cc63
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Each source and target subgroup is also an object with the following properties:
* `value` - the flow value *matrix*[*i*][*j*]
* `index` - the node index *i*

The chords are typically passed to [d3.ribbon](#ribbon) to display the network relationships. The returned array includes only chord objects for which the value *matrix*[*i*][*j*] or *matrix*[*j*][*i*] is non-zero. Furthermore, the returned array only contains unique chords: a given chord *ij* represents the bidirectional flow from *i* to *j* *and* from *j* to *i*, and does not contain a duplicate chord *ji*.
The chords are typically passed to [d3.ribbon](#ribbon) to display the network relationships. The returned array includes only chord objects for which the value *matrix*[*i*][*j*] or *matrix*[*j*][*i*] is non-zero. Furthermore, the returned array only contains unique chords: a given chord *ij* represents the bidirectional flow from *i* to *j* *and* from *j* to *i*, and does not contain a duplicate chord *ji*; *i* and *j* are chosen such that the chord’s source always represents the larger of *matrix*[*i*][*j*] and *matrix*[*j*][*i*].

The *chords* array also defines a secondary array of length *n*, *chords*.groups, where each group represents the combined outflow for node *i*, corresponding to the elements *matrix*[*i*][0 … *n* - 1], and is an object with the following properties:

Expand Down
10 changes: 8 additions & 2 deletions src/chord.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ function chord(directed, transpose) {
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) {
const chord = chords[i * n + j] || (chords[i * n + j] = {source: null, target: null});
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 {
const chord = chords[j * n + i] || (chords[j * n + i] = {source: null, target: null});
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]};
}
Expand Down

0 comments on commit 538cc63

Please sign in to comment.