Building tag clouds

A tag cloud or word cloud (or weighted list in visual design) is a visual depiction of user-generated tags, or simply the word content of a site, used typically to describe the content of web sites. Tags are usually single words and are typically listed alphabetically, and the importance of a tag is shown with font size or color. Therefore, finding a tag by alphabet and by popularity is possible. The tags are usually hyperlinks that lead to a collection of items that are associated with a tag.

A more complex example is the building a tag cloud around a piece of content by retrieving Keywords one level up and down.

ASP.NET
The following is an example of the ASP.NET implementation:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Tridion.ContentDelivery.Taxonomies;
using Tridion.ContentDelivery.DynamicContent.Query;
using Tridion.ContentDelivery.DynamicContent;

namespace testtax_net
{
    public partial class features : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CompositeFilter tagCloudFilter = new CompositeFilter();
            tagCloudFilter.DepthFiltering(1, DepthFilter.FilterUp);
            tagCloudFilter.DepthFiltering(1, DepthFilter.FilterDown);
 
            String[] componentURIS = new String[] { "tcm:12-40-16", "tcm:12-41-16", "tcm:12-42-16" };
                        

            TaxonomyRelationManager relationManager = new TaxonomyRelationManager();
            TaxonomyFactory taxFactory = new TaxonomyFactory();

            String[] contextKeywordURIs = { "tcm:12-70-1024", "tcm:12-68-1024", "tcm:12-80-1024" };
            Keyword[] contextKeywordArray = new Keyword[contextKeywordURIs.Length];


            for (int i = 0; i < contextKeywordURIs.Length; i++)
            {
                contextKeywordArray[i] = taxFactory.GetTaxonomyKeyword(contextKeywordURIs[i]);

            }
            Keyword[] cloudFacets = relationManager.GetTaxonomyKeywords("tcm:12-900-512", componentURIS, contextKeywordArray, tagCloudFilter, 16);
             
            for(int i = 0; i < cloudFacets.GetLength(0); i++)
            {
            	
	            int fontSize = 11 + (cloudFacets[i].ReferencedContentCount * 2);
                Response.Write("<a class = 'big' style='white-space: nowrap; font-size: " + fontSize + " px;' href= 'index.jsp?Keyword=" + cloudFacets[i].KeywordUri + "'> ");
                Response.Write(cloudFacets[i].KeywordName + "</a>");
               if (i%2==0 ) Response.Write("<br/>");
            }

        }
    }
}
Java
The following is an example of the Java implementation:
<%@ page language="java" contentType="text/html"%>
<%@page import="com.tridion.broker.querying.Query"%>
<%@page import="com.tridion.broker.querying.criteria.Criteria"%>
<%@page import="com.tridion.broker.querying.criteria.operators.AndCriteria"%>
<%@page import="com.tridion.broker.querying.criteria.content.PublicationCriteria"%>
<%@page import="com.tridion.broker.querying.criteria.taxonomy.TaxonomyKeywordCriteria"%>
<%@page import="com.tridion.dcp.ComponentPresentationFactory"%>
<%@page import="com.tridion.dcp.ComponentPresentation,
com.tridion.broker.componentpresentations.meta.*,
com.tridion.broker.components.meta.*,
com.tridion.broker.querying.filter.LimitFilter,
com.tridion.broker.querying.*,
com.tridion.taxonomies.*,
com.tridion.meta.*,
com.tridion.taxonomies.filters.*,
java.util.*,
java.util.Iterator,
com.tridion.dcp.filters.sorting.*"%>
<%@ taglib uri="/cd_tags.tld" prefix="tridion" %>

<% 
CompositeFilter tagCloudFilter = new CompositeFilter();
tagCloudFilter.depthFiltering(1, DepthFilter.FILTER_UP);
tagCloudFilter.depthFiltering(1, DepthFilter.FILTER_DOWN);

String[] componentURIS = new String[] { "tcm:12-40-16", "tcm:12-41-16", "tcm:12-42-16" };
            

TaxonomyRelationManager relationManager = new TaxonomyRelationManager();
TaxonomyFactory taxFactory = new TaxonomyFactory();

String[] contextKeywordURIs = { "tcm:12-70-1024", "tcm:12-68-1024", "tcm:12-80-1024" };
Keyword[] contextKeywordArray = new Keyword[contextKeywordURIs.length];

for (int i = 0; i < contextKeywordURIs.length; i++)
{
    contextKeywordArray[i] = taxFactory.getTaxonomyKeyword(contextKeywordURIs[i]);
}
Keyword[] cloudFacets = relationManager.getTaxonomyKeywords("tcm:12-900-512", componentURIS, contextKeywordArray, tagCloudFilter, 16);
 
for(int i = 0; i < cloudFacets.length; i++)
{
	
    int fontSize = 11 + (cloudFacets[i].getReferencedContentCount() * 2);
    System.out.println("<a class = 'big' style='white-space: nowrap; font-size: " + fontSize);
    System.out.println("px;' href= 'index.jsp?Keyword=" + cloudFacets[i].getKeywordURI()+ "'> ");
    System.out.println(cloudFacets[i].getKeywordName() + "</a>");
   if (i%2==0 )System.out.println("<br/>");
}

%>