# How to display GDchart image

**URL:** <https://www.ruby-forum.com/t/how-to-display-gdchart-image/97113>\
**Category:** Rails\
**Created:** [December 10, 2005, 8:17pm UTC](https://www.ruby-forum.com/t/how-to-display-gdchart-image/97113 "2005-12-10T20:17:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rails](https://www.ruby-forum.com/u/rails)\
**Post date:** [December 10, 2005, 8:17pm UTC](https://www.ruby-forum.com/t/how-to-display-gdchart-image/97113/1 "2005-12-10T20:17:13Z")

</div>

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/](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.

---

<div class="post-metadata">

**Author:** ![rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rails](https://www.ruby-forum.com/u/rails)\
**Post date:** [December 10, 2005, 10:01pm UTC](https://www.ruby-forum.com/t/how-to-display-gdchart-image/97113/2 "2005-12-10T22:01:30Z")

</div>

Hi,

you might want to have a look at send\_data, this sends a data stream to  
the client. The full description is at  
[ActionController::Streaming](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/](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.

---

<div class="post-metadata">

**Author:** ![rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rails](https://www.ruby-forum.com/u/rails)\
**Post date:** [December 10, 2005, 11:36pm UTC](https://www.ruby-forum.com/t/how-to-display-gdchart-image/97113/3 "2005-12-10T23:36:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rails](https://www.ruby-forum.com/u/rails)\
**Post date:** [December 24, 2005, 1:43am UTC](https://www.ruby-forum.com/t/how-to-display-gdchart-image/97113/4 "2005-12-24T01:43:43Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rails](https://www.ruby-forum.com/u/rails)\
**Post date:** [January 4, 2006, 6:31am UTC](https://www.ruby-forum.com/t/how-to-display-gdchart-image/97113/5 "2006-01-04T06:31:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5f9b8f/32.png) [@rails](https://www.ruby-forum.com/u/rails)\
**Post date:** [December 24, 2005, 4:08am UTC](https://www.ruby-forum.com/t/how-to-display-gdchart-image/97113/6 "2005-12-24T04:08:01Z")

</div>

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.
