Saturday, June 7, 2008

Tag Cloud in Asp.net

<script runat="server">

    public string st;
    protected void Page_Load(object sender, EventArgs e)
    {
        st = CategoryCloud();
    }
    private string CategoryCloud()
   {   
        String theme = "<a class='weight{weight}' href=#?tag={url}'>{cat}</a> ";
        StringBuilder sb = new StringBuilder();
        double max = 0;
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("server=.;database=northwind;uid=sa;pwd=sa");
        DataTable ds = new DataTable();
        System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter("SELECT a.CategoryID,a.CategoryName, COUNT"
         +"(b.ProductID) AS NumberOfProducts FROM Categories AS a INNER JOIN Products AS b"
         +" ON a.CategoryID = b.CategoryID GROUP BY a.CategoryID, a.CategoryName ORDER BY a.CategoryName", con);
         sda.Fill(ds);
        double.TryParse(ds.Compute("max(NumberOfProducts)", null).ToString(), out max);
        foreach (DataRow rv in ds.Rows)
        {
            double Percent = (double.Parse(rv["NumberOfProducts"].ToString()) / max) * 100;
            int weight = 0;
            if (Percent >= 99)
                //heaviest
                weight = 1;
            else if (Percent >= 70)
                weight = 2;
            else if(Percent >= 40)
                weight = 3;
            else if (Percent >= 20)
                weight = 4;
            else if (Percent >= 3)
                //weakest
                weight = 5;
            else
                weight = 0;
                        
            if (weight > 0)
               sb.Append(theme.Replace("{weight}", weight.ToString()).Replace("{cat}", rv["categoryname"].ToString()).Replace("{url}", HttpUtility.UrlEncode(rv["categoryname"].ToString())));
        }
         return sb.ToString();
    }

 

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style>
#tagCloud
{
 width:238px;
 border:solid 1px #ccc;
 padding:1px;
 margin-bottom:10px;
 text-align:inherit;
 line-height:0.5em; color:blue;
 height:40px;
}

#tagCloud A
{
 text-decoration:none;
 margin-left:5px;
 margin-right:5px;
 font-family:Trebuchet MS, Verdana, Arial;
 text-transform:lowercase;
}

#tagCloud A:hover
{
 color:#00cc00;
 text-decoration:underline;
 line-height:0.9em;
}

#tagCloud A.weight1
{
 color: #ff9900;
 font-size: 14pt;
 font-weight:bolder;
 text-decoration:underline;
}
#tagCloud A.weight2
{
 color: #4169e1;
 font-size:12pt;
 font-weight:bolder;
}
#tagCloud A.weight3
{
 color: #009eff;
 font-size: 11pt;
 font-weight:bolder;
 text-decoration:underline;
}
#tagCloud A.weight4
{
 color: #4188cf;
 font-size: 10pt;
 
}
#tagCloud A.weight5
{
 color: #83bcd8;
 font-size: 8pt;
 
}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="tagCloud" >
       <%=st %>
   </div>
    </form>
</body>
</html>

1 comment :

  1. I want count the weight of tag from the XML File

    ReplyDelete

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath