bosko2
07-24-2012, 12:14 AM
Hi all,
First time poster and new forum member. I have this piece of javascript written by someone that has left the company, it's for a tag cloud, however I want to change how it is sorted/displayed. Currently it is returning the tags in alphabetical order. I want them to come back in random order. Below is the code:
(function($) {
$.fn.tagCloud = function(options) {
var defaults = { "fontMax": 200, "fontMin": 100, "fontUnit": "%",
tagClass: "tagCloud", tagUrlFormatString: "{0}", maxTagLimit: 100
},
o = $.extend({}, defaults, options);
var dataBlock = $(this).hide(),
regx = /[\w\s\']+/g,
dataTags = dataBlock.text(),
tags = [],
keywords,
maxCount = 0,
minCount = 1;
while ((keywords = regx.exec(dataTags)) != null) {
var key = $.trim(keywords[0].toLowerCase());
if (key) {
var tag = key;
key = "tag" + key;
if (tags[key] == null) {
tags[key] = tags.length;
tags.push(new tagElement(tag));
}
else
tags[tags[key]].Count++;
maxCount = Math.max(tags[tags[key]].Count, maxCount);
minCount = Math.min(tags[tags[key]].Count, minCount);
}
}
if (tags.length > o.maxTagLimit) {
tags.sort(sortByCount);
tags.splice(o.maxTagLimit, tags.length - o.maxTagLimit);
minCount = tags[tags.length - 1].Count;
}
var tagBlock = dataBlock.after("<div id='tagBlock' class='" + o.tagClass + "'></div>").next("div#tagBlock"),
scaleFont = (o.fontMax - o.fontMin) / Math.max(maxCount - minCount, 1);
tags.sort(sortByText);
function tagElement(text) {
this.Text = text;
this.Count = 1;
}
function sortByCount(a, b) {
return b.Count - a.Count;
}
function sortByText(a, b) {
return ((a.Text < b.Text) ? -1 : ((a.Text > b.Text) ? 1 : 0));
}
for (var i = 0; i < tags.length; i++) {
var tag = tags[i],
fontsize = (tag.Count - minCount) * scaleFont + o.fontMin;
tagBlock.append("<a href='" + o.tagUrlFormatString.replace("{0}", tag.Text) + "' title='" + tag.Count + " item(s)'>" + tag.Text + "</a>").children(":last").css("font-size", fontsize + o.fontUnit);
}
};
})(jQuery);
I know that there is a sortby.math function but I'm not too sure where to place it. Any ideas?
Thanks ahead, bosko2.
First time poster and new forum member. I have this piece of javascript written by someone that has left the company, it's for a tag cloud, however I want to change how it is sorted/displayed. Currently it is returning the tags in alphabetical order. I want them to come back in random order. Below is the code:
(function($) {
$.fn.tagCloud = function(options) {
var defaults = { "fontMax": 200, "fontMin": 100, "fontUnit": "%",
tagClass: "tagCloud", tagUrlFormatString: "{0}", maxTagLimit: 100
},
o = $.extend({}, defaults, options);
var dataBlock = $(this).hide(),
regx = /[\w\s\']+/g,
dataTags = dataBlock.text(),
tags = [],
keywords,
maxCount = 0,
minCount = 1;
while ((keywords = regx.exec(dataTags)) != null) {
var key = $.trim(keywords[0].toLowerCase());
if (key) {
var tag = key;
key = "tag" + key;
if (tags[key] == null) {
tags[key] = tags.length;
tags.push(new tagElement(tag));
}
else
tags[tags[key]].Count++;
maxCount = Math.max(tags[tags[key]].Count, maxCount);
minCount = Math.min(tags[tags[key]].Count, minCount);
}
}
if (tags.length > o.maxTagLimit) {
tags.sort(sortByCount);
tags.splice(o.maxTagLimit, tags.length - o.maxTagLimit);
minCount = tags[tags.length - 1].Count;
}
var tagBlock = dataBlock.after("<div id='tagBlock' class='" + o.tagClass + "'></div>").next("div#tagBlock"),
scaleFont = (o.fontMax - o.fontMin) / Math.max(maxCount - minCount, 1);
tags.sort(sortByText);
function tagElement(text) {
this.Text = text;
this.Count = 1;
}
function sortByCount(a, b) {
return b.Count - a.Count;
}
function sortByText(a, b) {
return ((a.Text < b.Text) ? -1 : ((a.Text > b.Text) ? 1 : 0));
}
for (var i = 0; i < tags.length; i++) {
var tag = tags[i],
fontsize = (tag.Count - minCount) * scaleFont + o.fontMin;
tagBlock.append("<a href='" + o.tagUrlFormatString.replace("{0}", tag.Text) + "' title='" + tag.Count + " item(s)'>" + tag.Text + "</a>").children(":last").css("font-size", fontsize + o.fontUnit);
}
};
})(jQuery);
I know that there is a sortby.math function but I'm not too sure where to place it. Any ideas?
Thanks ahead, bosko2.