Click here to Skip to main content
15,867,594 members
Articles / Web Development / ASP.NET

Cloud Control for ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.84/5 (38 votes)
4 Jul 2006CPOL4 min read 278.2K   1.8K   184   75
This cloud control displays a list of hyperlinks in varying styles depending on a weight. This is similar to tag clouds in del.icio.us or Flickr.

Introduction

Web sites such as del.icio.us, Technorati, and Flickr, which allow tagging of their content, depict the popularity of tags using a tag cloud. The popular items have larger font sizes. This concept can be applied to any list of items where each item has an associated weight. For example, a list of products can be displayed in a cloud weighed by the cost of a product. Using the ASP.NET server control presented in this article, you can display your own domain specific items as a cloud. The image below shows a sample cloud for articles on Ajaxian.

sample image

Using the Control

This control works only with ASP.NET 2.0. First, you need to add a reference to the control assembly or the control project to your website. In the page you need to use the control, add the following declaration:

ASP.NET
<%@ Register Namespace="VRK.Controls" TagPrefix="vrk" Assembly="VRK.Controls" %>

This will allow you to use the control in the page using the following declaration:

ASP.NET
<vrk:Cloud ID="c1" runat="server" />

Now, you need to add items to the control. Each item will be displayed as a hyperlink on the page. You can supply the following properties for the items you want to add:

  • Text - The text of the hyperlink.
  • Href - The URL where the user will be navigated to when he clicks the hyperlink. If this property is left blank, the control causes a postback and raises the ItemClick event.
  • Title - The tooltip text of the HTML anchor.
  • Weight - The weight determines how the item will be displayed.

You can add the items declaratively, as shown:

XML
<vrk:Cloud ID="c1" runat="server">
<Items>
   <vrk:CloudItem Text="Item1" 
      Href="Default.aspx?tag=Item1" 
      Title="Some title" Weight="4" />
   <vrk:CloudItem Text="Item2" 
      Href="Default.aspx?tag=Item2" 
      Title="Some title" Weight="4" />
</Items>
</vrk:Cloud>

You can also add the items programmatically:

C#
c1.Items.Add(new CloudItem("Item1", 4, 
                           "Default.aspx?tag=Item1", 
                           "Some title"
                           ));

You can also use data binding to add items. First, you need to add a data source to your page. The code example below shows an ObjectDataSource, but you can use any ASP.NET DataSourceControl such as SqlDataSource or AccessDataSource.

ASP.NET
<asp:ObjectDataSource 
   ID="ItemsSource" 
   runat="server" 
   SelectMethod="GetItems" 
   TypeName="CloudTest.ItemsSource" 
  />

You need to indicate to the cloud control that it needs to use the ItemsSource data source control by specifying its DataSourceID.

ASP.NET
<vrk:Cloud ID="c1" runat="server" DataSourceID="ItemsSource" .... />

Once the data source is specified, you will need to indicate how the items should be populated from the data source. The following control properties can be used to supply the information:

  • DataTextField - the name of the data field that is bound to the Text property of an item.
  • DataTextFormatString - the format string for the Text property. {0} in the string is replaced with the value of the field from the data source.
  • DataHrefField - the data field which is bound to the Href property of an item.
  • DataHrefFormatString - the format string to format the Href property value.
  • DataTitleField - the data field which is bound to the Title property of an item.
  • DataTitleFormatString - the format string for the title (tooltip) of an item.
  • DataWeightField - the field in the Data Source from where the weight of an item is to be obtained.

The control then normalizes the weight of all the items so that they fit in the range 1 to 7. You can control the display of the normalized items using the optional ItemCssClassPrefix property. If you use the ItemCssClassPrefix property, you need to add seven different CSS classes to your HTML page. For example, if you specify the property value to be "Item", you need to specify the CSS classes Item1, Item2... Item7. If you don't specify the ItemCssClassPrefix, the font size CSS attribute is set depending on the weight, as follows:

Normalized WeightFont-size
1xx-small
2x-small
3small
4medium
5large
6x-large
7xx-large

Now, let's examine how the control works.

How the Control Works?

The main logic is to convert the distribution of weights into a integral range between 1 and 7. After struggling with statistics for some time, I figured out the following algorithm for normalizing the weights:

  1. Compute the mean and the standard deviation (σ) of the weights. This is done using the functions from the Statistics class similar to the Math class.
  2. C#
    private IEnumerable<double> ItemWeights
    {
      get
      {
        foreach (CloudItem item in this.Items)
        {
          yield return item.Weight;
        }
      }
    }
    
    ...
    
    double mean; 
    double stdDev = Statistics.StdDev(ItemWeights, out mean);

    The StdDev function takes an IEnumerable<double> parameter which is supplied by the ItemWeights method.

  3. The weight factor is calculated according to the following formula:
  4. factor = (weight - mean)/(stddev)
  5. The normalized weights are obtained based on the following table:
  6. Normalized WeightCondition
    1factor <= -2*stddev
    2-2*stddev < factor <= -1*stddev
    3-1*stddev < factor <= -0.5*stddev
    4-0.5*stddev < factor < 0.5*stddev
    50.5*stddev <= factor < 1*stddev
    61*stddev < = factor < 2*stddev
    7factor >= 2 * stddev

