about
framework & approach
knowledge network
news & events
technology council
why join?

Tuesday, September 25, 2007

Tag Clouds - didn't think the web 2.0 mullet could be so omplex

Yes, we have our own tag cloud. I'm not a big fan of those, but I guess some users like them.
The secret of tag clouds relies in normalizing your tag sizes. I found out that there's a bunch of different approaches to the problem, with various results. We started from the following control:

http://www.codeproject.com/useritems/cloud.asp

Works fine, but I didn't like the way the normalization was being done. It assumed 7 steps in its normalization process, and relied on 7 css styles to render each step. Too hard-coded for my taste.

Instead we used the following algorithm when normalizing values:

private static int NormalizeWeight(double minValue, double maxValue, double weight, double maxWeight)
{
double retVal;
double m;
if (minValue > maxValue)
{
m = (minValue - maxValue) / Math.Log(maxWeight);
retVal = minValue - Math.Floor(Math.Log(weight) * m);
}
else
{
m = (maxValue - minValue) / Math.Log(maxWeight);
retVal = Math.Floor(Math.Log(weight) * m + minValue);
}
return (int)Math.Abs(retVal);
}

This allows to normalize font size, color (by passing rgb values) - you name it. The result looks pretty good actually. Just make sure your max weight is greater than 1 or it's not going to work. So before calling the method, check your tags. If every single tag in the cloud has only one match, set the number to your the minimum value and skip the normalizing method.

0 comments: