# Integrate scruffy graph in rails view?

**URL:** <https://www.ruby-forum.com/t/integrate-scruffy-graph-in-rails-view/86054>\
**Category:** Rails\
**Created:** [February 19, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/integrate-scruffy-graph-in-rails-view/86054 "2007-02-19T14:01:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [February 19, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/integrate-scruffy-graph-in-rails-view/86054/1 "2007-02-19T14:01:42Z")

</div>

Hi everybody,

I’m using scruffy to create some graphs, and would like to integrate  
them into a regular view (i.e. show them in an ![]() tag).

However, I get errors when using img\_tag, or the image doesn’t load if  
I try saving the graph to disk and then loading them in html.  
Generating the graph and linking to it works, however then I only have  
the graph open in a new window, whereas I’d like to use the graph as  
an image inside an html page.

Any pointers on how to do this? I’ve searched around the ibrasten  
website, and scruffy itself works just fine, but I’ve been unable to  
locate any information on how to integrate scruffy into rails. Any  
links would be most appreciated.

Thanks,  
Elmar

---

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [February 20, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/integrate-scruffy-graph-in-rails-view/86054/2 "2007-02-20T15:01:39Z")

</div>

Hi Elmar,

I’m using scruffy successfully in my project, here is what I do. I  
have this in a helper of mine (you could put it in  
application\_helper.rb or wherever it’s fine for you)

def render\_scruffy\_graph(options = nil)  
opts = make\_options(options)  
graph = Scruffy::Graph.new

# add the Scruffy::Layers you would like to use

graph.add :bar, opts[:data]  
graph.point\_markers = opts[:markers]

graph.renderer = opts[:renderer]

graph.render(  
:size =\> options[:size],  
:min\_value =\> opts[:data].first, # you might not need this one  
:max\_value =\> opts[:data].last, # you might not need this one  
:theme =\> opts[:theme], # you might not need this one  
:as =\> opts[:img\_format],  
:to =\> opts[:filename]  
)  
end

private

# options specified here possibly

# override options passed in parameter

def make\_options(options = nil)  
opts = options ? {}.merge(options) : {}  
returning opts do |o|  
# add o[:foo] = bar statements as you wish (look @ the options  
that graph.render supports)  
# you can specify :renderer and :theme like so  
o[:renderer] = Scruffy::Renderers::YourRenderer.new  
o[:theme] = Scruffy::Themes::YourTheme.new  
end  
end

There are definitely more flexible ways for generating your options,  
but this did the trick for me …

Then in the view just call

\<%= render\_scruffy\_graph %\>

Hope this helps  
Martin Gamsjäger

---

<div class="post-metadata">

**Author:** ![Elmar\_Schraml](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/858c86/32.png) [@Elmar\_Schraml](https://www.ruby-forum.com/u/Elmar_Schraml)\
**Post date:** [February 20, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/integrate-scruffy-graph-in-rails-view/86054/3 "2007-02-20T15:05:40Z")

</div>

Oops forgot something,

Be sure not to use :as =\> “svg” if you want svg images. Scruffy’s  
default is to use svg anyway, and if I recall correctly, something  
went wrong when supplying :as =\> “svg”, :to =\> “blabla.svg”. Just use  
the :to option if you want to render svg images to files.

cheers  
Martin Gamsjäger
