Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an overflow option that will draw words even if they don't fit #37

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion d3.layout.cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
timeInterval = Infinity,
event = d3.dispatch("word", "end"),
timer = null,
overflow = false,
cloud = {};

cloud.start = function() {
Expand Down Expand Up @@ -101,7 +102,11 @@
tag.y = startY + dy;

if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 ||
tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue;
tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) {
if (!overflow) {
continue;
}
}
// TODO only check for collisions within current bounds.
if (!bounds || !cloudCollide(tag, board, size[0])) {
if (!bounds || collideRects(tag, bounds)) {
Expand Down Expand Up @@ -189,6 +194,12 @@
return cloud;
};

cloud.overflow = function(x) {
if (!arguments.length) return overflow;
overflow = d3.functor(x);
return cloud;
};

return d3.rebind(cloud, event, "on");
}

Expand Down
44 changes: 44 additions & 0 deletions examples/simple_with_overflow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="../lib/d3/d3.js"></script>
<script src="../d3.layout.cloud.js"></script>
<script>
//OneBigWord will (usually) not show up, because it doesn't fit in
var fill = d3.scale.category20();

var words = [
"Hello", "world", "normally", "you", "want", "more", "words",
"than", "this", "OneBigWord"].map(function(d) {
return {text: d, size: 10 + Math.random() * 90};
});

d3.layout.cloud().size([300, 300])
.words(words)
.padding(5)
.overflow(true)
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();

function draw(words) {
d3.select("body").append("svg")
.attr("width", 300)
.attr("height", 300)
.append("g")
.attr("transform", "translate(150,150)")
.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; });
}
</script>