Topic: Inline Gruff

I'm using gruff to generate some graphs (duh!) and I can't find any documentation to tell me how to include a graph in a view/partial.  The current method in my controller ends with the send_data method which sends my graph to the browser just fine.  How do I get that pretty graph into my templates so I can make it dynamic?

def show
    g = Gruff::Pie.new(400)
    g.theme = {
      :colors => ['#2a90c3', '#aaaaaa'],
      :marker_color => '#103262',
      :background_colors => ['white', '#ffffff']
    }
    g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT)

    g.title = "Effort / Estimated"
   
    g.data("Effort", [9])
    g.data("Estimated", [81])

    g.scale_measurements()
    send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png")
end


PS.  For those experienced gruffers, is there a better way to scale a graph, or at least change the font size than passing a size to the constructor?

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: Inline Gruff

Well, the quickest way to get an object from a controller to a view is to make it an instance variable.  If you use:
[code lang=ruby]
  @g = Gruff::Pie.new(400)
[/code]
Then in your view you'll have the @g available to you.  If your intention is to make this graph persistent somehow then you can also save it to a file via @g.write('filename')

Re: Inline Gruff

I have no experience with Gruff, but if you have a URL that outputs an image, then just have a separate view that contains an img HTML tag that references the graph just like you would reference to any image file. Like this:

<img src="/my_graph_controller/show" alt="My Graph">

Does that make sense?

As for scaling, I'm not sure. I know Gruff uses rmagick so perhaps you could use one of those calls to scale the image if Gruff does not support it directly.

Railscasts - Free Ruby on Rails Screencasts

Re: Inline Gruff

# To make caching easier, add a line like this to config/routes.rb:
  # map.graph "graph/:action/:id/image.png", :controller => "graph"
  #
  # Then reference it with the named route:
  #   image_tag graph_url(:action => 'show', :id => 42)

Re: Inline Gruff

Thanks for you help.  The "graph_url" method doesn't seem to exists though.  But using the hard coded img tag will work for now.  I haven't explored images scaling enough to give an answer for that yet.

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)