public static MvcHtmlString TagCloud(this HtmlHelper html, IDictionary<string, int> tagsAndCounts, Func<string, string> urlExpression, object htmlAttributes)
{
if (tagsAndCounts == null || !tagsAndCounts.Any())
return MvcHtmlString.Empty;
var min = tagsAndCounts.Min(t => t.Value);
var max = tagsAndCounts.Max(t => t.Value);
var dist = (max - min) / 3;
var links = new StringBuilder();
foreach (var tag in tagsAndCounts)
{
string tagClass;
if (tag.Value == max)
{
tagClass = "largest";
}
else if (tag.Value > (min + (dist * 2)))
{
tagClass = "large";
}
else if (tag.Value > (min + dist))
{
tagClass = "medium";
}
else if (tag.Value == min)
{
tagClass = "smallest";
}
else
{
tagClass = "small";
}
links.AppendFormat("<a href=\"{0}\" title=\"{1}\" class=\"{2}\">{1}</a>{3}",
urlExpression(tag.Key), tag.Key, tagClass, Environment.NewLine);
}
var div = new TagBuilder("div");
div.MergeAttribute("class", "tag-cloud");
div.InnerHtml = links.ToString();
if (htmlAttributes != null)
{
div.MergeAttributes(new RouteValueDictionary(htmlAttributes), false);
}
return MvcHtmlString.Create(div.ToString());
}