Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Tag Cloud in Ruby

The options.delete with the default looks mildly interesting. Just wanted to remember that.

def font_size_for_tag_cloud( total, lowest, highest, options={} )
 return nil if total.nil? or highest.nil? or lowest.nil?
 #
 # options
 maxf = options.delete( :max_font_size ) || 14
 minf = options.delete( :min_font_size ) || 11
 maxc = options.delete( :max_color ) || [ 0, 0, 0 ]
 minc = options.delete( :min_color ) || [ 156, 156, 156 ]
 hide_sizes = options.delete( :hide_sizes )
 hide_colours = options.delete( :hide_colours )
 #
 # function to work out rgb values
 def rgb_color( a, b, i, x)
  return nil if i <= 1 or x <= 1
  if a > b
   a-(Math.log(i)*(a-b)/Math.log(x)).floor
  else
   (Math.log(i)*(b-a)/Math.log(x)+a).floor
  end
 end
 #
 # work out colours
 c = []
 (0..2).each { |i| c << rgb_color( minc[i], maxc[i], total, highest ) || nil }
 colors = c.compact.empty? ? minc.join(',') : c.join(',')
 #
 # work out the font size
 spread = highest.to_f - lowest.to_f
 spread = 1.to_f if spread <= 0
 fontspread = maxf.to_f - minf.to_f
 fontstep = spread / fontspread
 size = ( minf + ( total.to_f / fontstep ) ).to_i
 size = maxf if size > maxf
 #
 # display the results
 size_txt = "font-size:#{ size.to_s }px;" unless hide_sizes
 color_txt = "color:rgb(#{ colors });" unless hide_colours
 return [ size_txt, color_txt ].join
end

Tag cloud using Text()


from appuifw import *
app.body = t = Text()

fonts = [u'', u'LatinPlain12', u'LatinBold12', u'LatinBold13', u'LatinBold17', u'LatinBold19']
tags = [(1, "apache"), (1, "array"), (1, "bash"), (1, "c"), (1, "cli"), (1, "csharp"), (1, "css"),
  (1, "database"), (1, "date"), (1, "expressionengine"), (1, "file"), (2, "html"), (1, "image"),
  (2, "java"), (4, "javascript"), (1, "lighttpd"), (1, "linux"), (1, "mac"), (1, "math"), (1, "mysql"),
  (1, "osx"), (1, "perl"), (3, "php"), (1, "pys60"), (5, "python"), (4, "rails"), (1, "regex"),
 (4, "ruby"), (1, "rubyonrails"), (1, "series"), (3, "series60"), (1, "servers"), (2, "shell"), (2, "sql"),
 (1, "ssh"), (1, "string"), (1, "text"), (1, "time"), (1, "unix"), (1, "web"), (1, "windows"), (1, "xml"),]

for level, tag in tags:
    t.font = fonts[level]
    t.add(tag + u' ')

See screenshot.

If using font is not enough, we can use other styles or colors.
See a related snippet.
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS