-
Notifications
You must be signed in to change notification settings - Fork 0
/
D3WordCloud.js
126 lines (110 loc) · 3.72 KB
/
D3WordCloud.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function wordCloud(selector) {
var fill = d3.scale.category20();
//Construct the word cloud's SVG element
var svg = d3.select(selector).append("svg")
.attr("width", 1000)
.attr("height", 500)
.append("g")
.attr("transform", "translate(250,250)");
//Draw the word cloud
function draw(words) {
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 {
update: function(words) {
d3.layout.cloud().size([900, 800])
.words(words)
.padding(5)
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();
}
}
}
var words = [
"FedEx Delivery Driver Catalina Island $25/hr",
"Find jobs with Flexible Hours!",
"Permits Supervisor",
"Visitor Service Center Agent",
"U.S. Secret Service Criminal Investigator (Special Agent)",
"Marine Interdiction Agent CBP LIVE Recruitment Webinar",
"Part-time Guest Services Representative",
"Border Patrol Agent GL-9",
"Limpia de casa",
"Utility Pole Inspection Foreman",
"Executive Assistant",
"Full-time Guest Services Representative",
"Airport Operations Assistant Manager",
"Operations Analyst",
"Manager",
"Front Desk / Guest Services Agent",
"Plant Equipment Operator",
"Fitness Center CSR",
"Housekeeper",
"Game Host for Unique Sightseeing Tour",
"Food Service Worker",
"Distribution Journeyman Lineman - Catalina Island",
"Housekeeping Supervisor",
"Communications Operator-Customer Service Rep",
"TMV Trucking Inc",
"Indeed Gigs",
"Catalina Island Conservancy",
"Catalina Island Company",
"United States Secret Service",
"U.S. Customs and Border Protection",
"Catalina Island Medical Center",
"Osmose Utility Services Inc",
"Boldly",
"MMW Hospitality Corp",
"Hotel Vista Del Mar",
"Southern California Edison",
"Fantastic Race",
"Inspire Malibu"
];
//Prepare one of the sample sentences by removing punctuation,
// creating an array of words and computing a random size attribute.
function getWords(i) {
return words[i]
.replace(/[!\.,:;\?]/g, '')
.split(' ')
.map(function(d) {
return {text: d, size: 10 + Math.random() * 60};
})
}
// 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)
}
//Create a new instance of the word cloud visualization.
var myWordCloud = wordCloud('body');
//Start cycling through the demo data
showNewWords(myWordCloud);