chart = {
const svg = d3.select(DOM.svg(width, 1750));
const tooltip = d3.select("body").append("div").attr("class", "tooltip");
const path = svg
.append("path")
.attr(
"d",
`m110 80 ${lineWidth},0 0,80 -${lineWidth},0 0,80 ${lineWidth}, 0 0,80 -${lineWidth},0 0, 80 ${lineWidth},0 0,80 -${lineWidth},0 0,80 ${lineWidth}, 0 0,80 -${lineWidth},0 0, 80 ${lineWidth},0 0,80 -${lineWidth},0 0,80 ${lineWidth}, 0 0,80 -${lineWidth},0 0, 80 ${lineWidth},0 0,80 -${lineWidth},0 0,80 ${lineWidth}, 0 0,80 -${lineWidth},0 0, 80 ${lineWidth},0 0,80 -${lineWidth},0 0,80 ${lineWidth}, 0 0,80 -${lineWidth},0 0, 80 ${lineWidth}, 0`
)
.attr("stroke", "#ededed")
.attr("fill", "none")
.attr("stroke-width", 2);
const pathLength = path.node().getTotalLength();
const pathNode = path.node();
const t = svg.transition().duration(500);
const x = d3
.scaleLinear()
.domain(d3.extent(data, (d) => d.year))
.range([0, pathLength]);
const xYears = d3.scaleLinear().domain(years).range([0, pathLength]);
const dataNew = _.map(dataRaw, (d) => {
return {
...d,
year: +d.year,
xPos: pathNode.getPointAtLength(x(d.year)).x,
yPos: pathNode.getPointAtLength(x(d.year)).y,
color: colorScale(d.cat)
};
});
const uniqueDataCalc = _.map(uniqueData, (d) => {
return {
...d,
year: +d.year,
xPos: pathNode.getPointAtLength(x(d.year)).x,
yPos: pathNode.getPointAtLength(x(d.year)).y
};
});
const r = 5;
const simulation = d3
.forceSimulation(dataNew)
.force(
"x",
d3
.forceX()
.x((d) => d.xPos)
.strength(1)
)
.force(
"y",
d3
.forceY()
.y((d) => d.yPos)
.strength(0.8)
)
.force("collide", d3.forceCollide(r + 1))
.stop();
_.times(1000, () => {
simulation.tick();
});
svg
.selectAll("text")
.data(uniqueDataCalc)
.join("text")
.attr("fill", "#cfcccc")
.attr("x", (d) => d.xPos - 17)
.attr("y", (d) => (d.year > 1939 ? d.yPos + 38 : d.yPos + 30))
.text((d) => d.year);
const circles = svg
.selectAll("circle")
.data(dataNew)
.join("circle")
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.attr("r", r)
.attr("fill", (d) => d.color)
.attr("stroke", (d) =>
d.gender === "Female African American Firsts" ? "#3e1a24" : "#F8F8F8"
);
circles.on("mouseover", (event, d) => {
const current = event.currentTarget;
d3.select(current)
.raise()
.transition()
.attr("r", r + 3);
tooltip
.style("visibility", "visible")
.style("top", event.y + "px")
.style("left", event.x + "px")
.html(`<div>${d.person} </div> <div>..........<div/> <div>${d.accomplishment} </div>
`);
});
circles.on("mouseout", (event, d) => {
const current = event.currentTarget;
d3.select(current).transition().attr("r", r);
tooltip.style("visibility", "hidden");
});
return svg.node();
}