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

I am wondering whether I can add two arrays into GD Graphic bars such like
@data1 = ( ["2010/03/29 14:00:00","2010/03/29 14:00:00"], [13,15] ); @data2 = ( ["2010/03/29 14:00:00","2010/03/29 14:00:00"], [20,21] );
How can I use the plot function to add this?

Replies are listed 'Best First'.
Re: How to add two array data into one GD plot?
by BrowserUk (Patriarch) on Jul 29, 2012 at 04:08 UTC

    As the labels remain the same for the two datasets, just add the second (and subsequent) set(s) of values to the first array:

    #! perl -slw use strict; use GD::Graph::bars; my @d1 = ( [ "2010/03/29 14:00:00", "2010/03/29 14:00:00" ], [ 13, 15 ], [ 20, 21 ], [ 4, 9 ], ); my $graph = GD::Graph::bars->new(1000, 800); open PNG, '>:raw', "$0.png" or die $!; print PNG $graph->plot( \@d1 )->png; close PNG; system "$0.png";

    Produces:this graph.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Hi BrowersUk,

      Actually, I have made a mistake, could you help me again, I mean date might be different. As I would like gather these two data plots into one bars.

      @data1 = ( ["2010/03/29 14:00:00","2010/03/29 14:00:00"], [13,15] ); @data2 = ( ["2010/04/29 14:00:00","2010/04/29 14:00:00"], [20,21] );

        Hm. You could just combine the two datasets into a single dataset. But that doesn't produce a very inspiring graph:

        #! perl -slw use strict; use GD::Graph::bars; my @d1 = ( [ "2010/03/29 14:00:00", "2010/03/29 14:00:00", "2010/04/29 14:00: +00", "2010/04/29 14:00:00" ], [ 13, 15, 20, 21 ], ); my $g1 = GD::Graph::bars->new( 1000, 800 ); my $img1 = $g1->plot( \@d1 ); open PNG, '>:raw', "$0.png" or die $!; print PNG $img1->png; close PNG; system "$0.png";

        or maybe you need to retain two datasets and just combine the labels, to give this:

        #! perl -slw use strict; use GD::Graph::bars; my @d1 = ( [ "2010/03/29 14:00:00", "2010/04/29 14:00:00" ], [ 13, 15 ], [ 20, 21 ], ); my $g1 = GD::Graph::bars->new( 1000, 800 ); my $img1 = $g1->plot( \@d1 ); open PNG, '>:raw', "$0.png" or die $!; print PNG $img1->png; close PNG; system "$0.png";

        .

        Or maybe I've combined the values the wrong way and what you want is this

        #! perl -slw use strict; use GD::Graph::bars; my @d1 = ( [ "2010/03/29 14:00:00", "2010/04/29 14:00:00" ], [ 13, 20 ], [ 15, 21 ], ); my $g1 = GD::Graph::bars->new( 1000, 800 ); my $img1 = $g1->plot( \@d1 ); open PNG, '>:raw', "$0.png" or die $!; print PNG $img1->png; close PNG; system "$0.png";

        And maybe you want a gap between the two this

        #! perl -slw use strict; use GD::Graph::bars; my @d1 = ( [ "2010/03/29 14:00:00", undef, "2010/04/29 14:00:00" ], [ 13, undef, 20 ], [ 15, undef, 21 ], ); my $g1 = GD::Graph::bars->new( 1000, 800 ); my $img1 = $g1->plot( \@d1 ); open PNG, '>:raw', "$0.png" or die $!; print PNG $img1->png; close PNG; system "$0.png";

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: How to add two array data into one GD plot?
by zwon (Abbot) on Jul 29, 2012 at 04:05 UTC

    You have four values all for the same moment of time. Here are two possible solutions. First, if you want them as two different pairs you should join them like this:

    @data = ( ["2010/03/29 14:00:00", "2010/03/29 14:00:00"], [13, 15], [20, 21], );
    Second, if they are really for the same time, maybe they should be like this:
    @data = (["2010/03/29 14:00:00"], [13], [15], [20], [21],);
Re: How to add two array data into one GD plot?
by dasgar (Priest) on Jul 29, 2012 at 22:07 UTC

    It sounds like others might have given you enough help for you to figure out what you're wanting to do.

    In case you're interested, I recently found a web site that had some examples of different kinds of graphs you can do with GD::Graph. The site is at http://gdgraph.com/.

    On one hand, they provide code for their sample charts. On the other hand, their code is calling in another Perl script and calling subroutines from that script. Unfortunately, that script is not provided. (At least, I couldn't find it.) Basically, all that script is actually writing out the chart to a file. If you look at the documentation for GD, you should be able to figure out the syntax needed to finish the scripts to save the charts as picture files of your desired format (jpg, png, etc.).

    At times, I personally find it easier to look at example code rather than reading through documentation when learning a new module. I personally found the sample code offered on this site to be useful to me, so I thought I'd share this information in case it helps others.