I'm playing with the code available here (GitHub repository here) to generate a heatmap/cloud.
I'm running into a lot of NoMethodErrors and other problems that prevent the page from even being rendered. The two or three times the page did render, the cloud didn't display properly. I would appreciate any and all help in getting this working so I can play with the code.
The error of the hour is:
undefined method `each_pair' for "Many":String
This is referencing my view:
<h1>Title</h1>
.
.
.
<% stylesheet_link_tag "custom" %>
<%= heatmap( {"Foo" => 13,
"Bar" => 15,
"Trouble" => 5,
"Braids" => 1,
"Something" => 9,
"Else" => 13,
"Many" => 20, <----
"Zombies" => 7,
"nothing" => 0}) %>
I'm dumping the test hash directly into the view because I was getting a:
undefined method `keys' for nil:NilClass
For completeness, this is my ApplicationHelper (where the heatmap method is defined):
module ApplicationHelper
def heatmap(histogram={})
content_tag(:div, :class => "heatmap")
histogram.keys.sort{|a,b| histogram[a] <=> histogram[b]}.reverse.each do |k|
next if histogram[k] < 1
_max = histogram_max(histogram) * 2
_size = element_size(histogram, k)
_heat = element_heat(histogram[k], _max)
content_tag(:span, content_tag( :class => "heatmap_element", :style => "color: ##{_heat}#{_heat}#{_heat}; font-size: #{_size}px;"), "#{k}")
end
content_tag(:br, :style => "clear: both;")
end
def histogram_max(histogram)
histogram.map{|k,v| histogram[k]}.max
end
def element_size(histogram, key)
(((histogram[key] / histogram.map{|k,v| histogram[k]}.sum.to_f) * 100) + 5).to_i
end
def element_heat(val, max)
sprintf("%02x" % (200 - ((200.0 / max) * val)))
end
end
I pulled the code out of the repository and used that in the helper instead. I have changed the code to use content_tags because the app was开发者_如何学JAVA escaping the HTML code and printing it on the site.
Heres how I implemented it...https://gist.github.com/3505320. Good luck!
精彩评论