|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Download source code - 56.1 Kb
IntroductionWeb 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. Using the ControlThis control works only with the ASP.NET 2.0. First, you need to add a reference to the control assembly or the control project to your web site. In the page you need to use the control, add the following declaration. <%@ Register Namespace="VRK.Controls" TagPrefix="vrk" Assembly="VRK.Controls" %> This will allow you to use the control in the page using the following declaration. <vrk:Cloud ID="c1" runat="server" /> Now you need to add items to the control. Each item will be displayed as an hyperlink on the page. You can supply following properties for the items you want to add.
You can add the items declaratively as shown: <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: 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 a data source
to your page. The code example below shows an <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
<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. Following control properties can be used to supply the information:
The control than 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
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 that between 1 to 7. After struggling with statistics for some time I figured out the following algorithm for normalizing the weights.
Once the normalized weights are obtained it is easy to set the font sizes and the classes. 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
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||