Saturday, March 24, 2012

TAG Cloud

Hi there,

I have searched everywhere for code that would allow me to create a Tag Cloud using ASP.Net 1.1 and VBScript. All I can find are ones for PHP which leaves me with no clue.

Does anyone know/have an example on how to create one?

Thanks in advance,

TCM_

what the heck is a "TAG Cloud"?
Tag Cloud: http://en.wikipedia.org/wiki/Tag_cloud
so its a list of used tags?
A list of tags where size reflects popularity

should'nt be too hard...

i assume you would have say a table called "tags" with columns like "tagName", "tagCount", where the count would be how many times its been "used" (ie, searched or added)

then just do a sql query and order by the "tagCount", then the highest "tags" would be in the largest font, etc...

how will your tags be stored? and what are the tags used for etc? more info and i may be able to rustle up some code for you...

regards.


Hi RiCo83,

Thank you all for your comments.

To explain a little more I have a table that has many columns, but the ones of interest are Category and Count. The count column counts how many times a record has been viewed. I would like to create a TagCloud that would 'weight' the Category column with the Category (i.e. Fashion) weighted on the number of times it has been viewed. Maybe 5 different weight types? So it would look through the table at the Category column (there will be multiple records with the same Category), so we would want to somehow add up all the count for Fashion etc. and weight on the most popular?

Hopefully this makes sense. Really unsure on how to acheive this through code?

Any thoughts would be greatly appreciated.

Regards,

TCM_


take a look at the "Select Count(field)..." sql script option


Hi there,

Thanks for the suggestion. I am able to use the Count() method, however I am unsure of how to use this for multiple categories and displaying information in a Tag Cloud type effect with different font weightings etc.

Any thoughts?

Thanks again,

TCM_


Hi there, I ran across this post while trying to figure out how to build a tag cloud. I ended up whacking together a little solution that I think works well. The hard part about building one is weighting the tags. To size each tag I wanted to answer the question, "how far off average is the count of this tag?" To answer this you can calculate thestandard deviation for the set of tag counts and figure out how many standard deviations away from average each tag count is.

I also wanted a maximum tag size and a minimum tag size in the cloud (so everything stays reasonable). Since, acording to some math theory, 96% of the tag counts are only 2 standard deviations away from the average (check out that bell curve on the standard deviation page), I created a line with the based on two points: (-2s, minSize) and (2s, maxSize) where s is the standard deviation. Once we have the function for that line, we can just give it the difference between our tag count and the average tag count, and it will give us the proper size. Anything more than 2s away from average is sized to either the min size or the max size. I used pixels for size, but you could use anything I suppose. The sizes in this solution are integers though.

Here's the code. It's two classes: Tag and Tags. Tags is a list of Tag objects. To use them, create Tag objects for each row in your result set (you need the tagName and the tagCount) and add them to the Tags list. Then call SizeTags() on the list. Then you can use the Tags as the datasource in a repeater on your ASP.NET page.

Public Class Tag
Private _tagName As String
Private _tagCount As Integer
Private _tagSize As Integer

Public Sub New(ByVal tagName As String, ByVal tagCount As Integer)
_tagName = tagName
_tagCount = tagCount
End Sub

Public ReadOnly Property TagName() As String
Get
Return _tagName
End Get
End Property

Public ReadOnly Property TagCount() As Integer
Get
Return _tagCount
End Get
End Property

Public ReadOnly Property TagSize() As String
Get
Return _tagSize
End Get
End Property

Friend Sub SetTagSize(ByVal size As Integer)
_tagSize = size
End Sub

End Class

Public Class Tags
Inherits Generic.List(Of Tag)

' Sizes all the tags based on number
' minSize: The smallest you want the tags to get
' maxSize: The largest you want the tags to get
Public Sub SizeTags(ByVal minSize As Integer, ByVal maxSize As Integer)
Dim avg = AverageTagCount()
Dim s As Double = StandardDeviation(avg)

'get the slope of our line
Dim m As Double = (maxSize - minSize) / (4 * s)

'get the y-intercept
Dim b As Double = (maxSize - minSize) / 2

'now for any given deviation from average we can generate the tag size

For Each t As Tag In Me
'x is the distance from average for this tagCount
Dim x As Double = t.TagCount - avg
t.SetTagSize(CInt(m * x + b))

'Just in case the tag turned out too small
If t.TagSize < minSize Then
t.SetTagSize(minSize)
End If

'And just in case the tag turned out too big
If t.TagSize > maxSize Then
t.SetTagSize(maxSize)
End If
Next

End Sub

' Calculates the standard deviation of the tagCount of these tags
'avgerage: accepts the average so we don't have to recalculate it when needed along with the standard deviation
Private Function StandardDeviation(ByVal avgerage As Double) As Double

Dim sum As Double = 0
For Each t As Tag In Me
sum += (t.TagCount - avgerage) ^ 2
Next

Return ((1 / Me.Count) * sum) ^ 0.5
End Function

Public Function AverageTagCount() As Double
Dim sum As Integer = 0
For Each t As Tag In Me
sum += t.TagCount
Next

Return sum / Me.Count
End Function

End Class

So in asp.net 1.1, create a project with those two classes, add that project to your solution that already has your asp.net project, then reference the "TagCloud" project in your asp.net project.

Then on your page, you can add a repeater like:


<asp:Repeater ID="TagsRepeater" runat="server">
<ItemTemplate>
<a class='tag' style='font-size: <%# Eval("TagSize") %>px' href='Stuff.aspx?tag=<%# Eval("TagName") %>'><%# Eval("TagName") %></a>
</ItemTemplate>
</asp:Repeater
And in the page load on the code behind set its datasource and databind it:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim t As New Tags

With t
.Add(New Tag("52", 52))
.Add(New Tag("13", 13))
.Add(New Tag("12", 12))
.Add(New Tag("2", 2))
.Add(New Tag("3", 3))
.Add(New Tag("4", 4))
.Add(New Tag("1", 1))
.Add(New Tag("67", 67))
.Add(New Tag("67b", 67))
.Add(New Tag("120", 120))
End With

t.SizeTags(10, 60)

TagsRepeater.DataSource = t
TagsRepeater.DataBind()

End Sub

Good luck!
Oh I just had a thought, I think in ASP.NET 1.1 you'll have to use Databinder.Eval() in your aspx page rather than just Eval() ...I can't remember.
Another thing with ASP.NET 1.1 is that you can't use Generics. So you'll have to change around that Tags class and turn it into a custom collection. Sorry, I wrote this in 2.0.
Try this solution :http://www.codeproject.com/useritems/cloud.asp

Hi guys,

Thanks so much for all the help and guidance...I am still struggling to get through the code and the link provided only works in .Net 2.0. If any one has any other ideas / links / controls for VB and .Net 1.1 it would be greatly appreciated.

Will let you know how I get on.

Regards,

TCM_


Look at this:http://www.toysforthebrain.com/TagCloudTest.zip

0 comments:

Post a Comment