Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

added font-weight spread to rendering #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions build/angular-word-cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ angular.module('vr.directives.wordCloud',[])
var type = angular.isUndefined(attr.type) ? 'list' : attr.type;
switch(type) {
case 'cloud':
elem.children().eq(0).attr('style',"font-size: "+$interpolate.startSymbol()+" fontSize(word.size) "+$interpolate.endSymbol()+";");
elem.children().eq(0).attr(
'style',
"font-size: " + $interpolate.startSymbol() + " fontSize(word.size) " + $interpolate.endSymbol() + ";" +
"font-weight: " + $interpolate.startSymbol() + " fontWeight(word) " + $interpolate.endSymbol() + ";"
);
break;
case 'list':
break;
Expand Down Expand Up @@ -68,7 +72,36 @@ angular.module('vr.directives.wordCloud',[])
words = [];
}

words = words.map(function(e) { return {word: e.word, size: e.size, rawSize: parseFloat(e.size) }; });
var min = words.reduce(function(a,b){
if(a.size < b.size) {
return a;
}
return b;
}).size;

var max = words.reduce(function(a,b){
if(a.size > b.size) {
return a;
}
return b;
}).size;

var uniqueCount = unique(
words.map(function(word){
return word.size.toString();
})
).length;

words = words.map(function(e) {
return {
word: e.word,
size: e.size,
rawSize: parseFloat(e.size),
min: min,
max: max,
uniqueCount: uniqueCount
};
});

scope.mywords = words;
};
Expand All @@ -80,6 +113,20 @@ angular.module('vr.directives.wordCloud',[])
return size;
};

scope.fontWeight = function(word) {

var weightRange = (word.max - word.min) / ( word.uniqueCount < 9 ? word.uniqueCount : 9);

for( var i = 0; i < 10; i++) {

if ( word.size < (weightRange * i) ){
return i * 100;
}
}

return 900;
};

scope.$watch('words',function() {
convertWords();
},true);
Expand All @@ -90,6 +137,19 @@ angular.module('vr.directives.wordCloud',[])
scope.reverse = newVal.substr(-4).toLowerCase() == 'desc';
});

function unique(array){
var u = {},
a = [];

for(var i = 0; i < array.length; ++i){
if(u.hasOwnProperty(array[i])) {
continue;
}
a.push(array[i]);
u[array[i]] = 1;
}
return a;
}
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions build/angular-word-cloud.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 67 additions & 2 deletions src/angular-word-cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ angular.module('vr.directives.wordCloud',[])
var type = angular.isUndefined(attr.type) ? 'list' : attr.type;
switch(type) {
case 'cloud':
elem.children().eq(0).attr('style',"font-size: "+$interpolate.startSymbol()+" fontSize(word.size) "+$interpolate.endSymbol()+";");
elem.children().eq(0).attr(
'style',
"font-size: " + $interpolate.startSymbol() + " fontSize(word.size) " + $interpolate.endSymbol() + ";" +
"font-weight: " + $interpolate.startSymbol() + " fontWeight(word) " + $interpolate.endSymbol() + ";"
);
break;
case 'list':
break;
Expand Down Expand Up @@ -68,7 +72,41 @@ angular.module('vr.directives.wordCloud',[])
words = [];
}

words = words.map(function(e) { return {word: e.word, size: e.size, rawSize: parseFloat(e.size) }; });
var min = 0,
max = 0;

if ( angular.isArray(words) && words.length > 0 ){
min = words.reduce(function(a,b){
if(a.size < b.size) {
return a;
}
return b;
}).size;

max = words.reduce(function(a,b){
if(a.size > b.size) {
return a;
}
return b;
}).size;
}

var uniqueCount = unique(
words.map(function(word){
return word.size.toString();
})
).length;

words = words.map(function(e) {
return {
word: e.word,
size: e.size,
rawSize: parseFloat(e.size),
min: min,
max: max,
uniqueCount: uniqueCount
};
});

scope.mywords = words;
};
Expand All @@ -80,6 +118,20 @@ angular.module('vr.directives.wordCloud',[])
return size;
};

scope.fontWeight = function(word) {

var weightRange = (word.max - word.min) / ( word.uniqueCount < 9 ? word.uniqueCount : 9);

for( var i = 0; i < 10; i++) {

if ( word.size < (weightRange * i) ){
return i * 100;
}
}

return 900;
};

scope.$watch('words',function() {
convertWords();
},true);
Expand All @@ -90,6 +142,19 @@ angular.module('vr.directives.wordCloud',[])
scope.reverse = newVal.substr(-4).toLowerCase() == 'desc';
});

function unique(array){
var u = {},
a = [];

for(var i = 0; i < array.length; ++i){
if(u.hasOwnProperty(array[i])) {
continue;
}
a.push(array[i]);
u[array[i]] = 1;
}
return a;
}
}
}
};
Expand Down