How to display GDchart image


#1

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.

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

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.


#2

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/ActionController/Streaming.html .

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.

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

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.


#3

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.


#4

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.


#5

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:

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

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:

...

Best,
Rob


#6

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.