Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

GD::Graph and Multiple Y Axes

by ChrisR (Hermit)
on Sep 26, 2010 at 16:28 UTC ( [id://862090]=perlquestion: print w/replies, xml ) Need Help??

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

I have been trying to make some graphs using GD::Graph::lines. I have run into a snag when trying to use multiple Y axes. I can configure the 1st axis with no problem at all. My problem is configuring the ticks for the second axis.

I have tried using attributes like y1_tick_number, y2_tick_number, etc. but get the error No attribute 'y2_tick_number'

I have tried specifying an array such as y_tick_number  => [3,30] but the second element seems to get ignored.

I have searched the documentation and the web but can't seem to find an answer. The idea is to provide a different set of ticks for the second data set. I would like to have 15 ticks numbered 10, 20, 30, etc. on the right side of the graph. Any help will be greatly appreciated.

Here is a short example of my test code:
#!/usr/bin/perl use strict; use GD::Graph::lines; my @data = ( ["Mar","Apr","May","Jun","Jul"], [5,2,10,6,4], [100,80,50,60,20] ); my $graph = GD::Graph::lines->new(800, 600); $graph->set( title => 'Test Graph', two_axes => 'true', use_axis => [1,2], x_label => 'Month', y1_label => 'Units', y1_max_value => 15, y1_min_value => 0, y_tick_number => [15,15], #second element get ignored y_label_skip => 0, y2_label => 'Profit', y2_max_value => 150, y2_min_value => 0, #y2_tick_number => 15, #throws error #y2_label_skip => 0 #throws error ) or die $graph->error; my $gd = $graph->plot(\@data) or die $graph->error; open(IMG, '>/var/www/html/file.gif') or die $!; binmode IMG; print IMG $gd->gif; close IMG; exit;
Chris Rogers

Replies are listed 'Best First'.
Re: GD::Graph and Multiple Y Axes
by zentara (Archbishop) on Sep 26, 2010 at 17:05 UTC
    Multiple y axis ticks it is not possible, and it has been asked here before, IIRC. Google for " GD graph multiple y axis".

    Usually, the advice is to go with a more powerful graph generation program, like GnuPlot or PGPlot.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks. I will look into both. Is there a general preference among the Monks?
      Chris Rogers
        As an after thought, considering the hubris and ingenuity of perl programmers, there may be a way to hack a GD solution, by stitching together 2 GD images. The first, would be your normal graph, and the second would be just an custom image containing your second y axis, which you would overlay and stitch to the right side of the actual graph.

        I would look at the demo pages for each program, like gnuplot demo page and see if you find a model you like, especially on gnuplot axis tic demo

        It looks like they call the second independent y-axis a "z-range".


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: GD::Graph and Multiple Y Axes
by Proclus (Beadle) on Sep 27, 2010 at 13:02 UTC
    I find myself hacking GD::Graph to accomplish my needs. It's not that difficult once you figure out which package and which function are taking care of the drawing tasks.

    In my case, first I found the loop data array is processed. And then I observed how the drawing methods are used with the data points. Sorry, I do not remember the package and the function names.
Re: GD::Graph and Multiple Y Axes
by Anonymous Monk on Dec 28, 2010 at 19:55 UTC
    Replace below line # two_axes => 'true', with two_axes => 1, then problem solved
Re: GD::Graph and Multiple Y Axes
by Anonymous Monk on Dec 28, 2010 at 19:55 UTC
    Replace below line # two_axes => 'true', with two_axes => 1, then problem solved

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-28 18:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found