Published unlisted
Edited
Jul 22, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function WordCloud(data, height=400, { invalidation } = {}) {
// Local, simplified adaptation of '@d3/word-cloud'
// const height = 400;
const margin = {top: 10, right: 10, bottom: 10, left: 10};
const fontScale = 10;
const sizeMax = d3.max(data, d => d.size);

// console.log(sizeMax)
// let styleString = "max-width: 100%; height: auto; height: intrinsic;";
// styleString += bgColor ? `background-color: ${bgColor};` : "";
// styleString += shadow ? `box-shadow: ${shadow};` : "";
// styleString += border ? `border: ${border};` : "";
// styleString += borderRadius ? `border-radius: ${borderRadius}px;` : "";
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.attr("width", width)
// .attr("font-family", fontFamily)
.attr("text-anchor", "middle")
// .attr("style", styleString);

const g = svg.append("g").attr("transform", `translate(${margin.left},${margin.top})`);

const cloud = d3Cloud()
.size([width - margin.left - margin.right, height - margin.top - margin.bottom])
.words(data)
.padding(20)
.rotate(0)
.fontSize(d => Math.sqrt(d.size) * fontScale)
.on("word", d => g.append("text")
.attr("font-size", d.size)
.attr("transform", `translate(${d.x},${d.y}) rotate(${d.rotate})`)
.attr("fill-opacity", `${(d.size/sizeMax)*100}%`)
.text(d.text))

console.log(cloud)
cloud.start();
invalidation && invalidation.then(() => cloud.stop());
return svg.node();
}
Insert cell
Insert cell
Insert cell
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