chart = {
const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("font-size", 10)
.attr("font-family", "sans-serif")
.style("width", "100%")
.style("height", "auto");
const chords = chord(data);
const group = svg.append("g")
.selectAll("g")
.data(chords.groups)
.enter().append("g");
const groupPath = group.append("path")
.attr("class", "group")
.attr("fill", d => color(d.index))
.attr("stroke", d => d3.rgb(color(d.index)).darker())
.attr("d", arc)
.attr("id", function(d, i) { return "group" + d.index; })
.on("mouseover", fade(.1))
.on("mouseout", fade(opacityDefault));
group.append("text")
.attr("x", 5)
.attr("dy", 15)
.append("textPath")
.attr("xlink:href", function(d) { return "#group" + d.index; })
.text(function(chords, i){return names[i];})
.style("fill", "black");
// group.append("text")
// .each(d => { d.angle = (d.startAngle + d.endAngle) / 2; })
// .attr("dy", "100")
// .attr("transform", d => `
// rotate(${(d.angle * 180 / Math.PI - 90)})
// translate(${innerRadius + 26})
// ${d.angle > Math.PI ? "rotate(180)" : ""}`)
// .attr("text-anchor", d => d.angle > Math.PI ? "end" : null)
// .text(d => names[d.index]);
const groupTick = group.append("g")
.selectAll("g")
.data(d => groupTicks(d, 1e3))
.join("g")
.attr("transform", d => `rotate(${d.angle * 180 / Math.PI - 90}) translate(${outerRadius},0)`);
groupTick.append("line")
.attr("stroke", "#000")
.attr("x2", 6);
groupTick
.filter(d => d.value % 5e3 === 0)
.append("text")
.attr("x", 8)
.attr("dy", ".35em")
.attr("transform", d => d.angle > Math.PI ? "rotate(180) translate(-16)" : null)
.attr("text-anchor", d => d.angle > Math.PI ? "end" : null)
.text(d => formatValue(d.value));
const ribbons = svg.append("g")
.attr("fill-opacity", 0.67)
.selectAll("path")
.data(chords)
.enter().append("path")
.attr("class", "ribbons")
.attr("d", ribbon)
.attr("fill", d => color(d.target.index))
.attr("stroke", d => d3.rgb(color(d.target.index)).darker())
.on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", opacityDefault);
tooltip.html(d.source.value + " videos in " + names[d.source.index] + " are " + names[d.target.index])
.style("left", (d3.event.pageX + 5) + "px")
.style("top", (d3.event.pageY - 28) + "px");
// fade other ribbons
ribbons
.filter(dd => dd !== d)
.transition()
.style('opacity', 0.1);
// fade other groups
groupPath
.filter((dd,i) => dd.index !== d.source.index && dd.index !== d.target.index)
.transition()
.style("opacity", 0.1);
})
.on("mouseout", function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
// unfade ribbons
ribbons
.transition()
.style('opacity', opacityDefault);
// unfade groups
groupPath
.transition()
.style("opacity", opacityDefault);
});
//Returns an event handler for fading a given chord group.
function fade(opacity) {
return function(d,i) {
ribbons
.filter(function(dd) { return dd.source.index != d.index && dd.target.index != d.index; })
.transition()
.style("opacity", opacity);
// fade all other groups
groupPath
.filter(function(dd) { return dd.index != d.index; })
.transition()
.style("opacity", opacity);
};
}
return svg.node();
}