diff --git a/js/diagram.js b/js/diagram.js index 75aa163..67e71a6 100644 --- a/js/diagram.js +++ b/js/diagram.js @@ -111,6 +111,45 @@ const Diagram = { // case 'countryPopulation': // break; // } + + swap: (chords, i, j) => { + if (i < 0 || j < 0 || i === j) { + return chords; + } + + // Determine which arc is closer to 0, so earlier arc always swaps with later. + const f = (chords.groups[i].endAngle < chords.groups[j].endAngle ? i : j); + const s = (chords.groups[i].endAngle < chords.groups[j].endAngle ? j : i); + + const fst = Object.assign({}, chords.groups[f]); + const snd = Object.assign({}, chords.groups[s]); + + const fstAngle = fst.endAngle - fst.startAngle; + const sndAngle = snd.endAngle - snd.startAngle; + const offsetAngle = sndAngle - fstAngle; + + // Bring first forward. + chords.groups[f].startAngle = snd.endAngle - fstAngle; + chords.groups[f].endAngle = snd.endAngle; + // chords.groups[f].index = snd.index; + + // Bring second back. + chords.groups[s].startAngle = fst.startAngle; + chords.groups[s].endAngle = fst.startAngle + sndAngle; + // chords.groups[s].index = fst.index; + + // Bump other groups forward. + for (let ii = f + 1; ii < s; ii++) { + chords.groups[ii].startAngle += offsetAngle; + chords.groups[ii].endAngle += offsetAngle; + } + + // Swap array positions. + const tmp = chords.groups[f] + chords.groups[f] = chords.groups[s]; + chords.groups[s] = tmp; + + return chords; }, build: (data, eventIndex, metaMatrix, chordMatrix) => {