{
const container = d3
.create("div")
.attr("width", width)
.attr("height", height)
.attr("class", "container");
const imgContainer = container
.append("div")
.attr("width", width)
.attr("height", height)
.attr("class", "imgContainer");
const svg = container
.append("svg")
.attr("width", width)
.attr("height", height);
const links = Array.from(links_array);
const nodes = [...nodes_array];
const toolTip = d3.select("body").append("div").attr("class", "toolTip");
const yScale = d3
.scaleLinear()
.domain([1980, 2022])
.range([30, height - 30]);
// center — pulls all nodes to the center
// charge — nodes repel from each other which prevents overlap
// link — specifies that id is the link variable
const simulation = d3
.forceSimulation(nodes)
.force(
"link",
d3
.forceLink(links)
.id((d) => d.id)
.strength(1.5)
.distance(22)
)
// .force("link", d3.forceLink(links).id(d => d.id).strength(1).distance(10))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("charge", d3.forceManyBody().strength(-172)) // -400
// .force("collide", d3.forceCollide(10))
.force("x", d3.forceX())
.force("y", d3.forceY());
// .force('y', d3.forceY().y(d => yScale(d.year)))
// .force('collision', d3.forceCollide().radius(function(d) {
// return d.radius;
// }))
const array = [];
const link = svg
.append("g")
.selectAll("line")
.data(links)
.join("line")
.style("stroke", blue)
.attr("stroke-width", 1.2)
// .style("stroke", "rgba(255,255,255,1)")
// .attr("stroke-width", .75)
.on("mousemove", function (event, d, index) {
toolTip
.style("left", event.pageX + 18 + "px")
.style("top", event.pageY + 18 + "px")
.style("display", "block").html(`<div>
<div class="tooltip-movie">${d.data.name} (${d.data.year})</div>
<div class="tooltip-director">${d.data.director} <span>director</span></div>
<div class="tooltip-star">${d.data.star} </div>
</div>`);
// .html(`Tooltip for <strong>${d.source.id} - ${d.target.id}</strong>`);
// d3.select(this).attr("stroke-width", 3.8);
// console.log(d);
link.attr("stroke-width", (d2) =>
d2.data.name === d.data.name ? 3.8 : 1.2
);
})
.on("mouseout", function () {
toolTip.style("display", "none");
link.attr("stroke-width", 1.2);
// d3.select(this).attr("stroke-width", 1.1);
});
// const link = svg.append("g")
// .attr("fill", "none")
// .selectAll("path")
// .data(links)
// .join("path")
// .attr("stroke", '#1fd5d5')
// .attr("stroke-width", 1)
const node = svg
.append("g")
.attr("fill", "currentColor")
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round")
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation));
node
.append("text")
.text((d) => (d.type !== "name" ? d.id : ""))
// .attr('dx', 6)
.attr("text-anchor", "middle")
// .style('fill', d => d.type === 'director' ? '#333' : blue) //008fb2
.style("fill", (d) => (d.type === "director" ? "#efefef" : "#a9ecfd")) //008fb2
.attr("class", (d) => (d.type === "director" ? "director" : "star"))
.style("opacity", (d) =>
d.type === "director" ? 1 : d.type === "star" ? 1 : 0
)
.clone(true)
.lower()
.attr("stroke", "rgba(0,0,0,0.95)")
.attr("stroke-width", 2.2)
.on("mousemove", function (event, d, index) {
d3.select(this).style("opacity", 1);
});
simulation.on("tick", () => {
link
.attr("x1", (d) => d.source.x)
.attr("y1", (d) => d.source.y)
.attr("x2", (d) => d.target.x)
.attr("y2", (d) => d.target.y);
// link.attr("d", linkArc);
node.attr("transform", (d) => `translate(${d.x},${d.y})`);
});
return container.node();
}