Skip to content

Instantly share code, notes, and snippets.

@pepetox
Created December 6, 2014 10:44
Show Gist options
  • Save pepetox/ae7bc6b4a1cf8e08715e to your computer and use it in GitHub Desktop.
Save pepetox/ae7bc6b4a1cf8e08715e to your computer and use it in GitHub Desktop.
Rails chart in the views
gem ‘gruff’
gem ‘rmagick’
<%= image_tag url_for(:controller => “alarms”, :action => “show_image”, :id => @alarm.id) %>
def show_image
require ‘gruff’
@notifications = @alarm.notifications
@nombre = @alarm.name || “Mi alarma”
g = Gruff::Line.new
g.title = @nombre
g.labels = Hash.new()
# g.data = Hash.new()
misdatos = Array.new()
counter = 0
@ notifications.each do |notif|
g.labels[counter] = notif.fecha.strftime(“%d/%m/%Y”)
misdatos « notif.price
counter = counter + 1
end
g.data @nombre, misdatos
f = Tempfile.new([ ‘tmp_img’, ‘.png’ ])
@img2 = g.write(f.path)
@img = Magick::Image::from_blob(open(f.path).read)[0]
@img = @img.to_blob
send_data @img, :type => ‘image/png’, :disposition => ‘inline’
end
get ‘alarms/:id/show_image’ => ‘alarms#show_image’ , as: :show_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment