Skip to content

Instantly share code, notes, and snippets.

@EvanYellow
Last active December 25, 2015 14:08
Show Gist options
  • Save EvanYellow/6988424 to your computer and use it in GitHub Desktop.
Save EvanYellow/6988424 to your computer and use it in GitHub Desktop.
d3 keyword
//<script type="text/javascript" src="${resroot}/javascripts/lib/d3.v3.min.js"></script>
//<script type="text/javascript" src="${resroot}/javascripts/lib/d3.layout.cloud.js"></script>
/**
* EX: arr = [{"text":123,"size":12}]
* */
var arr = [];
var fill = d3.scale.category20();
d3.layout.cloud().size([700, 250])
.words(arr)
.rotate(function() { return Math.ceil(Math.random()*40)-20; })
.font("Impact")
.fontSize(function(d) { return d.size > 100? 100 : (d.size < 30 ? 30 : d.size); })
.on("end", draw)
.start();
function draw(words) {
words2png = words;
d3.select("[data-type='content']").append("svg")
.attr("width", 700)
.attr("height", 300)
.append("g")
.attr("transform", "translate(350, 170)")
.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; })
.style("opacity", 1e-6)
.transition()
.duration(1000)
.style("opacity", 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment