/// <summary> /// Creates Tag objects from a provided dictionary of string tags along with integer values indicating the weight of each tag. /// This overload is suitable when you have a list of already weighted tags, i.e. from a database query result. /// </summary> /// <param name="weightedTags">A dictionary that takes a string for the tag text (as the dictionary key) and an integer for the tag weight (as the dictionary value).</param> /// <param name="rules">A TagCloudGenerationRules object to decide how the cloud is generated.</param> /// <returns>A list of Tag objects that can be used to create the tag cloud.</returns> public static IEnumerable<Tag> CreateTags(IDictionary<string, int> weightedTags, TagCloudGenerationRules generationRules) { #region Parameter validation if (weightedTags == null) throw new ArgumentNullException("weightedTags"); if (generationRules == null) throw new ArgumentNullException("generationRules"); #endregion return CreateTags(weightedTags.ToList(), generationRules); }
/// <summary> /// Creates Tag objects from a provided list of string tags. /// The more times a tag occures in the list, the larger weight it will get in the tag cloud. /// </summary> /// <param name="tags">A string list of tags</param> /// <param name="rules">A TagCloudGenerationRules object to decide how the cloud is generated.</param> /// <returns>A list of Tag objects that can be used to create the tag cloud.</returns> public static IEnumerable<Tag> CreateTags(IEnumerable<string> tags, TagCloudGenerationRules generationRules) { #region Parameter validation if (tags == null) throw new ArgumentNullException("tags"); if (generationRules == null) throw new ArgumentNullException("generationRules"); #endregion //Transform tag string list to list with each distinct tag and its weight return CreateTags(from tag in tags group tag by tag into tagGroup select new KeyValuePair<string, int>(tagGroup.Key, tagGroup.Count()), generationRules); }
/// <summary> /// Creates tag cloud html from a provided list of string tags along with integer values indicating the weight of each tag. /// This overload is suitable when you have a list of already weighted tags, i.e. from a database query result. /// </summary> /// <param name="weightedTags">A list of KeyValuePair objects that take a string for the tag text and an integer for the weight of the tag.</param> /// <param name="generationRules">A TagCloudGenerationRules object to decide how the cloud is generated.</param> /// <returns>A string containing the html markup of the tag cloud.</returns> public static MvcHtmlString TagCloud(this HtmlHelper htmlHelper, IEnumerable<KeyValuePair<string, int>> weightedTags, TagCloudGenerationRules generationRules) { return MvcHtmlString.Create(new TagCloud(weightedTags, generationRules).ToString()); }
/// <summary> /// Creates tag cloud html from a provided dictionary of string tags along with integer values indicating the weight of each tag. /// This overload is suitable when you have a list of already weighted tags, i.e. from a database query result. /// </summary> /// <param name="weightedTags">A dictionary that takes a string for the tag text (as the dictionary key) and an integer for the tag weight (as the dictionary value).</param> /// <param name="generationRules">A TagCloudGenerationRules object to decide how the cloud is generated.</param> /// <returns>A string containing the html markup of the tag cloud.</returns> public static MvcHtmlString TagCloud(this HtmlHelper htmlHelper, IDictionary<string, int> weightedTags, TagCloudGenerationRules generationRules) { return MvcHtmlString.Create(new TagCloud(weightedTags, generationRules).ToString()); }
/// <summary> /// Creates tag cloud html from a provided list of string tags. /// The more times a tag occures in the list, the larger weight it will get in the tag cloud. /// </summary> /// <param name="tags">A string list of tags</param> /// <param name="generationRules">A TagCloudGenerationRules object to decide how the cloud is generated.</param> /// <returns>A string containing the html markup of the tag cloud.</returns> public static MvcHtmlString TagCloud(this HtmlHelper htmlHelper, IEnumerable<string> tags, TagCloudGenerationRules generationRules) { return MvcHtmlString.Create(new TagCloud(tags, generationRules).ToString()); }
/// <summary> /// Creates Tag objects from a provided list of string tags along with integer values indicating the weight of each tag. /// This overload is suitable when you have a list of already weighted tags, i.e. from a database query result. /// </summary> /// <param name="weightedTags">A list of KeyValuePair objects that take a string for the tag text and an integer for the weight of the tag.</param> /// <param name="rules">A TagCloudGenerationRules object to decide how the cloud is generated.</param> /// <returns>A list of Tag objects that can be used to create the tag cloud.</returns> public static IEnumerable<Tag> CreateTags(IEnumerable<KeyValuePair<string, int>> weightedTags, TagCloudGenerationRules generationRules) { #region Parameter validation if (weightedTags == null) throw new ArgumentNullException("weightedTags"); if (generationRules == null) throw new ArgumentNullException("generationRules"); #endregion // Select all tags that exists "settings.RequiredTagWeight" times or more and order them by weight. weightedTags = from weightedTag in weightedTags where weightedTag.Value >= generationRules.RequiredTagWeight orderby weightedTag.Value descending select weightedTag; // Crop list if "settings.MaxNumberOfTags" is specified if (generationRules.MaxNumberOfTags.HasValue) weightedTags = weightedTags.Take(generationRules.MaxNumberOfTags.Value); // Change sort order if necessary (the list is already ordered by weight descending) switch (generationRules.Order) { case TagCloudOrder.Alphabetical: // Renders tags alphabetically weightedTags = weightedTags.OrderBy(tag => tag.Key); break; case TagCloudOrder.AlphabeticalDescending: // Renders tags alphabetically descending weightedTags = weightedTags.OrderByDescending(tag => tag.Key); break; case TagCloudOrder.Weight: // Renders tags with higher weight at the end weightedTags = weightedTags.OrderBy(tag => tag.Value); break; case TagCloudOrder.Centralized: // Renders tags with higher weight in the middle weightedTags = weightedTags.OrderBy(tag => tag.Value); weightedTags = weightedTags.Where((kvp, i) => (i % 2 == 0)).Concat(weightedTags.Where((kvp, i) => (i % 2 == 1)).Reverse()); break; case TagCloudOrder.Decentralized: // Renders tags with higher weight at the edges (start and end) weightedTags = weightedTags.OrderBy(tag => tag.Value); weightedTags = weightedTags.Where((kvp, i) => (i % 2 == 0)).Reverse().Concat(weightedTags.Where((kvp, i) => (i % 2 == 1))); break; case TagCloudOrder.Random: // Renders tags rendomly weightedTags = weightedTags.OrderBy(tag => tag, RandomComparer.Comparer); break; } // Retrieve the css class table used to decide the style of the tags Dictionary<int, string> cssClassTable = GenerateCssClassTable(weightedTags, generationRules.TagCssClassPrefix, generationRules.WeightClassPartitioning.ToArray()); // Transform all the string tags into Tag objects IEnumerable<Tag> cloudTags = from weightedTag in weightedTags select new Tag { Text = weightedTag.Key, TagWeight = weightedTag.Value, CssClass = cssClassTable[weightedTag.Value], NavigateUrl = string.Format(generationRules.TagUrlFormatString, HttpUtility.UrlEncode(weightedTag.Key)), ToolTip = string.Format(generationRules.TagToolTipFormatString, weightedTag.Value), HtmlAttributes = generationRules.HtmlAttributes }; return cloudTags; }
/// <summary> /// Creates a tag cloud from a provided list of string tags along with integer values indicating the weight of each tag. /// This constructor is suitable when you have a list of already weighted tags, i.e. from a database query result. /// </summary> /// <param name="weightedTags">A list of KeyValuePair objects that take a string for the tag text and an integer for the weight of the tag.</param> /// <param name="generationRules">A TagCloudGenerationRules object to decide how the cloud is generated.</param> public TagCloud(IEnumerable<KeyValuePair<string, int>> weightedTags, TagCloudGenerationRules generationRules) { #region Parameter validation if (weightedTags == null) throw new ArgumentNullException("weightedTags"); if (generationRules == null) throw new ArgumentNullException("generationRules"); #endregion this.Tags = CreateTags(weightedTags, generationRules); }
/// <summary> /// Creates a tag cloud from a provided list of string tags. /// The more times a tag occures in the list, the larger weight it will get in the tag cloud. /// </summary> /// <param name="tags">A string list of tags</param> /// <param name="generationRules">A TagCloudGenerationRules object to decide how the cloud is generated.</param> public TagCloud(IEnumerable<string> tags, TagCloudGenerationRules generationRules) { #region Parameter validation if (tags == null) throw new ArgumentNullException("tags"); if (generationRules == null) throw new ArgumentNullException("generationRules"); #endregion this.Tags = CreateTags(tags, generationRules); }