in reply to Issues graphing multiple data sets on the same graph in GD::Graph

I don't know how much control you have over the software on the server you have, but you might want to check out Gnuplot , which is a more full featured graphing program. Check out the demo galleries for the different versions.

GD is great, but limited in flexibility; but someone may find a solution for you.

As for myself, I always reach for a Tk canvas to do graphing, on which I can do all sorts of tricks and export it to a graphic file. However, this requires an X server running.... a real drawback on a cgi server.

Here is a way to use Arrays of Arrays to plot multiple data sets with GD

#!/usr/bin/perl use warnings; use strict; use GD; use GD::Graph::lines; my @data = ( [1, 2, 3, 4, 5, 6, 7, 8, 9,], [ 8, 8.25, 8.5, 8.35, 8.76, 8.21, 8.6, 8.8, 8.5], [ 57.4, 57.6, 57.8, 57.9, 57.4, 58.5, 57, 57.87, 58.34] ); my $my_graph = new GD::Graph::lines (600, 400); $my_graph->set( title => "My Graph", transparent => '0', bgclr => 'lgray', boxclr => 'white', fgclr => 'white', title => 'Some simple graph', x_label => 'X axis label', y1_label => 'Y1 axis label', y2_label => 'Y2 axis label', #boxclr => '#C0C0C0', two_axes => 1, ); $my_graph->set_legend("data set 1", "data set 2"); my $gd = $my_graph->plot(\@data) or die $my_graph->error; open(IMG, ">$0.png") or die $!; binmode IMG; print IMG $gd->png; close IMG;

I'm not really a human, but I play one on earth. Cogito ergo sum a bum
  • Comment on Re: Issues graphing multiple data sets on the same graph in GD::Graph
  • Download Code

Replies are listed 'Best First'.
Re^2: Issues graphing multiple data sets on the same graph in GD::Graph
by SoaponaRope (Novice) on Nov 12, 2007 at 15:25 UTC
    So far as I know, Gnuplot isn't an option. :(

    As for using the multiple arrays to graph the data sets: I knew about that format, but my problem is that I can't hard code anything. The first thing this program is doing is being distributed to a couple international sites, where the groups and tickets we're graphing varies. Thus, everything has to be dynamic.

    To further expound on what I'm trying to do (example code):

    $userexample .= @users;

    make that output

    @users = ( [userexample[0]], [userexample[1]], [userexample[2]], );
    So on and so forth. Creating the multi-dimensional arrays by hand is something I understand, and I know GD can graph them like that, I'm just trying to figure out how to graph them in a dynamic fashion.

    Thanks for the help. :)

      Well, I'm not sure exactly what your code has to do, but the Array of Array doesn't have to be static. You can push an array onto the AoA. Something like
      @users = ( [userexample[0]], [userexample[1]], [userexample[2]], ); @userexample[3] = (1,2,3,4); push @users, \@userexample[3]; # now # @users = ( # [userexample[0]], # [userexample[1]], # [userexample[2]], # [userexample[3]], # );
      So, you can store the current values, like in a db, push on new arrays, and rebuild the graph.

      Read "perldoc perldsc" for a better explanation of all the possible Perl data structures. There are all sorts of data structures.... HoH, AoA,HoA,AoH,...etc., etc, etc.


      I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        The example code you posted unfortunately returns a syntax error when I attempt to compile it. I've read the perldsc page, but the examples on there are still somewhat vague to me, and I don't see anything that seems to imply the syntax I'm looking for.

        Sorry if I seem anxious: I've been working on this project for three weeks now, and this is literally the last bug that needs to be ironed out before we roll it into production. (It's also the first real program I've ever written in any language) I'm ready for it to be on the way.

        Edit: The syntax error that was returned was the result of a typo error on my part. Unfortunately, it also doesn't seem to be doing what I'm trying to do. This operator seems to add the numbers together in each arry, the numbers resulting are impossibly high. :(