module IRuby module Output module HTML require "gruff" require "base64" def self.table(data) # # data = {a: 1, b:2} if data.respond_to?(:keys) d = data else d = data end r = "" if d.respond_to?(:keys) # hash columns = [0,1] else columns = d.first.keys r << "#{columns.map{|c| ""}.join("\n")}" end d.each{|row| r << "" columns.each{|column| r << "" } r << "" } r << "
#{c}
#{row[column]}
" r end def self.image(image) data = image.respond_to?(:to_blob) ? image.to_blob : image "" end def self.chart_pie(o) data=o.delete(:data) title=o.delete(:title) size=o.delete(:size) || 300 g = Gruff::Pie.new(size) g.title = title if title data.each do |data| g.data(data[0], data[1]) end image(g.to_blob) end def self.chart_bar(o) data=o.delete(:data) title=o.delete(:title) size=o.delete(:size) || 300 klass = o.delete(:stacked) ? Gruff::StackedBar : Gruff::Bar g = klass.new(size) if labels=o.delete(:labels) if ! labels.respond_to?(:keys) labels = Hash[labels.map.with_index{|v,k| [k,v]}] end g.labels = labels end g.title = title if title data.each do |data| g.data(data[0], data[1]) end image(g.to_blob) end module Gmaps def self.points2latlng(points) "[" + points.reject{|p| not p.lat or not p.lon}.map{|p| " {location: new google.maps.LatLng(#{p.lat.to_f}, #{p.lon.to_f}) #{", weight: #{p.weight.to_i}" if p.respond_to?(:weight) and p.weight} } " }.join(',') + "]" end def self.heatmap(o) data = o.delete(:points) raise "Missing :points parameter" if not data points = points2latlng(data) zoom = o.delete(:zoom) center = o.delete(:center) map_type = o.delete(:map_type) r = < E STDERR.write("#{r}\n\n") r end end #stolen from https://github.com/Bantik/heatmap/blob/master/lib/heatmap.rb module WordCloud def self.wordcloud(histogram={}) html = %{
} 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) html << %{ #{k} } end html << %{
} end def self.histogram_max(histogram) histogram.map{|k,v| histogram[k]}.max end def self.element_size(histogram, key) (((histogram[key] / histogram.map{|k,v| histogram[k]}.reduce(&:+).to_f) * 100) + 5).to_i end def self.element_heat(val, max) sprintf("%02x" % (200 - ((200.0 / max) * val))) end end end end end