GD: Graph Data Array Issue

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default GD: Graph Data Array Issue

    Hi All.

    I'm relatively new to Perl, and have been somewhat successful using
    the GD::Graph and Graph3d modules.

    I'm using this procedure as a CGI app to generate graphs.
    I've got an issue where, I'm setting up the @data array and when my
    image renders in the browser, it has a width, and height, but still
    appears as a broken link.

    I've tried CARP and multiple other techniques to figure out what's
    happening. Obviously unsuccessful.

    I believe the issue is directly related to the @data array, and/or
    it's reference.

    I've attached a "scaled-down" snipped of what I'm working with, any
    advice is appreciated.

    Chad.

    <snip>

    use CGI ':standard';
    use GD::Graph::bars3d;

    # Create CGI Object
    my $q = new CGI;

    my $alpha = "abcdefghijklmnopqrstuvwxyz";
    my $ii = 0;
    my $paramname;

    # a sample url would be:
    # ?xlabels=this,that,theother&groups=joe,ed,frank&a= 1,2,3&b=4,5,6&c=7,8,9

    # get X labels
    my @labels = [ split (/\,/, $q->param ("xlabels")) ];

    # get data groups
    my @datagroups = split (/\,/, $q->param("groups"));

    # @data is supplied to the generator @plot-time
    my @data = ( @labels );

    # get data sets
    # data is on query string as: a=1,2,3&b=4,5,6
    my @datasets;
    foreach (@datagroups) {
    $paramname = substr $alpha, $ii, 1;
    push @datasets, [ split ( /\,/ , $q->param($paramname) ) ];
    $ii++;
    }
    # @datasets should have a structure like this now.
    # @datasets = ( [ 12,15,20 ]
    # ,[ 30,10,25 ]
    # ,[ 15,25,55 ]
    # );

    =DEBUGcomments
    need to transpose datasets into @data:
    @data = ( [ labels ]
    ,[ 12,30,15 ]
    ,[ 15,10,25 ]
    ,[ 15,25,55 ]
    );
    =cut

    foreach (@datasets) {
    # will have an array here

    # get each entry in the array
    # add/push it to an array on @data
    for ($i=0;$i<= $#{$_}; $i++) {
    push @{$data[$i+1]}, $_->[$i];
    carp @{$data[$i+1]};
    }
    }

    my $my_image = $my_graph->plot(\@data) or die $my_graph->error;

    print "Content-type: image/png\n\n";
    binmode ( STDOUT );
    print $my_image->png;

    </snip>
    Chad Thomson Guest

  2. Similar Questions and Discussions

    1. What is the easiest way to graph MySQL data?
      I'm guessing I'm not the only person in the world who wants to graph some data collected in a MySQL database. I have not found any great tutorials...
    2. Issue with a mailer and a data array
      Hi there, I hope someone can help me with this, I am fairly new to PHP and am struggling to get a simple order form to send a mail message form...
    3. SVG::Graph::Data::Datum and labels
      Hi I want to create a simple svg bar chart with labels for each bar, I can see that I can add a label via SVG::Graph::Data::Datum but i dont see...
    4. components for dynamically displaying data chart or graph in ASP
      Hello, I tried to find some components I can use to dynamically generate chart or graph from database in ASP. It seems to me that only some...
    5. Graph Data Sheet Won't Sort Properly
      The problems is that I am trying to create a graph on a Form with MMM YY as the X Axis. The data is based on a query of two queries with the final...
  3. #2

    Default Re: GD: Graph Data Array Issue

    On 4 Sep 2003 08:45:39 -0700,
    Chad Thomson <chad_thomson18@hotmail.com> wrote:
    > Hi All.
    >
    > I'm relatively new to Perl, and have been somewhat successful using
    > the GD::Graph and Graph3d modules.
    >
    > I'm using this procedure as a CGI app to generate graphs.
    > I've got an issue where, I'm setting up the @data array and when my
    > image renders in the browser, it has a width, and height, but still
    > appears as a broken link.
    Renders in the browser? How do you mean, and what is the URL to the
    script below you use?
    > I've tried CARP and multiple other techniques to figure out what's
    > happening. Obviously unsuccessful.
    Have you also checked the web server logs for error messages?
    > use CGI ':standard';
    > use GD::Graph::bars3d;
    >
    > # Create CGI Object
    > my $q = new CGI;
    >
    > my $alpha = "abcdefghijklmnopqrstuvwxyz";
    > my $ii = 0;
    > my $paramname;
    >
    > # a sample url would be:
    > # ?xlabels=this,that,theother&groups=joe,ed,frank&a= 1,2,3&b=4,5,6&c=7,8,9
    >
    > # get X labels
    > my @labels = [ split (/\,/, $q->param ("xlabels")) ];
    >
    > # get data groups
    > my @datagroups = split (/\,/, $q->param("groups"));
    >
    > # @data is supplied to the generator @plot-time
    > my @data = ( @labels );
    >
    > # get data sets
    > # data is on query string as: a=1,2,3&b=4,5,6
    > my @datasets;
    > foreach (@datagroups) {
    > $paramname = substr $alpha, $ii, 1;
    > push @datasets, [ split ( /\,/ , $q->param($paramname) ) ];
    > $ii++;
    > }
    It looks here like you're use $ii and $alpha to get an alphabetically
    incrementing letter, right? Perl's automagical increment can do this
    for you:

    my $paramname = "a";
    foreach my $dg (@datagroups)
    {
    push @datasets, [split /,/ , $q->param($paramname)];
    $paramname++;
    }
    > # @datasets should have a structure like this now.
    > # @datasets = ( [ 12,15,20 ]
    > # ,[ 30,10,25 ]
    > # ,[ 15,25,55 ]
    > # );
    And the number of labels should be the same as the number of array
    references in @datasets here, right?

    So, each column here represents a set of data that will become a line
    or bar group in the chart?
    >=DEBUGcomments
    > need to transpose datasets into @data:
    > @data = ( [ labels ]
    > ,[ 12,30,15 ]
    > ,[ 15,10,25 ]
    > ,[ 15,25,55 ]
    ^^
    20?
    > );
    >=cut
    [ I wish you hadn't chosen a three-by-three data set. It's easier to
    visualise this sort of stuff with non-square sets ]

    But, given this, it looks like for each @datagroup, you're reading in
    the values for the three data sets that at that point need to be
    appended to the sets. The GD::Graph::Data module that prepares data
    sets for GD::Graph has an "add_point" method for this.

    I'd probably do something like:

    use GD::Graph::Data;

    my $dataset = GD::Graph::Data->new();

    my $paramname = "a";
    foreach my $dg (@datagroups)
    {
    $dataset->add_point("dummy", split /,/ , $q->param($paramname));
    $paramname++;
    }
    for my $i (0 .. $#labels)
    {
    $dataset->set_x($i, $labels[$i]);
    }

    And then use $dataset instead of \@data. It's quite easy to lose track
    of how to work with those data structures.
    > my $my_image = $my_graph->plot(\@data) or die $my_graph->error;
    This probably generates an error. Check your web server log.

    Alternatively, if you use the fatalsToBrowser argument with CGI, you
    should see the error messages in your browser.
    > print "Content-type: image/png\n\n";
    Use the CGI methods for this, since you've loaded the module anyway.

    print $q->header(-type => "image/png");
    > binmode ( STDOUT );
    You should use the binmode as the first thing you do on STDOUT, before
    the above print even.

    Without really deconstructing what you did with that data set, I can't
    tell whether that is what is wrong. I'd advise you to get check the
    GD::Graph::Data module, which can help with this particular structure,
    and maybe use Data::Dumper to dump out the structure to your browser,
    so you can see what it looks like.

    Also, for testing, don't use square data sets. It's much easier to
    debug a non-square one.

    Martien
    --
    |
    Martien Verbruggen |
    Trading Post Australia | Can't say that it is, 'cause it ain't.
    |
    Martien Verbruggen Guest

  4. #3

    Default Re: Graph Data Array Issue

    "Chad Thomson" <chad_thomson18@hotmail.com> wrote in message
    news:ebe52c7.0309040745.9e49377@posting.google.com ...
    > Hi All.
    >
    > I'm relatively new to Perl, and have been somewhat successful using
    > the GD::Graph and Graph3d modules.
    >
    > I'm using this procedure as a CGI app to generate graphs.
    > I've got an issue where, I'm setting up the @data array and when my
    > image renders in the browser, it has a width, and height, but still
    > appears as a broken link.
    >
    > I've tried CARP and multiple other techniques to figure out what's
    > happening. Obviously unsuccessful.
    >
    > I believe the issue is directly related to the @data array, and/or
    > it's reference.
    >
    > I've attached a "scaled-down" snipped of what I'm working with, any
    > advice is appreciated.
    >
    > Chad.
    I do the same sort of thing & a superficial look at your code shows no
    obvious errors. CARP will not work as you are not generating HTML.

    I suggest opening a file (in / tmp if necessary) and writing debugging
    messages there. As you have a broken link you may not even be calling the
    program.
    You can try writing your png file to disk (as 'test.png') instead of stdout
    and try to load test.png in the browser.

    You might want to look at the HTML I have at [url]www.ipo-australia.com[/url] to
    generate graphs.

    gtoomey


    Gregory Toomey Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139