drawChart = () => {
d3.selectAll("#nodes_chart svg").remove();
generateData();
const svg = d3
.select("#nodes_chart")
.append("svg")
.attr("width", WIDTH)
.attr("height", HEIGHT)
.attr("shape-rendering", "geometricPrecision")
.style("background-color", "#FFFDE7")
.style("border", "2px solid #D7CCC8")
.style("border-radius", "8px");
addRoomsTo(svg);
const links = svg
.append("g")
.selectAll()
.data(dataLinks)
.join("path")
.attr("class", "link")
.attr("id", (_, i) => linkId(i))
.attr("from", (d) => `${d.from}`)
.attr("to", (d) => `${d.to}`)
.attr("d", (d) =>
linkType === "Horizontal"
? d3.linkHorizontal()(d.link)
: d3.linkVertical()(d.link)
)
.attr("fill", "none")
.attr("stroke", (d) => linkColor[d.master])
.on("mouseover", function () {
const link = d3.select(this).raise();
link.attr("stroke-width", strong.stroke);
let node = d3.select(link.attr("from")).raise();
node.attr("r", strong.r * UNIT);
node.attr("stroke-width", strong.stroke);
node = d3.select(link.attr("to")).raise();
node.attr("r", strong.r * UNIT);
node.attr("stroke-width", strong.stroke);
})
.on("mouseout", function (d) {
const link = d3.select(this);
d3.select(link.attr("from")).attr("stroke-width", 1).attr("r", UNIT);
link.attr("stroke-width", 1);
d3.select(link.attr("to")).attr("stroke-width", 1).attr("r", UNIT);
})
.append("title")
.text((d, i) => `${linkId(i)} :: from ${d.from} to ${d.to}`);
const nodes = svg
.append("g")
.selectAll()
.data(dataNodes)
.join("circle")
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.attr("r", UNIT)
.attr("class", "node")
.attr("id", (d) => d.id)
.attr("fill", (_, i) => d3.schemePastel1[i % d3.schemePastel1.length])
.attr(
"stroke",
(_, i) => d3.schemeCategory10[i % d3.schemeCategory10.length]
)
.on("mouseover", function () {
const node = d3
.select(this)
.raise()
.attr("r", strong.r * UNIT)
.attr("stroke-width", strong.stroke);
d3.selectAll(`[from=${node.attr("id")}],[to=${node.attr("id")}]`).each(
function (d, i) {
const link = d3
.select(this)
.raise()
.attr("stroke-width", strong.stroke);
d3.select(
`#${
link.attr("from") === node.attr("id")
? link.attr("to")
: link.attr("from")
}`
)
.raise()
.attr("r", strong.r * UNIT)
.attr("stroke-width", strong.stroke);
}
);
})
.on("mouseout", function () {
const node = d3.select(this).attr("r", UNIT).attr("stroke-width", 1);
d3.selectAll(`[from=${node.attr("id")}],[to=${node.attr("id")}]`).each(
function (d, i) {
const link = d3.select(this).attr("stroke-width", 1);
d3.select(
`#${
link.attr("from") === node.attr("id")
? link.attr("to")
: link.attr("from")
}`
)
.attr("r", UNIT)
.attr("stroke-width", 1);
}
);
})
.append("title")
.text((d, i) => `${d.id} :: [${d.x}, ${d.y}]`);
return svg.node();
}