Forum: Ruby on Rails How to display GDchart image

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
55742d912a9003080d202c5522d4de00?d=identicon&s=25 rails (Guest)
on 2005-12-10 20:17
(Received via mailing list)
I'm working on a project right now that is migrating a site to RoR
(originally written in Ruby... but not as easy to add features, hence
the
migration).  The one sticking point is a 3d GDchart graph made using the
gdchart binding for ruby
<http://www.cozmixng.org/repos/ruby/gdchart/trunk/>

The problem is that GDChart wants to output the file to a stream (a
file,
STDOUT, etc..).  At the moment I have the image being saved to a file,
then using send_file.  I would like to find a way to send the image
without saving to a tempfile first.

<code>
     graph = GDChart::Graph.new
...
     graph.draw(f)
     send_file f.path, :type => "image/png", :disposition => "inline"
</code>

Any solutions on how to avoid using the file?  I'm probably missing
something obvious here.  Of course I'm open to other solutions that can
create nice looking 3d charts and graphs.
91308e9bc88cb069fd1bcf88e910d042?d=identicon&s=25 Nick Snels (nicksnels)
on 2005-12-10 22:01
Hi,

you might want to have a look at send_data, this sends a data stream to
the client. The full description is at
http://api.rubyonrails.org/classes/ActionControlle... .

Kind regards,

Nick

rails wrote:
> I'm working on a project right now that is migrating a site to RoR
> (originally written in Ruby... but not as easy to add features, hence
> the
> migration).  The one sticking point is a 3d GDchart graph made using the
> gdchart binding for ruby
> <http://www.cozmixng.org/repos/ruby/gdchart/trunk/>
>
> The problem is that GDChart wants to output the file to a stream (a
> file,
> STDOUT, etc..).  At the moment I have the image being saved to a file,
> then using send_file.  I would like to find a way to send the image
> without saving to a tempfile first.
>
> <code>
>      graph = GDChart::Graph.new
> ...
>      graph.draw(f)
>      send_file f.path, :type => "image/png", :disposition => "inline"
> </code>
>
> Any solutions on how to avoid using the file?  I'm probably missing
> something obvious here.  Of course I'm open to other solutions that can
> create nice looking 3d charts and graphs.
55742d912a9003080d202c5522d4de00?d=identicon&s=25 rails (Guest)
on 2005-12-10 23:36
(Received via mailing list)
I originally wanted to do this, but I couldn't figure out how to make it
work.  Honestly, I think I'm just missing something obvious.  I know how
to make GDChart output to a file or to STDOUT or STDERR, but I can't
figure out how to pipe this together with send_data to send the image.
1738dc801b47a9191702b57447f3392c?d=identicon&s=25 Richard Hull (Guest)
on 2005-12-24 01:43
(Received via mailing list)
> I think I'm just missing something obvious.  I know how
> to make GDChart output to a file or to STDOUT or STDERR, but I can't
> figure out how to pipe this together with send_data to send the image.

>From distant (perl w/GDChart) memory, try using graph.out(width, height, output,
type, data) instead...

It would be nice to use something along the lines:

  send_data(:type => 'img/png', :disposition => 'inline') do |output|
    graph.out(w, h, output, type, data)
  end

but I haven't got a rig setup to test this out right now, but not sure
if
send_data supports blocks.
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 Sky Yin (Guest)
on 2005-12-24 04:08
(Received via mailing list)
You can create a separate controller method with send_data at the end,
then in your rhtml just quote the URL of that method as the source of
a image tag.
D4ad9b4fab4ef4503f4ae1f55a85308f?d=identicon&s=25 Rob (Guest)
on 2006-01-04 06:31
(Received via mailing list)
In the controller for this action/view, create a new action, call it
GraphGen,
for instance.  Let's assume that the function call to render your graph
as a
binary blob is called "to_blob".  Then just do:

<code>
...
def GraphGen
      graph = GDChart::Graph.new
      ...
      send_data graph.to_blob, :type => "image/png", :disposition =>
"inline",
                                              :filename => "graph.png"
end
</code>

Keep in mind that specifying a filename here doesn't actually save it
locally.
It simply suggests what the client should call it.

Then from your view, simply do something like this:

<code>
...
<img src="<%= url_for :controller => 'CONTROLLER_NAME',
				     :action => 'GraphGen'%>">
</code>

Best,
Rob
This topic is locked and can not be replied to.