【问题标题】:TagCloud size problemTagCloud 大小问题
【发布时间】:2011-02-06 23:48:03
【问题描述】:

我正在使用代码来显示基于百分比值的标签云。第一次将标签添加到我的数据库时,它看起来很大,然后相对于其他标签缩小(添加的次数越多)。代码如下:

private void BindTagCloud()
{

    int pro_id = Convert.ToInt32(proj_id);
    int iteration_id = Convert.ToInt32(iteration);

    var tagSummaryNegative = from af in db.AgileFactors
                     join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                     join s in db.Stories on psf.StoryID equals s.StoryID
                     join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                     join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                     join pro in db.Projects on it.ProjectID equals pro.ProjectID
                     where pro.ProjectID == pro_id &&
                           pro.ProjectID == it.ProjectID &&
                           it.ProjectIterationID == pim.ProjectIterationID &&
                           pim.ProjectIterationMemberID == s.ProjectIterationMemberID && s.StoryCategoryID == 1 &&
                           s.StoryID == psf.StoryID &&
                           psf.AgileFactorID == af.AgileFactorID
                     group af by af.Name into tagGroup

                     select new
                     {

                         Tag = tagGroup.Key,
                         tagCount = tagGroup.Count()

                     };

    var tagSummaryNegativeIteration = from af in db.AgileFactors
                             join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                             join s in db.Stories on psf.StoryID equals s.StoryID
                             join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                             join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                             join pro in db.Projects on it.ProjectID equals pro.ProjectID
                             where pro.ProjectID == pro_id &&
                                   pro.ProjectID == it.ProjectID &&
                                   it.ProjectIterationID == iteration_id &&
                                   it.ProjectIterationID == pim.ProjectIterationID &&
                                   pim.ProjectIterationMemberID == s.ProjectIterationMemberID && s.StoryCategoryID == 1 &&
                                   s.StoryID == psf.StoryID &&
                                   psf.AgileFactorID == af.AgileFactorID
                             group af by af.Name into tagGroup

                             select new
                             {

                                 Tag = tagGroup.Key,
                                 tagCount = tagGroup.Count()

                             };

    var tagSummaryPositive = from af in db.AgileFactors
                             join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                             join s in db.Stories on psf.StoryID equals s.StoryID
                             join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                             join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                             join pro in db.Projects on it.ProjectID equals pro.ProjectID
                             where pro.ProjectID == pro_id &&
                                   pro.ProjectID == it.ProjectID &&
                                   it.ProjectIterationID == pim.ProjectIterationID &&
                                   pim.ProjectIterationMemberID == s.ProjectIterationMemberID && s.StoryCategoryID == 0 &&
                                   s.StoryID == psf.StoryID &&
                                   psf.AgileFactorID == af.AgileFactorID
                             group af by af.Name into tagGroup

                             select new
                             {

                                 Tag = tagGroup.Key,
                                 tagCount = tagGroup.Count()

                             };

    var tagSummaryPositiveIteration = from af in db.AgileFactors
                             join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                             join s in db.Stories on psf.StoryID equals s.StoryID
                             join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                             join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                             join pro in db.Projects on it.ProjectID equals pro.ProjectID
                             where pro.ProjectID == pro_id &&
                                   pro.ProjectID == it.ProjectID &&
                                   it.ProjectIterationID == iteration_id &&
                                   it.ProjectIterationID == pim.ProjectIterationID &&
                                   pim.ProjectIterationMemberID == s.ProjectIterationMemberID && s.StoryCategoryID == 0 &&
                                   s.StoryID == psf.StoryID &&
                                   psf.AgileFactorID == af.AgileFactorID
                             group af by af.Name into tagGroup

                             select new
                             {

                                 Tag = tagGroup.Key,
                                 tagCount = tagGroup.Count()

                             };


    int maxTagFrequencyNegative = (from t in tagSummaryNegative select (int?)t.tagCount).Max() ?? 0;
    int maxTagFrequencyPositive = (from t in tagSummaryPositive select (int?)t.tagCount).Max() ?? 0;

    int maxTagFrequencyNegativeIteration = (from t in tagSummaryNegativeIteration select (int?)t.tagCount).Max() ?? 0;
    int maxTagFrequencyPositiveIteration = (from t in tagSummaryPositiveIteration select (int?)t.tagCount).Max() ?? 0;


    var tagCloudNegative = from af in db.AgileFactors
                   join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                   join s in db.Stories on psf.StoryID equals s.StoryID
                   join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                   join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                   join pro in db.Projects on it.ProjectID equals pro.ProjectID
                   where pro.ProjectID == pro_id &&
                         pro.ProjectID == it.ProjectID &&
                         it.ProjectIterationID == pim.ProjectIterationID &&
                         pim.ProjectIterationMemberID == s.ProjectIterationMemberID && s.StoryCategoryID == 1 &&
                         s.StoryID == psf.StoryID &&
                         psf.AgileFactorID == af.AgileFactorID
                   group af by af.Name into tagGroup
                   select new
                   {

                       Tag = tagGroup.Key,
                       weight = (double)tagGroup.Count() / maxTagFrequencyNegative * 100
                   };

    var tagCloudNegativeIteration = from af in db.AgileFactors
                           join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                           join s in db.Stories on psf.StoryID equals s.StoryID
                           join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                           join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                           join pro in db.Projects on it.ProjectID equals pro.ProjectID
                           where pro.ProjectID == pro_id &&
                                 pro.ProjectID == it.ProjectID &&
                                 it.ProjectIterationID == iteration_id &&
                                 it.ProjectIterationID == pim.ProjectIterationID &&
                                 pim.ProjectIterationMemberID == s.ProjectIterationMemberID && s.StoryCategoryID == 1 &&
                                 s.StoryID == psf.StoryID &&
                                 psf.AgileFactorID == af.AgileFactorID
                           group af by af.Name into tagGroup
                           select new
                           {

                               Tag = tagGroup.Key,
                               weight = (double)tagGroup.Count() / maxTagFrequencyNegativeIteration * 100
                           };

    var tagCloudPositive = from af in db.AgileFactors
                           join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                           join s in db.Stories on psf.StoryID equals s.StoryID
                           join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                           join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                           join pro in db.Projects on it.ProjectID equals pro.ProjectID
                           where pro.ProjectID == pro_id &&
                                 pro.ProjectID == it.ProjectID &&
                                 it.ProjectIterationID == pim.ProjectIterationID &&
                                 pim.ProjectIterationMemberID == s.ProjectIterationMemberID && s.StoryCategoryID == 0 &&
                                 s.StoryID == psf.StoryID &&
                                 psf.AgileFactorID == af.AgileFactorID
                           group af by af.Name into tagGroup
                           select new
                           {

                               Tag = tagGroup.Key,
                               weight = (double)tagGroup.Count() / maxTagFrequencyPositive * 100
                           };

    var tagCloudPositiveIteration = from af in db.AgileFactors
                                    join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                                    join s in db.Stories on psf.StoryID equals s.StoryID
                                    join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                                    join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                                    join pro in db.Projects on it.ProjectID equals pro.ProjectID
                                    where pro.ProjectID == pro_id &&
                                          pro.ProjectID == it.ProjectID &&
                                          it.ProjectIterationID == iteration_id &&
                                          it.ProjectIterationID == pim.ProjectIterationID &&
                                          pim.ProjectIterationMemberID == s.ProjectIterationMemberID && s.StoryCategoryID == 0 &&
                                          s.StoryID == psf.StoryID &&
                                          psf.AgileFactorID == af.AgileFactorID
                                    group af by af.Name into tagGroup
                                    select new
                                    {

                                        Tag = tagGroup.Key,
                                        weight = (double)tagGroup.Count() / maxTagFrequencyPositiveIteration * 100
                                    };

    if (iteration_id != 0)
    {
        ListView1.DataSource = tagCloudNegativeIteration;
        ListView1.DataBind();

        ListView2.DataSource = tagCloudPositiveIteration;
        ListView2.DataBind();

    }
    else
    {
        ListView1.DataSource = tagCloudNegative;
        ListView1.DataBind();

        ListView2.DataSource = tagCloudPositive;
        ListView2.DataBind();

    }

}

