[Nitro] Tag cloud and calendar widgets?

Jonathan Buch john at oxyliquit.de
Tue Nov 28 17:40:41 EST 2006


Hi,

> What are Nitroites using for tag cloud and calendar widgets? Are there
> parts libraries, or is everyone rolling their own?
>
> I've posted the question on oxyliquit.de but am casting a wider net.
> I'll post any responses back on oxyliquit.

sorry for Oxyliquit being down, the service is moving to a (hopefully)
more stable server.
Thank you for posting here as well as over there.

To answer your first question:  there ain't really any nitro libs yet,
so everyone is rolling their own stuff.

But, basically a tag cloud is really easy to build.

Below working code from oxyliquit:

   <h2><a href="/tags">Tags</a></h2>
   <div class="cloud">
     <?r
       sum = @wtags.inject(0) { |sum, t| sum + t.popularity.to_i }
     ?>
     <?r @wtags.each do |t| ?>
       <a href="/tags/#{t.name}" style="font-size:#{(1+((t.popularity.to_i/sum.to_f)*3)).to_s[0..3]}em;">#{t.name}</a>
     <?r end ?>
   </div> <!-- #cloud -->

Not really pretty, but very neat.  It just uses the 'sum' as the highest
value and uses percentages of this to produce the cloud like look.

The tag.popularity here is a value which you have to calculate.  Just think
of something clever.  Oxyliquit for example uses clicks (page visits) to
weigh the Tags.

Again, working code from Oxywtf:

   def all_tags
     # This SQL query is used as a select statement for Tags
     # It first gets all sum'ed up clicks from questions/tips/tuts and adds
     # them together. This produces the popularity of a single Tag.
     sel = <<-SQL
       (SELECT SUM(clicks) FROM (
         SELECT SUM(q.clicks) AS clicks FROM ogquestion q, ogj_question_tag tq WHERE q.oid = tq.question_oid AND tq.tag_oid = ogtag.oid
         UNION
         SELECT SUM(q.clicks) AS clicks FROM ogtutorial q, ogj_tag_tutorial tq WHERE q.oid = tq.tutorial_oid AND tq.tag_oid = ogtag.oid
         UNION
         SELECT SUM(q.clicks) AS clicks FROM ogtip q, ogj_tag_tip tq WHERE q.oid = tq.tip_oid AND tq.tag_oid = ogtag.oid
         ) AS foo
       ) AS popularity, *
     SQL

     @all_tags ||= Tag.find(:select => sel, :order => 'popularity DESC')
   end

   def right
     @wtags = all_tags.sort_by { |x| x.popularity.to_i }.reverse[0..30].sort_by {|x| x.name }
   end

Not the most pretty code either, but very much does what I wanted.

Note, this is just an example, the popularity of a single tag
can be calculated of anything you like.  The easiest is to just use
the 'count' field of the tag.

As I said, it's quite easy to roll yourself, so making a real lib out
of that... I dunno.  :)

Feel free to use all code in here,

have a nice evening.

Jo

PS:  really really sorry for Oxy outages.  Got word from the server
support.  Apparently a defect made the server reboot with the main
disc mounted as readonly.  I just wonder how that can happen twice
in a row.  ;/
PPS:  working on a backup server, should be online soon.

-- 
Feel the love
http://pinkjuice.com/pics/ruby.png


More information about the Nitro-general mailing list