-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (62 loc) · 1.98 KB
/
index.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
var css = require('css');
var classes = require('classes');
module.exports = tagcloud;
//json object with the tags , minimum and maximum font size
function tagcloud(tags, minPercent, maxPercent){
var sortedTags = sortObjByValue(tags);
var tagArray = convertToArray(tags);
var minPercent = minPercent || 80;
var maxPercent = maxPercent || 280;
var min = sortedTags[0].value.weight;
var max = sortedTags.slice(-1)[0].value.weight;
var steps = sortedTags.length - 1;
var originalStep = calculateStep(min,max,steps);
var percentStep = calculateStep(minPercent,maxPercent,steps);
var ul = document.createElement('ul');
classes(ul).add('tagcloud');
tagArray.forEach(function (el,i){
var li = document.createElement('li');
var link = document.createElement('a');
link.textContent = el.tag;
link.setAttribute('href', el.value.link);
var distance = distanceFromStart(min,el.value.weight);
var size = minPercent + mapCalculation(originalStep, percentStep,distance);
css(link, {
fontSize:size+"%"
})
li.appendChild(link);
ul.appendChild(li);
})
return ul;
}
//sort an object by a specific value
function sortObjByValue(obj){
var sortable = [];
for(var key in obj){
sortable.push({tag : key, value : obj[key]})
}
var sorted = sortable.sort(function(a,b) { return a.value.weight - b.value.weight });
return sorted;
}
function convertToArray(obj){
var array = [];
for(var key in obj){
array.push({tag : key, value : obj[key]})
}
return array;
}
//calculate the step needed to go to max from min in n steps
function calculateStep(min,max,steps){
var step = (max - min)/steps;
return step;
}
//calculate values distance from lower
function distanceFromStart(lower,value){
var distance = value - lower;
return distance;
}
//calculate a value based on the step values we already have calculated
function mapCalculation(step1,step2,distance){
var mapDistance = (step2 * distance)/step1;
return mapDistance;
}