Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
The stupid question is the question not asked
 
PerlMonks  

GD::Graph::area - how to set the axis origin

by explodec14 (Novice)
on Jan 07, 2010 at 13:31 UTC ( #816099=perlquestion: print w/ replies, xml ) Need Help??
explodec14 has asked for the wisdom of the Perl Monks concerning the following question:

Hello to everyone - I intend to use the GD::Graph::area module for generating simple area graphs. Although i have searched the cpan and other resources, i still haven't figured out how to set the axis origin to zero so that the x-axis and the y-axis will start from zero both (from the same point). here is my code:
use GD::Graph::area; use GD::Graph::colour qw(hex2rgb); print "Content-type: image/png\n\n"; my @data = ( [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ); my @names = qw/sample21/; for my $graph (GD::Graph::area->new) { my $name = shift @names; #print STDERR "Processing $name\n"; $graph->set( title => 'An Area Graph', dclrs => ['green'], #accentclr=>'red', y_max_value => 10, x_max_value => 10, x_min_value=>0, y_min_value=>0, y_tick_number => 5, x_tick_number => 5, overwrite=>1, #ticks_length=>, #y_label_skip => 2, #x_label_skip => 0, x_tick_offset=>1, #y_plot_values=>1, #x_plot_values=>1, #correct_width=>1, #accent_treshold => 20, transparent => 1 ); #$graph->set_legend( 'one', 'two' ); my $im = $graph->plot(\@data); print $im->png; }
Does anyone have any idea how to do this ?

Comment on GD::Graph::area - how to set the axis origin
Download Code
Re: GD::Graph::area - how to set the axis origin
by BrowserUk (Pope) on Jan 07, 2010 at 14:39 UTC

    Hm. Your code works as is for me?


    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.
      yes it works like it looks for you. But you can see that the x-axis is shifted right and the origin is not a zero point

        As stefbv pointed out, because it is a generic graphing package where there will commonly be no relationship between the units displayed on the two axis, it makes sense to not have the Y-axis line up with the zero tick of the X-axis. To do so would be wrong for the majority of datasets.

        A possible solution to your requirement is to remember that underlying GD::Graph is GD, which means you can tweak the graph after you've plotted it with all the power of the latter.

        By adding the following four lines to your original code:

        ## Existing line my $im = $graph->plot(\@data); my $gd = $graph->gd; ## get a drawing handle ## blank out the new Y-axis position $gd->filledRectangle( 16,0, 48,300, $gd->transparent ); ## Copy the Y-axis to the new position $gd->copy( $gd, 35,0, 0,0, 15,300 ); ## Blank out the original $gd->filledRectangle( 0,0, 16,300, $gd->transparent );

        You get this.

        Alternatively, you might prefer to move the graph left to meet the axis and blank the residue.


        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.
Re: GD::Graph::area - how to set the axis origin
by stefbv (Chaplain) on Jan 07, 2010 at 17:56 UTC

    If you replace the first row of data as in the example 'sample21' from the module:

    ['1st','2nd','3rd','4th','5th','6th','7th', '8th', '9th', '10th'],

    you will see that it's nothing wrong with the graph, your expectation is wrong, the first value is 0, the second is 1 and so on. This is not a graph in the mathematical sense where you plot a function like y=f(x).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://816099]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-04-20 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls