Skip to content

Instantly share code, notes, and snippets.

@k12ish
Created January 22, 2022 20:27
Show Gist options
  • Save k12ish/cb89d5cc140ea375da26a906691c2d90 to your computer and use it in GitHub Desktop.
Save k12ish/cb89d5cc140ea375da26a906691c2d90 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://rawgit.com/jasondavies/d3-cloud/master/build/d3.layout.cloud.js"></script>
<script>
//Based on https://github.com/jasondavies/d3-cloud/blob/master/examples/simple.html
// Encapsulate the word cloud functionality
function wordCloud(selector) {
var fill = d3.scale.category20();
//Construct the word cloud's SVG element
var svg = d3.select(selector).append("svg")
.attr("width", 500)
.attr("height", 500)
.append("g")
.attr("transform", "translate(250,250)");
//Draw the word cloud
function draw(words) {
var cloud = svg.selectAll("g text")
.data(words, function (d) { return d.text; })
//Entering words
cloud.enter()
.append("text")
.style("font-family", "Impact")
.style("fill", function (d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr('font-size', 1)
.text(function (d) { return d.text; });
//Entering and existing words
cloud
.transition()
.duration(600)
.style("font-size", function (d) { return d.size + "px"; })
.attr("transform", function (d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.style("fill-opacity", 1);
//Exiting words
cloud.exit()
.transition()
.duration(200)
.style('fill-opacity', 1e-6)
.attr('font-size', 1)
.remove();
}
//Use the module pattern to encapsulate the visualisation code. We'll
// expose only the parts that need to be public.
return {
//Recompute the word cloud for a new set of words. This method will
// asycnhronously call draw when the layout has been computed.
//The outside world will need to call this function, so make it part
// of the wordCloud return value.
update: function (words) {
d3.layout.cloud().size([500, 500])
.words(words)
.padding(5)
.rotate(function () { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function (d) { return d.size; })
.on("end", draw)
.start();
}
}
}
function render_wordcloud(arr, selector) {
wordCloud(selector).update(arr)
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment