import Sorter from './sorter'; import Matrices from './matrices'; const Diagram = { clear: () => d3.select('svg').selectAll("*").remove(), swapGroupArcs: (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; } chords.forEach(v => { if (v.source.index === f) { v.source.startAngle += (snd.endAngle - fst.endAngle); v.source.endAngle += (snd.endAngle - fst.endAngle); } if (v.target.index === f) { v.target.startAngle += (snd.endAngle - fst.endAngle); v.target.endAngle += (snd.endAngle - fst.endAngle); } if (v.source.index === s) { v.source.startAngle -= (snd.startAngle - fst.startAngle); v.source.endAngle -= (snd.startAngle - fst.startAngle); } if (v.target.index === s) { v.target.startAngle -= (snd.startAngle - fst.startAngle); v.target.endAngle -= (snd.startAngle - fst.startAngle); } if (v.source.index > f && v.source.index < s) { v.source.startAngle += offsetAngle; v.source.endAngle += offsetAngle; } if (v.target.index > f && v.target.index < s) { v.target.startAngle += offsetAngle; v.target.endAngle += offsetAngle; } if ((v.source.index === f || v.source.index === s) && (v.target.index === f || v.target.index === s)) { const tmp = v.source.index; v.source.index = v.target.index; v.target.index = tmp; } else if (v.source.index === f) { v.source.index = s; } else if (v.target.index === f) { v.target.index = s; } else if (v.source.index === s) { v.source.index = f; } else if (v.target.index === s) { v.target.index = f; } }); // Swap array positions. const tmp = chords.groups[f] chords.groups[f] = chords.groups[s]; chords.groups[s] = tmp; return chords; }, swapGroups: (data, eventKey, chords, i, j) => { Diagram.swapGroupArcs(chords, i, j); Matrices.swapIndices(data.tourneys[eventKey].teams, i, j); return chords; }, getCountryName: (data, eventKey, n) => { const team = data.tourneys[eventKey].teams[n]; return data.countries[team.cId]; }, getPopulation: (data, eventKey, n) => { const team = data.tourneys[eventKey].teams[n]; return team.p; }, getGoalsFor: (data, eventKey, n) => { const team = data.tourneys[eventKey].teams[n]; return team.gf; }, getGoalsAgainst: (data, eventKey, n) => { const team = data.tourneys[eventKey].teams[n]; return team.ga; }, build: (data, eventKey, sort, SORT_TYPES, matrix) => { const svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height"), outerArcThickness = 5, outerRadius = Math.min(width, height) * 0.5 - 125, innerRadius = outerRadius - outerArcThickness; const chords = d3.chord() .padAngle(0.05) .call(null, matrix); let sortedChords = chords; switch (sort) { case SORT_TYPES.COUNTRY: sortedChords = Sorter.sort(chords, 0, chords.groups.length - 1, Diagram.getCountryName.bind(null, data, eventKey), Diagram.swapGroups.bind(null, data, eventKey)); break; case SORT_TYPES.GOALS: sortedChords = Sorter.sort(chords, 0, chords.groups.length - 1, Diagram.getGoalsFor.bind(null, data, eventKey), Diagram.swapGroups.bind(null, data, eventKey)); break; case SORT_TYPES.POPULATION: sortedChords = Sorter.sort(chords, 0, chords.groups.length - 1, Diagram.getPopulation.bind(null, data, eventKey), Diagram.swapGroups.bind(null, data, eventKey)); break; } const arc = d3.arc() .innerRadius(innerRadius) .outerRadius(outerRadius); const ribbon = d3.ribbon() .radius(innerRadius); // const color = d3.scaleOrdinal(d3.schemeCategory20); // const color = d3.scaleOrdinal(d3.schemeCategory10); const len = data.tourneys[eventKey].teams.length; // const color = d3.scaleLinear().domain([0, len]).range(["#edf8b1", "#081d58"]).interpolate(d3.interpolateRgb); // const color = d3.scaleLinear().domain([0, len]).range(["#aaa", "green"]).interpolate(d3.interpolateRgb); const color = d3.scaleLinear().domain([0, len]).range(["gainsboro", "darkgreen"]).interpolate(d3.interpolateRgb); // const colors = ["#ffffcc","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#0c2c84"]; // const colors = ["#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"]; // const color = i => colors[i % colors.length]; const g = svg.append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")") .datum(sortedChords); const group = g.append("g") .attr("class", "groups") .selectAll("g") .data(chords => chords.groups) .enter().append("g"); group.append("path") .style("fill", d => color(d.index)) .style("stroke", d => d3.rgb(color(d.index)).darker()) .attr("d", arc); g.append("g") .attr("class", "ribbons") .selectAll("path") .data(function(chords) { return chords; }) .enter().append("path") .attr("d", ribbon) .attr("data-round-id", (d) => { const t1 = data.tourneys[eventKey].teams[d.source.index]; const t2 = data.tourneys[eventKey].teams[d.target.index]; const game = data.tourneys[eventKey].games.find(v => { return (v.t1 === t1.tId || v.t1 === t2.tId) && (v.t2 === t1.tId || v.t2 === t2.tId); }); return game.rId; }) .style("fill", (d) => color(d.target.index)) .style("stroke", (d) => d3.rgb(color(d.target.index)).darker()) .classed("ribbon", true) .append("title") .text(function(d) { const t1 = data.tourneys[eventKey].teams[d.source.index]; const t2 = data.tourneys[eventKey].teams[d.target.index]; const c1 = data.countries[t1.cId]; const c2 = data.countries[t2.cId]; const game = data.tourneys[eventKey].games.find(v => { return (v.t1 === t1.tId || v.t1 === t2.tId) && (v.t2 === t1.tId || v.t2 === t2.tId); }); const e1 = game.se1 ? `(+${game.se1} in extended time)` : ''; const e2 = game.se2 ? `(+${game.se2} in extended time)` : ''; const p1 = game.sp1 ? `(+${game.sp1} in penalties)` : ''; const p2 = game.sp2 ? `(+${game.sp2} in penalties)` : ''; return `${c1}: ${game.s1} ${e1} ${p1}\n${c2}: ${game.s2} ${e2} ${p2}`; }); group.append("text") .each(function(d) { d.angle = (d.startAngle + d.endAngle) / 2; }) .attr("dy", ".35em") .attr("transform", function(d) { return "rotate(" + (d.angle * 180 / Math.PI - 91) + ")" + "translate(" + (innerRadius + 26) + ")" + (d.angle > Math.PI ? "rotate(180)" : ""); }) .style("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; }) .text(function(d) { const team = data.tourneys[eventKey].teams[d.index]; let metric = ''; switch (sort) { case SORT_TYPES.GOALS: metric = `(${team.gf})`; break; case SORT_TYPES.POPULATION: metric = `(${Number(team.p).toLocaleString()})`; break; } return `${data.countries[team.cId]} ${metric}`; }); }, }; export default Diagram;