chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const {nodes, links} = sankey({
nodes: graph.nodes.map(d => Object.assign({}, d)),
links: graph.links.map(d => Object.assign({}, d))
});
let div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("display", "none")
.style('z-index', '10')
.text("new tooltip");
svg.append("g")
.selectAll("rect")
.data(nodes)
.join("rect")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("height", d => d.y1 - d.y0)
.attr("width", d => d.x1 - d.x0)
.append("title")
.text(d => `${d.name}\n${d.value.toLocaleString()}`);
svg.append("g")
.attr("fill", "none")
.selectAll("g")
.data(links)
.join("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("class", function(d){
var text = "";
var i;
for(i=0; i < d.names.length; i++){
text = text + " " + d.names[i];
}
console.log(text);
return text;
})
.attr("stroke", function(d){
//console.log(d.names);
//console.log(color(d.names));
return color(d.names[0]); })
.attr("stroke-width", d => d.width)
.style("mix-blend-mode", "multiply")
.on("mouseover", function (e, i) {
d3.select(this).transition()
.duration("50")
.attr("stroke",'blue')
.attr("opacity", "0.7");
div.html(d => i.names.join(" → ") + "<br>" + Math.round(i.value) + "(" + i.value.toLocaleString() + ")")
.style("display", "block");
var parent = d3.selectAll(".Male")
.attr("color","red")
console.log(parent)
})
.on("mousemove", function (e) {
div.style("top", e.pageY - 48 + "px")
.style("left", e.pageX + "px");
})
.on('mouseout', function () {
d3.select(this).transition()
.duration('50')
.attr("stroke", function(d){ console.log(d.names);
console.log(color(d.names));
return color(d.names[0]); })
.attr('opacity', '1');
div.style("display", "none");
})
.append("title")
.text(d => d.names.join(" → ") + "\n" + "" + d.value.toLocaleString() + " ");
svg.append("g")
.style("font", "10px sans-serif")
.selectAll("text")
.data(nodes)
.join("text")
.attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name)
.attr("class", "t1")
.append("tspan")
.attr("fill-opacity", 0.7)
.attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
.attr("y", d => (d.y1 + d.y0) / 2 + 12)
.text(d => `${d.value.toLocaleString()}`)
.attr("class", "t2");
return svg.node();
}