Simple implementation of a tag cloud for xwiki

Thomas Drevon thomas.drevon at intermedia.uio.no
Thu May 31 00:27:13 CEST 2007


Hi!

Tag clouds (http://en.wikipedia.org/wiki/Tag_cloud) seem so in these 
days, so I made a simple implementation using the Main.Tags in xwiki. 
The styling isn't too sexy, but that should be easy to configure.

Here is the code for use in a panel or similar place:

#panelheader('Tag cloud')

#set($query = "select elements(prop.list) from BaseObject as obj, 
DBStringListProperty as prop where obj.className='XWiki.TagClass' and 
obj.id=prop.id.id and prop.id.name='tags'")

#set( $allTags = $xwiki.sort($xwiki.search($query)))

#set( $tagsWithCount = $xwiki.metawiki.getTagsWithCount($allTags))

#set($query = "select distinct elements(prop.list) from BaseObject as 
obj, DBStringListProperty as prop where obj.className='XWiki.TagClass' 
and obj.id=prop.id.id and prop.id.name='tags'")

#set( $tagsDistinct = $xwiki.sort($xwiki.search($query)))

#foreach($tag in $tagsDistinct)
<span style="#getStyle($tagsWithCount.get($tag))"><a 
href="$xwiki.getURL("Main.Tags", "view", "tag=$tag")")>$tag</a></span>
#end

#panelfooter()


#macro( getStyle $count )
   #if ($count < 2) "font-size: 0.80em;"
   #elseif ($count < 3) font-size: 1.00em; }
   #elseif ($count < 4) font-size: 1.20em; }
   #elseif ($count < 5) font-size: 1.40em; }
   #elseif ($count < 6) font-size: 1.60em; }
   #elseif ($count < 7) font-size: 1.80em; }
   #elseif ($count < 8) font-size: 2.00em; }
   #else font-size: 2.50em; }
   #end
#end


And here's the java code invoked:

public static HashMap<String, Integer> 
getTagsWithCount(ArrayList<String> allTags) {
   String tag = null;
   String previousTag = null;
   HashMap<String, Integer> tagsWithCount = new HashMap<String, Integer>();
   int count = 1;		
   for (int i = 0; i < allTags.size(); i++) {
     previousTag = tag;
     tag = allTags.get(i);
     if (tag.equals(previousTag)) {
       count++;
     } else {
       count = 1;
       previousTag = null;
     }
     tagsWithCount.put(tag, new Integer(count));			
   }
   return tagsWithCount;
}


Is this anything you would want to include in xwiki, or is it too 
trivial to bother with? See the attached file for a sample screenshot.



best regards :-)
Thomas Drevon
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tag_cloud.png
Type: image/png
Size: 18105 bytes
Desc: not available
Url : http://lists.xwiki.org/pipermail/devs/attachments/20070531/575b6f2e/attachment.png 


More information about the devs mailing list