From: Alex Fenton Date: 2008-04-01T01:40:04+09:00 Subject: Re: Displaying gruff graphs Victor Reyes wrote: > Once a graph is written to disk, g.write("filename.png"), how can it be > displayed in Ruby? > I would like to re-create the graph and re-displayed often enough to give > the appearance of "real time" It's not clear whether you meant via a browser or in a desktop app. If you want to show it on the desktop, you'll need some sort of GUI toolkit to display a window then draw the bitmap in it. You have a lot of choices: wxRuby, FXRuby, QT/Ruby, Tk, Ruby/GTK etc etc. They vary widely in suitability for different platforms, ease of installation, features, aesthetics. I use wxRuby b/c it's easy to install (gem install wxruby) and looks good on all the big platforms. A simple 25-line example that would regularly redraw an updated graph is below: hth alex __ require 'wx' require 'tempfile' # A frame that draws a regularly updated graph from Gruff every X millisecs class GruffFrame < Wx::Frame def initialize(refresh_delay = 5000) super(nil, :title => 'Gruff') Wx::Timer.every(refresh_delay) { refresh } evt_paint :on_paint end # Writes the gruff graph to a file then reads it back to draw it def on_paint # Set up your graph here graph = Gruff::Lines.new # .... # Write to a tempfile and read back tmp = Tempfile.new('gruff') graph.write(tmp.path) bmp = Wx::Bitmap.new(tmp.path, Wx::BITMAP_TYPE_PNG) # Draw it on the Frame paint do | dc | dc.draw_bitmap(bmp, 0, 0, false) end end end Wx::App.run { GruffFrame.new.show }