Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Niklas Hjelm 104 posts 125 karma points
    Jan 07, 2012 @ 15:31
    Niklas Hjelm
    0

    Tag cloud in razor

    Hi!

    Earlier I got some help with tagging my pages with razor but now I could use some help with creating a Tag cloud. Has anyone done a Tag cloud with razor?

    Thanks

    / Niklas

  • Gareth Evans 143 posts 335 karma points c-trib
    Jan 12, 2012 @ 23:04
    Gareth Evans
    0

    Hi Niklas

    I would probably write a class library with a static method and drop it in the bin folder, use that to generate your tag cloud data.

    Don't forget to add caching, the tag methods hit the database.

    You'd want to call into the Tag class and use one of the static methods to get your data, then aggregate that by using statistical functions to "snap" the font size.
    There are many common algorithms for that.

    Once you've got your list of "this tag, this size" return it as a IEnumerable<TagCloudItem> (where TagCloudItem is your own class) then you can just do:

    @using Niklas.TagCloud

    @foreach(var cloudItem in TagLibrary.Get())
    {
    <span style="font-size:@(cloudItem.Size)px">@cloudItem.Tag</span>
    }

    where Niklas.TagCloud is your namespace, TagLibrary is your class name and Get is your static method returning the IEnumerable<TagCloudItem> 
    In this example, TagCloudItem has two properties, Size and Tag

    Hope this helps.

    Gareth

Please Sign in or register to post replies

Write your reply to:

Draft