Public
Edited
May 12, 2023
1 fork
Insert cell
Insert cell
dataFile = FileAttachment("AllControversies_WeighteddDisa_Beeswarm.csv").csv()
Insert cell
data = dataFile.map((d) => {
return {
...d,
position: +d.AvgDW,
type: d.Ranking,
group: d.Controversy,
value: +d.SumT/250,
name: d.Topics,
};
})
Insert cell
viewof table = Inputs.table(data)
Insert cell
maxDisa = d3.max(data, (d)=>d.position)
Insert cell
colorScale = d3.scaleOrdinal(
[...new Set(data.map((d) => d.Ranking))],
d3.schemeTableau10
)
Insert cell
maxTw = d3.max(dataFile, (d)=>+d.SumT)
Insert cell
chart = beeswarm(data, "position", "group", "type", "value", "name")
Insert cell
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, 40])

const centerX = (d) => {
d.x = xScale(d[position]);return d.x;}

const centerY = (d) => {
d.y = yScale((d) => d[group])}


const xScale = d3.scaleLinear()
.domain([0, maxDisa])
.range([0, 700]);

const xAxis = d3.axisBottom().scale(xScale);

// y scale
const yScale = d3
.scaleBand()
.domain(categories)
.range([0, size.h - margin.top - margin.bottom]);
const yAxis = d3
.axisLeft()
.scale(yScale);
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", yScale((d) => d[group]))
.attr("cx", (d) => {
d.x = xScale(d[position]);return 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('x', d => d.x)
.attr('y', d => d.y)
.attr('font-size', 12)
.text(d => d.Topics);

let simulation = d3
.forceSimulation()
.force("collide", d3.forceCollide((d) => radius(d[value])+ padding))
.force("x", d3.forceX((d) => d.x).strength(1))
.alphaMin(0.00001);

if (categories) {
simulation.force("y", d3.forceY((d) => yScale(d[group])).strength(1));
} else {
simulation.force("y", d3.forceY((d) => d.y).strength(1));
}

simulation.nodes(data).on("tick", () => {
nodes.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
labels.attr("cx", (d) => d.x )
.attr("cy", (d) => d.y);
});

// Add y-axis
const yAxisG = box.append('g')
.call(yAxis);

// Adjust y-axis label position and text
yAxisG.selectAll('.tick text')
.attr('x', -2)
.attr('y', d => yScale(d[group]));

// Add y-axis label
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();
}
Insert cell
wrap = function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em")
while (word = words.pop()) {
line.push(word)
tspan.text(line.join(" "))
if (tspan.node().getComputedTextLength() > width) {
line.pop()
tspan.text(line.join(" "))
line = [word]
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", `${++lineNumber * lineHeight + dy}em`).text(word)
}
}
})}
Insert cell
d3 = require("d3@v6")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more