The information display is a very important process for anyone working with databases. Visual mechanisms, such as navigation maps or diagrams, can facilitate the understanding of the data thus transforming them into visual structures that combine graphical properties with the spatial organization of the data. This article shows how to implement an information visualization technique called tag cloud, which involves the concept of classification of content by tags to facilitate the classification, organization and data research. The article describes what the tag clouds are, how they are used and also how to implement a tag cloud from information stored in a SQL Server table. Finally, it presents an idea to generate a dynamic Web page containing a tag cloud.



CREATE TABLE #TB_LIMITS
(
ID_LIMIT INT,
MARK_LIMIT NUMERIC(10,3)
)
/* NOW WE GET THE DELTA AND CALCULATES THE FONT SIZE OF EACH TAG */
SELECT @MIN = MIN(QTD),
@DELTA = (MAX(QTD)-MIN(QTD)) / CONVERT(NUMERIC(10,3),@SIZES)
FROM #TB_TOTALS
/* THE LOOP CALCULATS THE FONT SIZE FOR EACH TAG*/
DECLARE @I INT
SET @I = 1
WHILE @I <= @SIZES
BEGIN
INSERT #TB_LIMITS VALUES(@I,@MIN + (@DELTA*@I) )
SET @I = @I + 1
END
/* NOW WE SET THE CORRECT VALUE USING THE SECOND PARAMETER */
SELECT TAG, QTD, @TAM_FONT_INICIAL + ( SELECT TOP 1 (ID_LIMIT -1)*2
FROM #TB_LIMITS B
WHERE B.MARK_LIMIT >= A.QTD ) AS SIZE_FONT
FROM #TB_TOTALS A
ORDER BY QTD DESC
END
Listing 2. Stored procedure that assigns fonts sized to the tags.
First, the stored procedure ST_SIZE_TAG returns all tags stored in the table TB_TAGS storing this result in the temporary table #TB_TOTALS. Then it gets the lower rate of tags and calculates the value of DELTA, which will be used to calculate the limit of each frequency. The stored procedure contain a loop to assign the limits of each font size, storing these data in the temporary table #TB_LIMITS. Despite using a loop inside the stored procedure, a technique that is not recommended by many developers due to performance issues, we highlight that the loop is not done on a cursor variable but upon variables of the stored procedure.
Finally, each tag receives its the font size through a SELECT statement that uses a sub query. The sub query returns only the first limit that is greater than or equal to the frequency of the tag calculating the value of the size using the initial size indicated by the parameter @TAM_FONT.
Table 1 show the first fifteen lines of the result after the execution of the stored procedure when the values 6 and 10 are assigned to parameters @SIZES and @TAM_FONT, respectively. The graph in Figure 4 shows the frequency distribution of tags in the sample data with the limits for each font size.
Table 1. The result of the stored procedure with the example data.
Figure 4. Bar graph with the amount of each tag and its font size.
To effectively build a tag cloud is necessary to use any programming language that allows dynamically assemble a web page, such as ASP.NET or PHP. This dynamic page should make a call to the stored procedure ST_SIZE_TAG and mount the tag cloud from the set of results returned. For each row in the result set returned by the stored procedure the dynamic page should assemble the following HTML statement:
…
<span font-size: [FONT SIZE COLUMN]px;"><a href=" ...">[TAG]</a></span> |
…
Where [FONT SIZE COLUMN] and [TAG] should be replaced dynamically by the values of columns SIZE_FONT and TAG, respectively, obtained from the result set of a stored procedure. The developer can also use the result of the QTD column to mount the tooltip for each tag (see Figure 1). This tooltip can indicate the number of elements of this tag thus facilitating the visualization of the number of occurrences. The developer must still fill the property href of the anchor <a> according to the page that will bring all the content associated with this tag. Other design features can be implemented, such as placing the selection of styles, colors or different fonts can be used in order to give a professional touch. The tag cloud mounted with the sample data can be visualized in Figure 5.
Figure 5. Tag cloud with the sample data of this article.
References
Del.icio.us Tag Cloud
http://del.icio.us/tag/
Flickr Tag Cloud
http://www.flickr.com/photos/tags/
Google News Cloud
http://fserb.com.br/newscloud/








See the prices for this post in Mr.Bool Credits System below: