-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagCloud.js
69 lines (59 loc) · 2.17 KB
/
tagCloud.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
// Generates a tag cloud
// (c) 2009, Ryan Corradini
//
// usage:
// var tag_list = {tag1:5,tag2:12,tag3:7};
// parentDiv.appendChild( makeCloud(tag_list, "http://www.buyog.com/labels/", 5, 0.75, 4.5, ' ', false) );
// Javascript implementation of Fisher-Yates algorithm;
// Found at http://sedition.com/perl/javascript-fy.html
function shuffleArray( myArray ) {
var i = myArray.length;
if ( i == 0 ) return false;
while ( --i ) {
var j = Math.floor( Math.random() * ( i + 1 ) );
var tempi = myArray[i];
var tempj = myArray[j];
myArray[i] = tempj;
myArray[j] = tempi;
}
}
function makeCloud(tags, baseUrl, minCount, minSize, max, delim, shuffle) {
if (minCount == 'undefined') { minCount = 0 }
if (minSize == 'undefined') { minSize = 1 }
if (max == 'undefined') { max = 4.5 }; //(in 'em' units)
if (delim == 'undefined') { delim = ' ' }
if (shuffle == 'undefined') { shuffle = false; }
var maxSeen = 0; var fontSize = max;
var cloud = document.createElement("div");
var link = null;
var taglinks = [];
cloud.className = "tag-cloud";
for (tag in tags) {
if (tags[tag] > minCount) {
if (tags[tag] > maxSeen) { maxSeen = tags[tag]; }
fontSize = max * (tags[tag] - minCount);
link = document.createElement("a");
link.rel = "tag";
link.href = baseUrl+tag+".html";
link.title = tag + " (" + tags[tag] + " instances)";
link.innerHTML = tag;
link.fontSize = fontSize;
taglinks.push(link);
}
}
// optionally shuffle the tag order
if (shuffle) {
shuffleArray(taglinks);
} else {
taglinks = taglinks.sort();
}
// now append the tags to the new parent DIV
for (var i in taglinks) {
var ems = ((taglinks[i].fontSize-minSize) / (maxSeen - minCount)) + minSize;
taglinks[i].style.fontSize = ems+"em";
if (ems < 1) { taglinks[i].style.lineHeight = ems+"em"; }
cloud.appendChild(taglinks[i]);
if (delim) { cloud.appendChild(document.createTextNode(delim)) }
}
return cloud;
}