Skip to content

Instantly share code, notes, and snippets.

@aaizemberg
Last active December 14, 2017 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaizemberg/60741fee4781955f3edd6bb0a35a4d37 to your computer and use it in GitHub Desktop.
Save aaizemberg/60741fee4781955f3edd6bb0a35a4d37 to your computer and use it in GitHub Desktop.
word cloud | tag cloud | nube de palabras
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>word cloud | tag cloud | nube de palabras</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
<script src="https://cdn.rawgit.com/jasondavies/d3-cloud/v1.2.1/build/d3.layout.cloud.js"></script>
</head>
<body>
<script>
var colores = d3.schemeCategory20;
function fill(i, vector) {
return vector[i % vector.length];
}
var data = [
{text:"Hola", size: 10 + Math.random() * 30},
{text:"Mundo", size: 10 + Math.random() * 30},
{text:"esto", size: 10 + Math.random() * 30},
{text:"es", size: 10 + Math.random() * 30},
{text:"un", size: 10 + Math.random() * 30},
{text:"word cloud", size: 10 + Math.random() * 30},
{text:"tag cloud", size: 10 + Math.random() * 30},
{text:"d3", size: 10 + Math.random() * 30},
{text:"layout-algorithm", size: 10 + Math.random() * 30},
{text:"gracias", size: 10 + Math.random() * 30},
{text:"Jason Davies", size: 10 + Math.random() * 30},
{text:"nube de palabras", size: 50}
];
var layout = d3.layout.cloud()
.size([500, 500])
.words(data)
.padding(10)
.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,colores); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment