Published
Edited
Jun 27, 2018
Fork of Word Cloud
1 fork
Insert cell
Insert cell
cloud = require("d3-cloud")
Insert cell
Insert cell
makecloud(
text.split(/ /).map(d => ({
text: d,
size: 10 + Math.random() * 90, // could be the number of occurrences
fill: color(Math.random()) // or something else
}))
)
Insert cell
Insert cell
// Adapted from https://github.com/jasondavies/d3-cloud/blob/master/examples/browserify.js
function makecloud(words) {
var a = DOM.svg();

var layout = cloud()
.size([500, 500])
.words(words)
.padding(5)
.rotate(d => ~~(Math.random() * 2) * 90)
.font("Impact")
.fontSize(d => d.size)
.on("end", draw);

layout.start();

function draw(words) {
d3.select(a)
.attr("width", layout.size()[0])
.attr("height", layout.size()[1])
.append("g")
.attr(
"transform",
"translate(" + layout.size()[0] / 2 + "," + layout.size()[1] / 2 + ")"
)
.selectAll("text")
.data(words)
.enter()
.append("text")
.style("font-size", d => `${d.size}px`)
.style("font-family", "Impact")
.attr("text-anchor", "middle")
.attr("transform", d => `translate(${[d.x, d.y]})rotate(${d.rotate})`)
.attr("fill", d => d.fill)
.text(d => d.text);
}

return a;
}
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more