Skip to content

Instantly share code, notes, and snippets.

@CITguy
Created November 19, 2018 08:32
Show Gist options
  • Save CITguy/5b425cf9410360aaf620a12a0f10e263 to your computer and use it in GitHub Desktop.
Save CITguy/5b425cf9410360aaf620a12a0f10e263 to your computer and use it in GitHub Desktop.
Rapid D3.js - Section 6 - Word Cloud example code
var fill = d3.scale.category20();
var layout = d3.layout.cloud()
.size([500, 500])
.words([
"Hello", "world", "normally", "you", "want", "more", "words",
"than", "this"].map(function(d) {
return {text: d, size: 10 + Math.random() * 90, test: "haha"};
}))
.padding(5)
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw);
layout.start();
function draw(words) {
d3.select("body").append("svg")
.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", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
.style('fill', function (d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment