Skip to content

Instantly share code, notes, and snippets.

@danielharan
Created November 13, 2008 03:34
Show Gist options
  • Save danielharan/24352 to your computer and use it in GitHub Desktop.
Save danielharan/24352 to your computer and use it in GitHub Desktop.
# Example use:
# weigh_by_relative_importance(@report.items, :cell_value, [2,4,6,8,10]) do |item,weight|
# xml.movie :lat => item.latitude, :long => item.longitude, :title => item.city,
# :height => weight, :width => weight
# end
# Code inspired by http://www.juixe.com/techknow/index.php/2006/07/15/acts-as-taggable-tag-cloud/
# and meant to be usable for items on a map (as above) or tags
module MapHelper
def weigh_by_relative_importance(items, method_name, classes)
sorted = items.collect {|item| item.send(method_name)}.sort
min, max = sorted.first, sorted.last
divisor = ((max - min) / classes.size) + 1
items.each do |item|
yield item, classes[(item.send(method_name) - min) / divisor]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment