Created
December 6, 2014 10:44
-
-
Save pepetox/ae7bc6b4a1cf8e08715e to your computer and use it in GitHub Desktop.
Rails chart in the views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem ‘gruff’ | |
gem ‘rmagick’ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= image_tag url_for(:controller => “alarms”, :action => “show_image”, :id => @alarm.id) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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