forked from jasondavies/d3-cloud
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathanimated.html
More file actions
106 lines (90 loc) · 3.37 KB
/
animated.html
File metadata and controls
106 lines (90 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<h2>Animation of famous opening lines:</h2>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
<script src="../build/d3.layout.cloud.js"></script>
<script>
function wordCloud(selector) {
var fill = d3.scale.category20();
var svg = d3.select(selector)
.append("svg")
.attr("width", 500)
.attr("height", 500)
.append("g")
.attr("transform", "translate(250,250)");
function draw(words) {
//Use the 'text' attribute (the word itself) to identity unique elements.
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();
}
return {
//Recompute the word cloud for a new set of words. This method will
// asycnhronously call draw when the layout has been computed.
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();
}
}
}
//http://en.wikiquote.org/wiki/Opening_lines
var words = [
"You don't know about me without you have read a book called The Adventures of Tom Sawyer but that ain't no matter.",
"The boy with fair hair lowered himself down the last few feet of rock and began to pick his way toward the lagoon.",
"When Mr. Bilbo Baggins of Bag End announced that he would shortly be celebrating his eleventy-first birthday with a party of special magnificence, there was much talk and excitement in Hobbiton.",
"It was inevitable: the scent of bitter almonds always reminded him of the fate of unrequited love."
]
//Remove punctation and repeated words. Compute a random
// size attribute for each word.
function getWords(i) {
function unique(value, index, self) {
return self.indexOf(value) === index;
}
return words[i]
.replace(/[!\.,:;\?]/g, '')
.split(' ')
.filter(unique)
.map(function(d) {
return {text: d, size: 10 + Math.random() * 60};
})
}
//Tell the word cloud to redraw with a new set of words.
//In reality the new words will probably come from a server request,
// user input or some other source.
function showNewWords(vis, i) {
i = i || 0;
vis.update(getWords(i ++ % words.length))
setTimeout(function() { showNewWords(vis, i + 1)}, 2000)
}
var myWordCloud = wordCloud('body');
showNewWords(myWordCloud);
</script>