Last active
May 20, 2019 02:01
-
-
Save mariotacke/e652c124902edd41250085e6f96e8cf2 to your computer and use it in GitHub Desktop.
blog-real-time-word-cloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function init () { | |
const cloud = await getCloud(); | |
const highestScore = Math.max(...cloud.map((x) => x.size)); | |
var layout = d3.layout.cloud() | |
.size([500, 500]) | |
.words(cloud) | |
.rotate(function () { return ~~(Math.random() * 2) * 90; }) | |
.font("Impact") | |
.fontSize(function (d) { return d.size / highestScore * 90 }) | |
.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") | |
.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