public class Tag
{
public string Text { get ; set ; }
public string Href { get ; set ; }
}
* This source code was highlighted with Source Code Highlighter .
[ToolboxData( "<{0}:TagCloudControl runat=server></{0}:TagCloudControl>" )]
public class TagCloudControl : WebControl
{
public const int MULTIPLIER = 2;
public const int MIN_SIZE = 5;
public event TagListDelegate TagsCollected;
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Align, "Center" );
writer.AddAttribute(HtmlTextWriterAttribute.Width, Width.ToString());
writer.AddAttribute(HtmlTextWriterAttribute.Height, Height.ToString());
writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
if (TagsCollected != null )
{
foreach ( var tag in TagCloudCache.GetTags(TagsCollected))
{
writer.WriteEncodedText( " " );
writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, string .Format( "{0}px" , (tag.Key+MIN_SIZE)*MULTIPLIER));
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.AddAttribute(HtmlTextWriterAttribute.Href, tag.Value.Href);
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.WriteEncodedText(tag.Value.Text);
writer.RenderEndTag();
}
}
writer.RenderEndTag();
}
}
* This source code was highlighted with Source Code Highlighter .
public delegate IEnumerable <Pair< int , Tag>> TagListDelegate();
* This source code was highlighted with Source Code Highlighter .
public class TagCalculationInfo
{
public TimeSpan TimeOut { get ; set ; }
public DateTime LastFiring { get ; set ; }
public IEnumerable <Pair< int , Tag>> CalculatedTags { get ; set ; }
public bool IsExpired
{
get
{
return DateTime .Now - LastFiring > TimeOut;
}
}
}
* This source code was highlighted with Source Code Highlighter .
public static class TagCloudCache
{
private const int TAG_GROUPS = 10;
private static readonly TimeSpan DEFAULT_TIMEOUT = new TimeSpan (0, 1, 0);
private static readonly Dictionary< string , TagCalculationInfo> m_Cache = new Dictionary< string , TagCalculationInfo>();
public static IEnumerable <Pair< int , Tag>> GetTags(TagListDelegate target)
{
lock (m_Cache)
{
string key = target.GetKey();
if (!m_Cache.ContainsKey(key))
{
m_Cache.Add(key, new TagCalculationInfo { TimeOut = DEFAULT_TIMEOUT });
}
var tagInfo = m_Cache[key];
if (tagInfo.IsExpired)
{
tagInfo.CalculatedTags = RecalculateTags(target);
tagInfo.LastFiring = DateTime .Now;
}
return tagInfo.CalculatedTags;
}
}
}
* This source code was highlighted with Source Code Highlighter .
public static string GetKey( this TagListDelegate del)
{
return string .Format( "{0}{1}{2}" , ((MulticastDelegate)del.Target).Method.DeclaringType, ((MulticastDelegate)del.Target).Method.Name, ((MulticastDelegate)del.Target).Method.MethodHandle.Value);
}
* This source code was highlighted with Source Code Highlighter .
private static IEnumerable <Pair< int , Tag>> RecalculateTags(TagListDelegate target)
{
var tags = new List <Pair< int , Tag>>(target());
var max = tags.Max().Key;
var min = tags.Min().Key;
var clusters = new int [TAG_GROUPS];
var step = (max - min)/(TAG_GROUPS - 1);
for ( int i = 0; i < TAG_GROUPS; i++)
{
clusters[i] = min + i*step;
}
foreach ( var tag in tags)
{
tag.Key = FindClosestPosition(clusters, tag.Key);
}
tags.Sort();
for ( int i = 0; i < tags.Count; i += 2)
{
yield return tags[i];
}
for ( int i = tags.Count % 2 == 0 ? tags.Count - 1 : tags.Count - 2; i >= 0; i -= 2)
{
yield return tags[i];
}
}
* This source code was highlighted with Source Code Highlighter .
public static int FindClosestPosition( int [] arr, int key)
{
int h = arr.Length - 1, l = 0;
while (h - l > 1)
{
int m = (h + l)/2;
if (arr[m] > key)
{
h = m;
}
else
{
l = m;
}
}
if ( Math .Abs(arr[h] - key) < Math .Abs(arr[l] - key))
{
return h;
}
return l;
}
* This source code was highlighted with Source Code Highlighter .
public TagListDelegate TestTags
{
get
{
return TagCloudCache.GetTestTags;
}
}
* This source code was highlighted with Source Code Highlighter .
<%@ Register Assembly="TagCloud" Namespace="TagCloud.TagCloud" TagPrefix="cc" %>
* This source code was highlighted with Source Code Highlighter .
< cc:TagCloudControl runat ="server" Name ="TagCloudControl" OnTagsCollected ="TestTags" />
* This source code was highlighted with Source Code Highlighter .
< span style ="font-size:10px;" >< a href ="#" > PHP </ a > < span style ="font-size:10px;" >< a href ="#" > Delphi </ a > < span style ="font-size:10px;" >< a href ="#" > Internet </ a > < span style ="font-size:10px;" >< a href ="#" > Nemerle </ a > < span style ="font-size:10px;" >< a href ="#" > Outsourcing </ a > < span style ="font-size:10px;" >< a href ="#" > VB.Net </ a > < span style ="font-size:10px;" >< a href ="#" > JavaScript </ a > < span style ="font-size:12px;" >< a href ="#" > C++ </ a > < span style ="font-size:12px;" >< a href ="#" > Apple </ a > < span style ="font-size:12px;" >< a href ="#" > Intel </ a > < span style ="font-size:14px;" >< a href ="#" > CLR </ a > < span style ="font-size:14px;" >< a href ="#" > Java </ a > < span style ="font-size:16px;" >< a href ="#" > WinForms </ a > < span style ="font-size:16px;" >< a href ="#" > Web </ a > < span style ="font-size:18px;" >< a href ="#" > WPF </ a > < span style ="font-size:22px;" >< a href ="#" > AJAX </ a > < span style ="font-size:28px;" >< a href ="#" > .Net </ a > < span style ="font-size:24px;" >< a href ="#" > ASP.Net </ a > < span style ="font-size:20px;" >< a href ="#" > C# </ a > < span style ="font-size:18px;" >< a href ="#" > Google </ a > < span style ="font-size:16px;" >< a href ="#" > MVC </ a > < span style ="font-size:14px;" >< a href ="#" > Microsoft </ a > < span style ="font-size:14px;" >< a href ="#" > SQL </ a > < span style ="font-size:12px;" >< a href ="#" > SEO </ a > < span style ="font-size:12px;" >< a href ="#" > jQuery </ a > < span style ="font-size:12px;" >< a href ="#" > Habrahabr </ a > < span style ="font-size:12px;" >< a href ="#" > Flash </ a > < span style ="font-size:10px;" >< a href ="#" > Sun </ a > < span style ="font-size:10px;" >< a href ="#" > LISP </ a > < span style ="font-size:10px;" >< a href ="#" > Facebook </ a > < span style ="font-size:10px;" >< a href ="#" > Perl </ a > < span style ="font-size:10px;" >< a href ="#" > RSDN </ a > < span style ="font-size:10px;" >< a href ="#" > Yandex </ a ></ span >
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/47448/
All Articles