C#版 Tag云图控件

系统 1645 0
今天看到TerryLee的一篇文章 http://www.cnblogs.com/Terrylee/archive/2008/02/20/1075764.html ,其中有一条 ASP.NET Tag/Search Cloud Server Control (导读:在Web2.0时代,Tag成为了一个标志,通常一些网站都会采用Tag云图来显示。有“好事者”干脆开发了一个Tag云的ASP.NET服务器控件),里边的源文件是用vb写的,我就把代码改改,转换成C#的了,代码我直接贴出来吧,大家自己看吧!

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;

namespace Conovosoft.Web.UI.WebControls
{
[ToolboxData(
" <{0}:SearchCloudrunat=server></{0}:SearchCloud> " )]
public class SearchCloud:WebControl
{
"属性" #region "属性"

"Appearance" #region "Appearance"

[Bindable(
true ),Category( " Appearance " ),Localizable( true )]
public int MinFontSize
{
get
{
string s = ( string )ViewState[ " MinFontSize " ];
if (String.IsNullOrEmpty(s))
return 10 ;
else
{
int result = 10 ;
int .TryParse(s, out result);
return result;
}

}

set
{
ViewState[
" MinFontSize " ] = value;
}

}


[Bindable(
true ),Category( " Appearance " ),Localizable( true )]
public int MaxFontSize
{
get
{
string s = ( string )ViewState[ " MaxFontSize " ];
if (String.IsNullOrEmpty(s))
return 22 ;
else
{
int result = 22 ;
int .TryParse(s, out result);
return result;
}

}

set
{
ViewState[
" MaxFontSize " ] = value;
}

}


[Bindable(
true ),Category( " Appearance " ),Localizable( true )]
public string FontUint
{
get
{
string s = ( string )ViewState[ " FontUint " ];
if (String.IsNullOrEmpty(s))
return " pt " ;
else
return s;
}

set
{
switch (value)
{
case " pt " :
case " em " :
case " % " :
case " px " :
ViewState[
" FontUnit " ] = value; break ;
default :
ViewState[
" FontUnit " ] = " px " ; break ;
}

}

}


[Bindable(
true ),Category( " Appearance " ),Localizable( true )]
public string MaxColor
{
get
{
string s = ( string )ViewState[ " MaxColor " ];

if (String.IsNullOrEmpty(s))
return " #00f " ;
else
return s;
}

set
{
ViewState[
" MaxColor " ] = value;
}

}


[Bindable(
true ),Category( " Appearance " ),Localizable( true )]
public string MinColor
{
get
{
string s = ( string )ViewState[ " MinColor " ];

if (String.IsNullOrEmpty(s))
return " #000 " ;
else
return s;
}

set
{
ViewState[
" MinColor " ] = value;
}

}


#endregion


"Data" #region "Data"

[Bindable(
true ),Category( " Data " ),DefaultValue( "" )]
public DataSetDataSource
{
get
{
return (DataSet)ViewState[ " DataSource " ];
}

set
{
ViewState[
" DataSource " ] = value;
}

}


[Bindable(
true ),Category( " Data " ),DefaultValue( "" ),Localizable( true )]
public string DataIDField
{
get
{
return ( string )ViewState[ " DataIDField " ];
}

set
{
ViewState[
" DataIDField " ] = value;
}

}


[Bindable(
true ),Category( " Data " ),DefaultValue( "" ),Localizable( true )]
public string DataKeywordField
{
get
{
return ( string )ViewState[ " DataKeywordField " ];
}

set
{
ViewState[
" DataKeywordField " ] = value;
}

}


[Bindable(
true ),Category( " Data " ),DefaultValue( "" ),Localizable( true )]
public string DataURLField
{
get
{
return ( string )ViewState[ " DataURLField " ];
}

set
{
ViewState[
" DataURLField " ] = value;
}

}


[Bindable(
true ),Category( " Data " ),DefaultValue( "" ),Localizable( true )]
public string DataCountField
{
get
{
return ( string )ViewState[ " DataCountField " ];
}

set
{
ViewState[
" DataCountField " ] = value;
}

}


[Bindable(
true ),Category( " Data " ),Localizable( true )]
public string KeywordTitleFormat
{
get
{
string s = ( string )ViewState[ " KeywordTitleFormat " ];
if (String.IsNullOrEmpty(s))
{
return " %koccured%ctimes " ;
}

else
{
return ( string )ViewState[ " KeywordTitleFormat " ];
}

}

set
{
ViewState[
" KeywordTitleFormat " ] = value;
}

}


[Bindable(
true ),Category( " Data " ),Localizable( true )]
public string KeywordURLFormat
{
get
{
return ( string )ViewState[ " KeywordURLFormat " ];
}

set
{
ViewState[
" KeywordURLFormat " ] = value;
}

}


[Bindable(
true ),Category( " Data " ),DefaultValue( "" ),Localizable( true )]
public string SortBy
{
get
{
return ( string )ViewState[ " SortBy " ];
}

set
{
ViewState[
" SortBy " ] = value;
}

}


#endregion


[Bindable(
false ),Category( " Debug " ),DefaultValue( false ),Localizable( true )]
public BooleanDebug
{
get
{
if (ViewState[ " Debug " ] != null )
{
return (Boolean)ViewState[ " Debug " ];
}

else
{
return false ;
}


}

set
{
ViewState[
" Debug " ] = value;
}

}


PrivateProperties #region PrivateProperties

private HashtablearrAttributes;

private string CloudHTML
{
get
{
string s = ( string )ViewState[ " CloudHTML " ];
if (String.IsNullOrEmpty(s))
{
return string .Empty;
}

return s;
}

set
{
ViewState[
" CloudHTML " ] = value;
}

}


private HashtableKeyAttributes
{
get
{
return arrAttributes;
}

set
{
arrAttributes
= value;
}

}


#endregion


#endregion


protected override void Render(HtmlTextWriterwriter)
{
if ( ! String.IsNullOrEmpty(CloudHTML))
{
writer.WriteBeginTag(
" div " );

if ( ! String.IsNullOrEmpty(CssClass))
{
writer.WriteAttribute(
" class " ,CssClass);
}

writer.Write(HtmlTextWriter.TagRightChar);
writer.Write(CloudHTML);
writer.WriteEndTag(
" div " );
}

else
{
writer.Write(
" 这儿没有产生HTML,一个未操作错误发生. " );
}

}


protected override void OnLoad(EventArgse)
{
if (DataSource == null )
{
CloudHTML
= " 请指定DataSet " ;
return ;
}

if (DataIDField == string .Empty)
{
CloudHTML
= " 请指定一个ID数据段 " ;
return ;
}

if (DataKeywordField == string .Empty)
{
CloudHTML
= " 请指定一个关键词数据段 " ;
return ;
}

if (DataCountField == string .Empty)
{
CloudHTML
= " 请指定一个关键词数量数据段 " ;
return ;
}

if ( ! Regex.IsMatch(MinColor, " ^#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$ " ))
{
CloudHTML
= " 最小颜色必须为十六进制编码并且必须为如:#000or#ff99cc " ;
return ;
}

if ( ! Regex.IsMatch(MaxColor, " ^#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$ " ))
{
CloudHTML
= " 最小颜色必须为十六进制编码并且必须为如:#000or#ff99cc " ;
return ;
}


try
{
StringBuildersb
= new StringBuilder();
DataViewdv
= new DataView(DataSource.Tables[ 0 ]);
// DataRowViewrow;

dv.Sort
= string .Format( " {0}DESC " ,DataCountField);

int count = dv.Count;

if (count == 0 )
{
CloudHTML
= " 没有任何值产生云 " ;
return ;
}


int MaxQty = int .Parse(dv[ 0 ].Row[DataCountField].ToString());
int MinQty = int .Parse(dv[dv.Count - 1 ].Row[DataCountField].ToString());

int Spread = MaxQty - MinQty;

if (Spread == 0 )
Spread
= 1 ;

int FontSpread = MaxFontSize - MinFontSize;
if (FontSpread == 0 )
FontSpread
= 1 ;

double FontStep = ( double )(FontSpread) / Spread;

if ( ! string .IsNullOrEmpty(SortBy))
{
dv.Sort
= SortBy;
}

else
{
dv.Sort
= string .Format( " {0}ASC " ,DataKeywordField);
}


foreach (DataRowViewrow in dv)
{
int sKeyID = int .Parse(row.Row[DataIDField].ToString());
string sKeyWord = row.Row[DataKeywordField].ToString();
int sKeyCount = int .Parse(row.Row[DataCountField].ToString());
string sKeyURL;
string ColorRGB;
double Weight = MinFontSize + ((sKeyCount - MinQty) * FontStep);
int FontDiff = MaxFontSize - MinFontSize;
double ColorWeight = Math.Round( 99 * (Weight - MinFontSize) / (FontDiff) + 1 );

if (MinColor == MaxColor)
{
ColorRGB
= MinColor;
}

else
{
ColorRGB
= Colorize(MinColor,MaxColor,ColorWeight);
}


if (String.IsNullOrEmpty(DataURLField))
{
if ( ! String.IsNullOrEmpty(KeywordURLFormat))
{
sKeyURL
= ReplaceKeyValues(KeywordURLFormat,sKeyID,sKeyWord, "" ,sKeyCount);
}

else
{
sKeyURL
= " # " ;
}

}

else
{
sKeyURL
= row[DataURLField].ToString();
}

sb.Append(
string .Format( " <ahref=/ " { 0 } / " style=/ " font - size: { 1 } { 5 } ;color: { 4 } ;/ " title=/ " { 2 } / " {6}>{3}</a> " ,
sKeyURL,
Math.Round(Weight),
ReplaceKeyValues(KeywordTitleFormat,sKeyID,sKeyWord,sKeyURL,sKeyCount),
HttpContext.Current.Server.HtmlEncode(sKeyWord),
ColorRGB,
FontUint,
GenerateAttributes(sKeyWord,sKeyID,sKeyURL,sKeyCount)));
}

CloudHTML
= sb.ToString();
}

catch (Exceptionex)
{
if ( ! Debug)
{
CloudHTML
= " 错误产生 " ;
}

else
{
throw ex;
}

}

finally
{
base .OnLoad(e);
}

}


public void AddAttribute( string value, string text)
{
if (KeyAttributes == null )
{
KeyAttributes
= new Hashtable();
}

KeyAttributes.Add(value,text);
}


private string GenerateAttributes( string k, int id, string u, int c)
{
if (KeyAttributes == null )
{
return string .Empty;
}


StringBuilders
= new StringBuilder();
ICollectionkeys
= KeyAttributes.Keys;

foreach ( object key in keys)
{
s.Append(
string .Format( " {0}=/ " { 1 } / "" ,key,ReplaceKeyValues(KeyAttributes[key].ToString(),id,k,u,c)));
}


return s.ToString();
}


private string ReplaceKeyValues( string txt, int id, string k, string u, int c)
{
k
= k.Replace( " ' " , " &apos; " );

txt
= txt.Replace( " %i " ,id.ToString());
txt
= txt.Replace( " %k " ,HttpContext.Current.Server.HtmlEncode(k));
txt
= txt.Replace( " %u " ,u);
txt
= txt.Replace( " %c " ,c.ToString());

return txt;
}


private string Colorize( string minc, string maxc, double w)
{
w
= w / 100 ;
string rs,gs,bs;
string r,g,b;
int minr,ming,minb,maxr,maxg,maxb;

if (minc.Length == 4 )
{
rs
= minc.Substring( 1 , 1 );
gs
= minc.Substring( 2 , 1 );
bs
= minc.Substring( 3 , 1 );

minc
= " # " + rs + rs + gs + gs + bs + bs;
}


if (maxc.Length == 4 )
{
rs
= maxc.Substring( 1 , 1 );
gs
= maxc.Substring( 2 , 1 );
bs
= maxc.Substring( 3 , 1 );

maxc
= " # " + rs + rs + gs + gs + bs + bs;
}


minr
= Convert.ToInt32(minc.Substring( 1 , 2 ), 16 );
ming
= Convert.ToInt32(minc.Substring( 3 , 2 ), 16 );
minb
= Convert.ToInt32(minc.Substring( 5 , 2 ), 16 );

maxr
= Convert.ToInt32(maxc.Substring( 1 , 2 ), 16 );
maxg
= Convert.ToInt32(maxc.Substring( 3 , 2 ), 16 );
maxb
= Convert.ToInt32(maxc.Substring( 5 , 2 ), 16 );

r
= Convert.ToString( int .Parse(Math.Round(((maxr - minr) * w) + minr).ToString()), 16 );
g
= Convert.ToString( int .Parse(Math.Round(((maxg - ming) * w) + ming).ToString()), 16 );
b
= Convert.ToString( int .Parse(Math.Round(((maxb - minb) * w) + minb).ToString()), 16 );

if (r.Length == 1 )
r
= " 0 " + r;
if (g.Length == 1 )
g
= " 0 " + g;
if (b.Length == 1 )
b
= " 0 " + b;

string color;
color
= " # " + r + g + b;

return color;
}

}

}

C#版 Tag云图控件


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论