public string GetTagSize(double weight)
{

    if (weight >= 99)
        return "xx-large";
    else if (weight >= 60)
        return "x-large";
    else if (weight >= 40)
        return "large";
    else if (weight >= 20)
        return "medium";
    else
        return "small";
}

然而,我想做的是首先使标签变小(第一次添加时),然后随着被提及的次数增加其大小。有谁知道我怎么能做到这一点?非常感谢您的帮助!

【问题讨论】:

    标签: c# asp.net linq linq-to-sql


    【解决方案1】:

    根据您显示的内容,标签应该已经开始看起来很小 - tagGroup.Count() 将为 1 并且 GetTagSize 将返回“小”

    某些东西必须覆盖此代码以强制字体大小变大 - 把它去掉,我认为你应该得到你所要求的。

    【讨论】:

    • 我就是这么想的。我已经更新了我的帖子以显示我的代码。你能看出问题出在哪里吗?谢谢..
    【解决方案2】:

    你为什么不拿这个:

    weight = (((double)tagGroup.Count() / maxTagFrequencyNegative * 100) - 100) * -1
    

    这样你就可以取最大的数并将它们反转。

    【讨论】:

    • 我试过这样做。起初标签看起来很小,但随着我将更多标签添加到我的数据库中,添加次数较少的标签看起来比出现频率高的标签大......
    【解决方案3】:

    我简化了您的代码以在我的盒子上运行它。一切似乎都很好。在这里你可以看到我的测试页面:

    Test.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication2.Test" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    
        <style type="text/css">
            .xx-large
            {
                font-size: 30px;
            }
    
            .x-large
            {
                font-size: 25px;
            }
    
            .large
            {
                font-size: 20px;
            }
    
            .medium
            {
                font-size: 15px;
            }
    
            .small
            {
                font-size: 10px;
            }
            .tagCloud
            {
                padding: 20px;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
        <div class=tagCloud>
            <asp:ListView ID="ListView1" runat="server">
                <ItemTemplate>
                    <span class='<%# GetTagSize((double)Eval("weight")) %>'>
                        <%# Eval("Tag") %></span>
                </ItemTemplate>
            </asp:ListView>
        </div>
        <div class=tagCloud>
            <asp:ListView ID="ListView2" runat="server">
                <ItemTemplate>
                    <span class='<%# GetTagSize((double)Eval("weight")) %>'>
                        <%# Eval("Tag") %></span>
                </ItemTemplate>
            </asp:ListView>
        </div>
        </form>
    </body>
    </html>
    

    Test.aspx.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication2
    {
        public partial class Test : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                BindTagCloud();
            }
    
            public class Race
            {
                public string GrandPrix { get; set; }
                public string TeamWinner { get; set; }
                public int Year { get; set; }
            }
    
            public IEnumerable<Race> GetRaces()
            {
                yield return new Race { GrandPrix = "Australia", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Malaysia", TeamWinner = "Williams-BMW", Year = 2002 };
                yield return new Race { GrandPrix = "Brazil", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "San Marino", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Spain", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Austria", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Monaco", TeamWinner = "McLaren-Mercedes", Year = 2002 };
                yield return new Race { GrandPrix = "Canada", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Europe", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Great Britain", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "France", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Germany", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Hungary", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Belgium", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Italy", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "United States", TeamWinner = "Ferrari", Year = 2002 };
                yield return new Race { GrandPrix = "Japan", TeamWinner = "Ferrari", Year = 2002 };
    
                yield return new Race { GrandPrix = "Australia", TeamWinner = "McLaren-Mercedes", Year = 2003 };
                yield return new Race { GrandPrix = "Malaysia", TeamWinner = "McLaren-Mercedes", Year = 2003 };
                yield return new Race { GrandPrix = "Brazil", TeamWinner = "Jordan-Ford", Year = 2003 };
                yield return new Race { GrandPrix = "San Marino", TeamWinner = "Ferrari", Year = 2003 };
                yield return new Race { GrandPrix = "Spain", TeamWinner = "Ferrari", Year = 2003 };
                yield return new Race { GrandPrix = "Austria", TeamWinner = "Ferrari", Year = 2003 };
                yield return new Race { GrandPrix = "Monaco", TeamWinner = "Williams-BMW", Year = 2003 };
                yield return new Race { GrandPrix = "Canada", TeamWinner = "Ferrari", Year = 2003 };
                yield return new Race { GrandPrix = "Europe", TeamWinner = "Williams-BMW", Year = 2003 };
                yield return new Race { GrandPrix = "France", TeamWinner = "Williams-BMW", Year = 2003 };
                yield return new Race { GrandPrix = "Great Britain", TeamWinner = "Ferrari", Year = 2003 };
                yield return new Race { GrandPrix = "Germany", TeamWinner = "Williams-BMW", Year = 2003 };
                yield return new Race { GrandPrix = "Hungary", TeamWinner = "Renault", Year = 2003 };
                yield return new Race { GrandPrix = "Italy", TeamWinner = "Ferrari", Year = 2003 };
                yield return new Race { GrandPrix = "United States", TeamWinner = "Ferrari", Year = 2003 };
                yield return new Race { GrandPrix = "Japan", TeamWinner = "Ferrari", Year = 2003 };
            }
    
            private void BindTagCloud()
            {
                int year = 2002;
    
                var tagSummaryNegative = from t in GetRaces()
                                         where t.Year == 2002
                                         group t by t.TeamWinner into tagGroup
                                         select new
                                         {
    
                                             Tag = tagGroup.Key,
                                             tagCount = tagGroup.Count()
    
                                         };
    
                var tagSummaryNegativeIteration = from t in GetRaces()
                                                  where t.Year == 2002
                                                  group t by t.TeamWinner into tagGroup
                                                  select new
                                                  {
    
                                                      Tag = tagGroup.Key,
                                                      tagCount = tagGroup.Count()
    
                                                  };
    
                var tagSummaryPositive = from t in GetRaces()
                                         where t.Year == 2003
                                         group t by t.TeamWinner into tagGroup
                                         select new
                                         {
    
                                             Tag = tagGroup.Key,
                                             tagCount = tagGroup.Count()
    
                                         };
    
                var tagSummaryPositiveIteration = from t in GetRaces()
                                                  where t.Year == 2003
                                                  group t by t.TeamWinner into tagGroup
                                                  select new
                                                  {
    
                                                      Tag = tagGroup.Key,
                                                      tagCount = tagGroup.Count()
    
                                                  };
    
    
                int maxTagFrequencyNegative = (from t in tagSummaryNegative select (int?)t.tagCount).Max() ?? 0;
                int maxTagFrequencyPositive = (from t in tagSummaryPositive select (int?)t.tagCount).Max() ?? 0;
    
                int maxTagFrequencyNegativeIteration = (from t in tagSummaryNegativeIteration select (int?)t.tagCount).Max() ?? 0;
                int maxTagFrequencyPositiveIteration = (from t in tagSummaryPositiveIteration select (int?)t.tagCount).Max() ?? 0;
    
    
                var tagCloudNegative = from t in GetRaces()
                                       where t.Year == 2002
                                       group t by t.TeamWinner into tagGroup
                                       select new
                                       {
    
                                           Tag = tagGroup.Key,
                                           weight = (double)tagGroup.Count() / maxTagFrequencyNegative * 100
                                       };
    
                var tagCloudNegativeIteration = from t in GetRaces()
                                                where t.Year == 2002
                                                group t by t.TeamWinner into tagGroup
                                                select new
                                                {
    
                                                    Tag = tagGroup.Key,
                                                    weight = (double)tagGroup.Count() / maxTagFrequencyNegativeIteration * 100
                                                };
    
                var tagCloudPositive = from t in GetRaces()
                                       where t.Year == 2003
                                       group t by t.TeamWinner into tagGroup
                                       select new
                                       {
    
                                           Tag = tagGroup.Key,
                                           weight = (double)tagGroup.Count() / maxTagFrequencyPositive * 100
                                       };
    
                var tagCloudPositiveIteration = from t in GetRaces()
                                                where t.Year == 2003
                                                group t by t.TeamWinner into tagGroup
                                                select new
                                                {
    
                                                    Tag = tagGroup.Key,
                                                    weight = (double)tagGroup.Count() / maxTagFrequencyPositiveIteration * 100
                                                };
    
                if (year == 2002)
                {
                    ListView1.DataSource = tagCloudNegativeIteration;
                    ListView1.DataBind();
    
                    ListView2.DataSource = tagCloudPositiveIteration;
                    ListView2.DataBind();
    
                }
                else
                {
                    ListView1.DataSource = tagCloudNegative;
                    ListView1.DataBind();
    
                    ListView2.DataSource = tagCloudPositive;
                    ListView2.DataBind();
    
                }
            }
    
            public string GetTagSize(double weight)
            {
    
                if (weight >= 99)
                    return "xx-large";
                else if (weight >= 60)
                    return "x-large";
                else if (weight >= 40)
                    return "large";
                else if (weight >= 20)
                    return "medium";
                else
                    return "small";
            }
        }
    }
    

    在这里你可以看到结果: ![标签云][1]

    我建议您稍微调试一下您的代码并检查所有数字是否正确。可能是你在查询中遇到了一些问题(不正确的连接或类似的东西),因为所有其他代码对我来说都很好。

    希望这会有所帮助。

    附:可能是你的 css 有问题?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多