mhearse has asked for the wisdom of the Perl Monks concerning the following question:

What is the best way to have multiple GD::Graphs on one web page? I found an earlier node, but it seemed to indicate it wasn't possible. Well, it did say that they all could be merged into one. Basically I want to dynamically generate 4 seperate graphs on the fly (whenever a link is clicked) and display them all on one web page. My current method is to generate each graph seperately, saving it to a http world writable directory (bad I know). Then include the images from within html. Is there a better way to do this? Thanks much.

Replies are listed 'Best First'.
Re: Multiple GD::graphs in one web page
by brian_d_foy (Abbot) on Apr 06, 2005 at 21:41 UTC

    If you are already creating them dynamically, you can turn that portion into a separate CGI script. Use that CGI script as the SRC value in the IMG tag.

    <img src="http://www.example.com/cgi-bin/make_graph.cgi?values" />

    Your CGI script has to return the right MIME type, such as "image/png" or something else.

    Once you put those things in IMG tags, you just use the tags in the HTML like any other IMG tag.

    --
    brian d foy <brian@stonehenge.com>
      Thanks, this worked. As you suggested, I used the CGI script itself as the image. Passing an argument of 1..4 in order to tell the script what image I wanted.
      <img src="/cgi-bin/trend.cgi?image=1"> <img src="/cgi-bin/trend.cgi?image=2"> <img src="/cgi-bin/trend.cgi?image=3"> <img src="/cgi-bin/trend.cgi?image=4">
      The CGI script, or course, accepted the param and returned the corresponding image.
Re: Multiple GD::graphs in one web page
by SciDude (Friar) on Apr 06, 2005 at 20:41 UTC

    For a recent customer service data project, I needed several GD::Graphs and simply duplicated my call to individual code for each graph:

    print "<img src=http://www.url.com/cgi/custsvcgr.pl>";
    Replicate the above link for each graph needed. Each graph can then be customized by code, for example:
    #!/usr/bin/perl -w use GD::Graph::bars; ...get data here... my $mygraph = GD::Graph::bars->new( 600, 350 ); $mygraph->set( dclrs => [qw(green pink blue cyan)] ); $mygraph->set( x_label => 'Section', y_label => 'Satisfaction', title => 'Customer Service By Question', bar_spacing => '5', ) or warn $mygraph->error; my $myimage = $mygraph->plot( \@graph )->png; print "Content-type: image/png\n\n"; print $myimage;

    I think you will find other examples similiar to this one with a super search.


    SciDude
    The first dog barks... all other dogs bark at the first dog.
Re: Multiple GD::graphs in one web page
by nikos (Scribe) on Apr 06, 2005 at 20:40 UTC
    You can combine them using GD into one image and display it. This way you do not need to create separate files. Your script will return one image only.