Once the normalized weights are obtained, it is easy to set the font sizes and the classes.

C#
foreach (CloudItem item in Items)
{
  HtmlAnchor a = new HtmlAnchor();
  a.HRef = String.IsNullOrEmpty(item.Href) ?
  this.Page.ClientScript.GetPostBackClientHyperlink(this, index.ToString()) :
                   item.Href;
  a.InnerText = item.Text;
  a.Title = item.Title;
  int normalWeight = NormalizeWeight(item.Weight, mean, stdDev);
  if (hasCssClassPrefix)
  {
    a.Attributes["class"] = 
      this.ItemCssClassPrefix + normalWeight.ToString();
  }
  else
  {
    a.Style.Add(HtmlTextWriterStyle.FontSize, _fontSizes[normalWeight - 1]);
  }
  this.Controls.Add(a);
  this.Controls.Add(new LiteralControl(" "));
  index++;
}

The method seems to work reasonably well in most of the situations. Any suggestions to improve it further are welcome.

History

  • July 4, 2006 - First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: onMouseOver event? Pin
Rama Krishna Vavilala17-Aug-06 6:09
Rama Krishna Vavilala17-Aug-06 6:09 
QuestionOnItemClick Pin
champ19797-Aug-06 12:35
champ19797-Aug-06 12:35 
AnswerRe: OnItemClick Pin
Rama Krishna Vavilala11-Aug-06 15:42
Rama Krishna Vavilala11-Aug-06 15:42 
GeneralRe: OnItemClick Pin
tutejaamit520-May-07 19:48
tutejaamit520-May-07 19:48 
QuestionLine wrap not working Pin
neutronron30-Jul-06 8:44
neutronron30-Jul-06 8:44 
AnswerRe: Line wrap not working [modified] Pin
pado2-Aug-06 4:31
pado2-Aug-06 4:31 
GeneralRe: Line wrap not working Pin
jlippiner11-Aug-06 12:22
jlippiner11-Aug-06 12:22 
GeneralRe: Line wrap not working Pin
Rama Krishna Vavilala11-Aug-06 14:32
Rama Krishna Vavilala11-Aug-06 14:32 
Look at the sample, it shows you how to add styles. But the easiest way is to provide an ID to the control, sya Cloud1, and then use the following CSS:

#Cloud1
{
background-color: orange;
foreground-color: white;
}



Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

GeneralRe: Line wrap not working Pin
azam1237-Sep-06 3:26
azam1237-Sep-06 3:26 
GeneralRe: Line wrap not working Pin
rNeo11-Sep-06 16:56
rNeo11-Sep-06 16:56 
GeneralRe: Line wrap not working Pin
azam12311-Sep-06 20:25
azam12311-Sep-06 20:25 
GeneralRe: Line wrap not working Pin
rNeo12-Sep-06 11:01
rNeo12-Sep-06 11:01 
GeneralRe: Line wrap not working Pin
Remy Blaettler26-Jan-07 5:51
Remy Blaettler26-Jan-07 5:51 
GeneralRe: Line wrap not working [modified] Pin
jimibt23-Aug-07 22:42
jimibt23-Aug-07 22:42 
AnswerRe: Line wrap not working Pin
conorstapleton7-Dec-07 12:29
conorstapleton7-Dec-07 12:29 
AnswerRe: Line wrap not working Pin
Jason S. Harmon27-Jul-07 23:18
Jason S. Harmon27-Jul-07 23:18 
GeneralGreat stuff (but I think there is a small issue) Pin
sbroenne13-Jul-06 2:17
sbroenne13-Jul-06 2:17 
GeneralRe: Great stuff (but I think there is a small issue) Pin
Rama Krishna Vavilala13-Jul-06 4:54
Rama Krishna Vavilala13-Jul-06 4:54 
GeneralRe: Great stuff (but I think there is a small issue) Pin
rNeo7-Sep-06 12:27
rNeo7-Sep-06 12:27 
QuestionTag in a table ? Pin
Soetam Rizky5-Jul-06 16:20
Soetam Rizky5-Jul-06 16:20 
AnswerRe: Tag in a table ? Pin
Rama Krishna Vavilala5-Jul-06 16:22
Rama Krishna Vavilala5-Jul-06 16:22 
GeneralRe: Tag in a table ? Pin
Soetam Rizky5-Jul-06 17:10
Soetam Rizky5-Jul-06 17:10 
GeneralSimilar to the one on weblogs.asp.net! Pin
Nish Nishant4-Jul-06 10:03
sitebuilderNish Nishant4-Jul-06 10:03 
GeneralRe: Similar to the one on weblogs.asp.net! Pin
Rama Krishna Vavilala4-Jul-06 10:12
Rama Krishna Vavilala4-Jul-06 10:12 
GeneralRe: Similar to the one on weblogs.asp.net! Pin
Nish Nishant4-Jul-06 10:31
sitebuilderNish Nishant4-Jul-06 10:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.