For the Layout Partial we will be simply defining the relevant classes where we will want to render the Word Cloud. The class names we will be used as inline with what our version of jQCloud (2.0.3) recommends.
For this particular Blank Canvas there is no need to actually reference the Tiles partial as we won't be actually loading any individual pieces of UGC.
Following the definition of the Layout Partial, we can now start to define the styling of our Word Cloud. In here the most important items we have defined relates to the various sizes and colours of the elements in the Word Cloud (Elements: div.jqcloud span.jqcloud-word.w1 - div.jqcloud span.jqcloud-word.w10) and the overarching Styling for the Wall.
Finally comes the most important part, loading the jQCloud Javascript Framework. For this example, we have used jQCloud 2.0.3, however if you use earlier or later versions the behaviour maybe slightly different. You will need to download the jQuery JS file, host it and then update the below code to reference it in order for the below Javascript to work.
Now in our Javascript we are defining how frequently we would like our Widget to refresh, the size of our Widget and the delay for loading each element. For this same, we've set it to refresh twice after 1000ms and have set the Widget size to 850px wide by 700px tall.
var refreshTime = 1 * 1000;
refreshed = false;
Stackla.loadJS([
"jqcloud.js"
]).then(function () {
// Load tiles data from selected filter
var refreshCloud = function () {
Stackla.loadTilesByFilter(function (tiles) {
if (!refreshed) Stackla.render({tiles: tiles});
var results = [];
var hashtags = tiles.forEach(function(post){
var retrievedHashtags = getHashTags(post.caption);
if(retrievedHashtags.length){
results.concat(retrievedHashtags);
retrievedHashtags.forEach(function(tag){
results.push(tag);
});
}
}, {
limit:2
});
var words = processResults(results);
if (!refreshed) {
setTimeout(function () {
$('.wall.first .wall__tags, .wall__tags_clone').jQCloud(words, {
width: 850,
height: 700,
delay: 2,
encodeURI:true,
afterCloudRender: function () {}
});
}, 1000);
refreshed = true;
}
});
};
// get the cloud
refreshCloud();
// set an interval to refresh
setInterval(refreshCloud, refreshTime);
});
function getHashTags(inputText) {
var regex = /(?:^|\s)(?:#)([a-zA-Z\d]+)/gm;
var matches = [];
var match;
while ((match = regex.exec(inputText))) {
matches.push(match[1]);
}
return matches;
}
function findWithAttr(array, attr, value) {
for(var i = 0; i < array.length; i += 1) {
if(array[i][attr].toLowerCase() === value.toLowerCase()) {
return i;
}
}
return -1;
}
function processResults(results){
var words = [];
results.forEach(function(tag){
var wordIndex = findWithAttr(words, "text", tag);
if(wordIndex < 0){
// if there's no match
var word = {
text : ''+tag,
weight : 1
};
words.push(word);
}else{
words[wordIndex].weight +=1;
}
});
return words;
}
var that = this;
The Final Result will look something like below. It is important to note, that depending on the Filter you are using, how much content you have associated with it, and the variation of words will all have an impact on the end design.
jQCloud will ignore words which are only ever mentioned once, and if it cannot differentiate the occurrence of certain words, it may skip some of the size classes we defined in the CSS.
<scripttype="text/javascript">(function (d, id) { var t, el =d.scripts[d.scripts.length-1].previousElementSibling; if (el) el.dataset.initTimestamp = (newDate()).getTime();if (d.getElementById(id)) return; t =d.createElement('script'); t.src ='//assetscdn.stackla.com/media/js/widget/fluid-embed.js'; t.id = id; (d.getElementsByTagName('head')[0] ||d.getElementsByTagName('body')[0]).appendChild(t); }(document,'stackla-widget-js'));</script>