• C#版 Tag云图控件


    今天看到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}:SearchCloud runat=server></{0}:SearchCloud>")]
        
    public class SearchCloud : WebControl
        
    {
            
    "属性"

            
    protected override void Render(HtmlTextWriter writer)
            
    {
                
    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(EventArgs e)
            
    {
                
    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 
    = "最小颜色必须为十六进制编码并且必须为如: #000 or #ff99cc";
                    
    return;
                }

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


                
    try
                
    {
                    StringBuilder sb 
    = new StringBuilder();
                    DataView dv 
    = new DataView(DataSource.Tables[0]);
                    
    //DataRowView row;

                    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 (DataRowView row 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("<a href=\"{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 (Exception ex)
                
    {
                    
    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;
                }


                StringBuilder s 
    = new StringBuilder();
                ICollection keys 
    = 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(11);
                    gs 
    = minc.Substring(21);
                    bs 
    = minc.Substring(31);

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


                
    if (maxc.Length == 4)
                
    {
                    rs 
    = maxc.Substring(11);
                    gs 
    = maxc.Substring(21);
                    bs 
    = maxc.Substring(31);

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


                minr 
    = Convert.ToInt32(minc.Substring(12), 16);
                ming 
    = Convert.ToInt32(minc.Substring(32), 16);
                minb 
    = Convert.ToInt32(minc.Substring(52), 16);

                maxr 
    = Convert.ToInt32(maxc.Substring(12), 16);
                maxg 
    = Convert.ToInt32(maxc.Substring(32), 16);
                maxb 
    = Convert.ToInt32(maxc.Substring(52), 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;
            }

        }

    }

  • 相关阅读:
    使用srvany.exe将任何程序作为Windows服务运行
    instsrv.exe用法
    在博客园中发现的一篇文章,感觉这些内容就是我心中所想表达的!
    HTML5的Video标签的属性,方法和事件汇总
    使用nodejs 来压缩整个目录
    git 基础
    mac 上安装 redis
    第12次实验总结
    第12次实验作业
    第十一次实验总结
  • 原文地址:https://www.cnblogs.com/liping13599168/p/1076645.html
Copyright © 2020-2023  润新知