Sunday, November 9, 2008

Create Tag Cloud C# 2.0 - The simplest and fastest way

Output:

palad1 palad2 palad3 palad4

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;

namespace WebApplication1
{

public class ItemFeq
{

private string _PhotoName;

public string PhotoName
{
get { return _PhotoName; }
set { _PhotoName = value; }
}
private int _Count;

public int Count
{
get { return _Count; }
set { _Count = value; }
}

private int _Size;

public int Size
{
get { return _Size; }
set { _Size = value; }
}

public ItemFeq(string photoname, int count, int size)
{
_PhotoName = photoname;
_Count = count;
_Size = size;
}
}

public class ItemTag
{
private int _ItemID;

public int PhotoID
{
get { return _ItemID; }
set { _ItemID = value; }
}
private string _Tag;

public string Tag
{
get { return _Tag; }
set { _Tag = value; }
}

public ItemTag(int itemid, string tag)
{
_Tag = tag;
_ItemID = itemid;
}
}

public class ItemTagComparer : IComparer<ItemTag>
{
public int Compare(ItemTag x, ItemTag y)
{
if (x.Tag == y.Tag) { return 0; } else { return x.Tag.CompareTo(y.Tag); }
}

}

public class ItemFeqByCountComparer : IComparer<ItemFeq>
{
public int Compare(ItemFeq x, ItemFeq y)
{
if (x.Count == y.Count)
{ return 0; }
else
{ return x.Count.CompareTo(y.Count); }
}

}

public partial class _Default : System.Web.UI.Page
{

public string CreateTagCloud(List<ItemTag> tags, int baseSize)
{

tags.Sort(new ItemTagComparer());

List<ItemFeq> photofeqlist = new List<ItemFeq>();

foreach (ItemTag ptag in tags)
{
if (photofeqlist.Exists(delegate(ItemFeq pf) { return pf.PhotoName == ptag.Tag; }))
{
photofeqlist.Find(delegate(ItemFeq pq) { return pq.PhotoName == ptag.Tag; }).Count += 1;
photofeqlist.Find(delegate(ItemFeq pq) { return pq.PhotoName == ptag.Tag; }).Size += 1;
}
else
{
photofeqlist.Add(new ItemFeq(ptag.Tag, 1, baseSize));
}
}

photofeqlist.Sort(new ItemFeqByCountComparer());
photofeqlist.Reverse();


StringBuilder sb = new StringBuilder();

int sum = 0;
foreach (ItemFeq pf in photofeqlist)
{
sum += pf.Count;
}


foreach (ItemFeq pf in photofeqlist)
{
sb.Append("<span style='font-size:" + (pf.Size / (sum / photofeqlist.Count)) * baseSize + "pt'>" + pf.PhotoName + " </span>");

}
return sb.ToString();
}

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{

List<ItemTag> phototags = new List<ItemTag>();
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(123, "palad1"));
phototags.Add(new ItemTag(124, "palad1"));
phototags.Add(new ItemTag(125, "palad1"));
phototags.Add(new ItemTag(126, "palad2"));
phototags.Add(new ItemTag(122, "palad2"));
phototags.Add(new ItemTag(122, "palad2"));
phototags.Add(new ItemTag(121, "palad2"));
phototags.Add(new ItemTag(122, "palad2"));
phototags.Add(new ItemTag(121, "palad2"));
phototags.Add(new ItemTag(126, "palad2"));
phototags.Add(new ItemTag(121, "palad2"));
phototags.Add(new ItemTag(122, "palad2"));
phototags.Add(new ItemTag(121, "palad2"));
phototags.Add(new ItemTag(122, "palad3"));
phototags.Add(new ItemTag(123, "palad3"));
phototags.Add(new ItemTag(124, "palad3"));
phototags.Add(new ItemTag(125, "palad4"));
phototags.Add(new ItemTag(126, "palad4"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));

Literal1.Text = CreateTagCloud(phototags, 12);

}
}
}

1 comment:

Zeeshan Umar said...

Nice link for tag clouds, I also wrote similar article, please checkout this:-

How to create Tag Cloud, Word Cloud or Weighted List