-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-cloud.js
201 lines (167 loc) · 5.4 KB
/
word-cloud.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var lowerLimit = 0;
var lensAperture = 200;
var dis = 6;
var fill = d3.scale.category20b();
var w = window.innerWidth*0.9,
h = window.innerHeight*0.8;
var max, fontSize;
var layout = d3.layout.cloud()
.timeInterval(Infinity)
.size([w, h])
.fontSize(function(d) {
if(d.value>lowerLimit){
return fontSize(+d.value);
}
else{
return 0;
}
})
.text(function(d) {
if(d.value>lowerLimit){
return d.key;
}
else{
return "";
}
})
.on("end", draw);
var svg = d3.select("#vis").append("svg")
.attr("width", w)
.attr("height", h);
var vis = svg.append("g").attr("transform", "translate(" + [w >> 1, h >> 1] + ")");
//var fisheye = d3.fisheye();
var text;
var fisheye;
var offsetWidth = w >> 1;
var offsetHeight = h >> 1;
console.log([offsetWidth, offsetHeight]);
var xtest = document.getElementById("filter1");
console.log(xtest);
var lensCheck = false;
var disableRot = false;
svg.on("mousemove", function() {
var mCoord = d3.mouse(this);
console.log(xtest);
fisheye = d3.fisheye.circular().radius(lensAperture).distortion(dis);
//console.log(d3.mouse(this));
//console.log(mCoord[1]);
//fisheye.focus(d3.mouse(this));
if(lensCheck)
{
mCoord[0] = mCoord[0] - offsetWidth;
mCoord[1] = mCoord[1] - offsetHeight;
fisheye.focus(mCoord);
text.each(function(d) { d.fisheye = fisheye(d); })
.attr("transform", function(d) {
console.log(d.size);
if(!disableRot)
{
if(d.fisheye.rot == -1)
return "translate(" + [d.fisheye.x , d.fisheye.y] + ")rotate(" + d.rotate + ")";
return "translate(" + [d.fisheye.x , d.fisheye.y] + ")rotate(" + d.rotate + ")";
}
else
{
if(d.fisheye.rot == -1)
return "translate(" + [d.fisheye.x , d.fisheye.y] + ")rotate(" + d.rotate + ")";
return "translate(" + [d.fisheye.x , d.fisheye.y] + ")rotate(" + d.fisheye.rot + ")";
}
})
.style("font-size", function(d) {
if(d.fisheye.size == -1)
return d.size + "px";
return d.fisheye.size + "px";
})
}
});
update();
window.onresize = function(event) {
update();
};
function draw(data, bounds) {
var w = window.innerWidth*0.9,
h = window.innerHeight*0.8;
svg.attr("width", w).attr("height", h);
scale = bounds ? Math.min(
w / Math.abs(bounds[1].x - w / 2),
w / Math.abs(bounds[0].x - w / 2),
h / Math.abs(bounds[1].y - h / 2),
h / Math.abs(bounds[0].y - h / 2)) / 2 : 1;
text = vis.selectAll("text")
.data(data, function(d) {
return d.text.toLowerCase();
});
text.transition()
.duration(1000)
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.style("font-size", function(d) {
return d.size + "px";
});
text.enter().append("text")
.attr("class", "words")
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.style("font-size", function(d) {
return d.size + "px";
})
.style("opacity", 0.1)
.transition()
.duration(300)
.style("opacity", 1);
text.style("font-family", function(d) {
return d.font;
})
.style("fill", function(d) {
return fill(d.text.toLowerCase());
})
.text(function(d) {
return d.text;
});
vis.transition().attr("transform", "translate(" + [w >> 1, h >> 1] + ")scale(" + scale + ")");
//vis.transition().attr("transform", "translate(" + [w >> 1, h >> 1] + ")scale(" + 1.0 + ")");
}
function update() {
layout.font('impact').spiral('archimedean');
fontSize = d3.scale['sqrt']().range([10,100]);
if (tags.length){
fontSize.domain([+11 || 1, +813]);
// fontSize.domain([+tags[tags.length - 1].value || 1, +tags[0].value]);
//fontSize.domain([10, +tags[0].value]);
}
layout.stop().words(tags).start();
}
function updateData() {
var elements = document.getElementsByClassName("words")
for(var i = elements.length - 1; i >= 0; i--) {
elements[i].remove();
}
update();
}
function lowerLimtSliderChanged() {
var x = document.getElementById("filter").value;
lowerLimit = x;
document.getElementById('lowerLimitOutput').innerHTML = x;
updateData();
}
function changeLensAperture() {
var x = document.getElementById("aperture").value;
lensAperture = x;
document.getElementById('lenAperture').innerHTML = x;
}
function changeLensDistortion() {
var x = document.getElementById("distortion").value;
dis = x;
document.getElementById('lenDistortion').innerHTML = x;
}
function activateLens(cb) {
lensCheck = cb.checked;
console.log("activateLens new value = " + cb.checked);
}
function rotateLens(cb) {
disableRot = cb.checked;
console.log("rotateLens new value = " + cb.checked);
}