Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* By given minFontSize and maxFontSize, generate the tags with different font-size, depending on the number of occurrences */
- function generateTagCloud() {
- 'use strict';
- var tags = ["cms", "javascript", "js", "ASP.NET MVC", ".net", ".net", "css", "wordpress", "xaml", "js", "http", "web", "asp.net",
- "asp.net MVC", "ASP.NET MVC", "wp", "javascript", "js", "cms", "html", "javascript", "http", "http", "cms"];
- var minFontSize = document.getElementById("min-font-size").value | 0;
- var maxFontSize = document.getElementById("max-font-size").value | 0;
- // use dictionary to count elements
- var dict = {};
- for (var i = 0; i < tags.length; i++) {
- if (tags[i] in dict) {
- dict[tags[i]]++;
- } else {
- dict[tags[i]] = 1;
- }
- }
- // use array of arrays to sort
- var sortable = [];
- for (var key in dict) {
- sortable.push([key, dict[key]]);
- }
- sortable.sort(function (a, b) { return a[1] - b[1]; });
- var diapason = maxFontSize - minFontSize;
- var rate = diapason / sortable[sortable.length - 1][1];
- var documenyFragment = document.createDocumentFragment();
- for (var i = 0; i < sortable.length; i++) {
- var span = document.createElement("span");
- span.innerHTML = sortable[i][0];
- span.style.fontSize = (minFontSize + rate * sortable[i][1]) + "px";
- span.style.display = "block";
- documenyFragment.appendChild(span);
- }
- document.getElementById("container").appendChild(documenyFragment);
- }
Advertisement
Add Comment
Please, Sign In to add comment