beeswarm = (data, position, group, type, value, name) => {
const svg = d3.create("svg").attr("viewBox", [0, 0, 900, 700]);
let categories = [...new Set(data.map((d) => d[group]))];
const padding = 1;
const size = { w: 900, h: 600 };
const margin = { top: 20, bottom: 30, left: 130, right: 100 };
const radius = d3
.scaleSqrt()
.domain(d3.extent(data, (d) => d[value]))
.range([2, 40]);
const labelSize = d3
.scaleSqrt()
.domain(d3.extent(data, (d) => d[value]))
.range([2, 20]);
const textRadius = d3
.scaleSqrt()
.domain(d3.extent(data, (d) => d[value]))
.range([2, 10]);
const xScale = d3.scaleLinear().domain([0, maxDisa]).range([0, 700]);
const xAxis = d3.axisBottom().scale(xScale);
const yScale = d3
.scaleBand()
.domain(categories)
.range([0, size.h - margin.top - margin.bottom]);
const yAxis = d3.axisLeft().scale(yScale);
data.forEach((d) => {
d.x = xScale(d[position]);
d.y = yScale(d[group]);
});
const box = svg
.append("g")
.attr("class", "box")
.attr("transform", "translate(100, 80)");
const nodes = box
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("class", "node")
.attr("cy", (d) => d.y)
.attr("cx", (d) => d.x)
.attr("r", (d) => radius(d[value]))
.style("fill", (d) => colorScale(d[type]));
const labels = box
.selectAll("text")
.data(data)
.enter()
.append("text")
.attr("text-anchor", "middle")
.attr("x", (d) => d.x)
.attr("y", (d) => d.y)
.attr("font-size", (d) => textRadius(d[value]))
.text((d) => d.Topics);
let simulation = d3
.forceSimulation()
.force(
"collide",
d3.forceCollide((d) => radius(d[value]) + padding)
)
.force(
"x",
d3.forceX((d) => d.x)
)
.force("y", d3.forceY((d) => d.y).strength(1))
.alphaMin(0.00001);
if (categories) {
simulation.force("y", d3.forceY((d) => yScale(d[group])).strength(1));
}
simulation.nodes(data).on("tick", () => {
nodes.attr("cx", (d) => d.x).attr("cy", (d) => d.y);
labels.attr("x", (d) => d.x).attr("y", (d) => d.y);
});
const yAxisG = box.append("g").call(yAxis);
yAxisG
.selectAll(".tick text")
.attr("x", -2)
.attr("y", (d) => yScale(d[group]));
box.append("text").attr("class", "axis-label").attr("x", 0).attr("y", 0);
box
.append("g")
.attr("transform", `translate(0, ${size.h - margin.top})`)
.style("font-size", "12px")
.style("font-family", "Spectral")
.call(xAxis)
.select(".domain")
.remove();
return svg.node();
}