在ASP.NET页面中添加关键字云

时间:2009-11-05 08:16:37

标签: c# asp.net keyword

我需要在我的asp网页中添加关键字云!有谁能建议我这样做的最好方法?我使用C#实现了一些工作示例,但我想知道是否还有其他可能性!任何好的建议都会非常感激!在此先感谢:)

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码..

        Int32 rank, minVal, maxVal, scale, s;

        minVal = st.GetMinRank();
        maxVal = st.GetMaxRank();
        scale = (maxVal - minVal) / 6;
        rank = Convert.ToInt32(yourRankValue that is stored in your DB);

        if (scale == 0)
            s = 0;
        else
            s = (rank - minVal) / scale;

        if (s == 0)
        {
            lblSTName.Font.Size = FontUnit.Smaller;
        }
        else if (s == 1)
        {
            lblSTName.Font.Size = FontUnit.Small;
        }
        else if (s == 2)
        {
            lblSTName.Font.Size = FontUnit.Medium;
        }
        else if (s == 3)
        {
            lblSTName.Font.Size = FontUnit.Large;
        }
        else if (s == 4)
        {
            lblSTName.Font.Size = FontUnit.Larger;
        }
        else if (s >= 5)
        {
            lblSTName.Font.Size = FontUnit.Larger;
            lblSTName.Font.Bold = true;
        }

答案 1 :(得分:0)

我想在某种程度上,答案取决于关键字数据的存储方式和位置。

我有一个包含以下表格的数据库:

Photos-TagsPhotos-Tags

所以照片通过TagsPhotos表链接到标签。

然后我使用以下方法生成关键字云:

protected void Page_Load(object sender, EventArgs e) {

  // "zhpCtx" is a LINQ to SQL data context.
  // "TagCloud" is a Panel control.

  // Select a list of tags and a count of how often they appear in TagsPhotos
  var tagDensity = from t in zhpCtx.siteContent_Tags
                 join tp in zhpCtx.siteContent_TagsPhotos 
                   on t.TagID equals tp.TagID
                 group t by new { t.TagID, t.TagText } into g
                 where g.Count() > 0
                 orderby g.Key.TagText
                 select new { g.Key.TagID, g.Key.TagText, NumTags = g.Count() };

  if (null != tagDensity && tagDensity.Count() > 0) {
    // We have some tags, get the lowest number of counts greater than 0, and 
    // the highest to give our range.
    int? min = tagDensity.Min(t => t.NumTags);
    int? max = tagDensity.Max(t => t.NumTags);

    // Loop through each tag
    foreach (var tag in tagDensity) {
      // Create a new HyperLink control to take the user to a given tag
      // Include the tag count in the list for accessibility.
      // Could probably move it to the title attribute.
      // Build the link however suits.
      HyperLink tagLink = new HyperLink { 
                NavigateUrl = string.Format("Photos.aspx?TagID={0}", tag.TagID),
                Text = string.Format("{0} ({1})", tag.TagText, tag.NumTags) };

      // Adjust the font-size of the tag link - calling getPercentage.
      // This will adjust the size based on the baseline font size for the 
      // container
      tagLink.Style.Add("font-size", 
        string.Format("{0}%", 
                       getPercentageSize((int)max, (int)min, tag.NumTags)));

      // Add the HyperLink to the Panel.
      TagCloud.Controls.Add(tagLink);
      // Add a LiteralControl with a comma and a space.
      TagCloud.Controls.Add(new LiteralControl(", "));
    }

    // Remove the last separator control from the Panel.
    TagCloud.Controls.RemoveAt(TagCloud.Controls.Count - 1);
  } else {
    // Hide the tag cloud panel
    TagCloud.Visible = false;
  }
}

private int getPercentageSize(int max, int min, int score) {
    // Ensure we've got a sensible number
    if (min < 1) {
        min = 1;
    }

    double spread = max - min;

    if (spread == 0) {
        spread = 1;
    }

    // Setup sensible bounds for the font sizes
    int minSize = 80;
    int maxSize = 200;

    // Step ensures that our least used keyword is 80% and our 
    // most used is 200%
    double step = (maxSize - minSize) / spread;

    return (int)(minSize + ((score - min) * step));
}