ScratchyPad Tag Clouds
The version I posted yesterday had a dodgy tag cloud implementation that I was experimenting with. I’ve reworked it this morning between other tasks and am much happier now. I imported some delicious bookmarks to populate the db a bit more and the result is this:
Much better!
Initially, the tag cloud was overpowered by one tag because I was using a linear weighting, but once I switched to using a logarithmic weighting, things looked a lot better.
This is how the tag cloud is rendered, using Markaby:
Ruby
-
# Render the tag cloud view
-
def tag_cloud(tags = nil)
-
@tags ||= tags
-
max = 0
-
@tags.each do |t|
-
max = t.taggings.length if t.taggings.length > max
-
end
-
-
div.cloud do
-
@tags.each do |t|
-
weight = Math.log10(t.taggings.length) / Math.log10(max.to_f)
-
_cloud_tag(t, weight)
-
end
-
end
-
end
-
-
# Generate tag HTML
-
def _tag(name, style = nil)
-
a.tag(name, :href => R(Tags, name), :title => "All entries tagged ‘#{name}’", :style => style)
-
end
-
-
# Generate the HTML for the tag in the cloud
-
def _cloud_tag(tag,weight)
-
_tag(tag.name,style_for_weight(weight))
-
self << " "
-
end
-
-
# Generate a CSS style string
-
def style_for_weight(weight)
-
n = 0xff/2 + (0xff/2*weight).to_i
-
"font-size:#{0.5 + weight}em;color:rgb(#{n},#{n},#{n})"
-
end
