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

Two questions for GD::Graph

by expresspotato (Beadle)
on Jan 12, 2011 at 18:49 UTC ( #881960=perlquestion: print w/replies, xml ) Need Help??
expresspotato has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm having two problems with GD::Graph. First I am looking to see if there is a way to "slant" the x-axis values to provide longer names for them. Because they are parallel to the chart's axis, long values can't be used for the ticks. I've used x_tick_skip but would still like the x-axis values slanted to provide more room and tighter spacing. Second, I was looking to see if there was a way to apply a square or lined (along the y axis) grid behind the graph. Thank you, Kevin

Replies are listed 'Best First'.
Re: Two questions for GD::Graph
by Anonyrnous Monk (Hermit) on Jan 12, 2011 at 19:29 UTC

    As for the grid, maybe you want long_ticks? (or x_long_ticks and y_long_ticks to set them separately)

    I'm not sure if you can print the labels slanted (actually, I'm afraid you can't without hacking the source1), but for long labels there's x_labels_vertical.


    1 the idea would be to change $angle in the following snippet:

    if (defined $text) { $self->{gdta_x_axis}->set_text($text); my $angle = 0; if ($self->{x_labels_vertical}) { $self->{gdta_x_axis}->set_align('bottom', 'center'); $angle = PI/2; } else { $self->{gdta_x_axis}->set_align('center', 'right'); } $self->{gdta_x_axis}->draw($x - $self->{axis_space}, $y, $ +angle); }


      Great! Both of these were exactly what I was looking for. Do you know how I can change the y_long_ticks colour or make the line dotted? Thank you, Kevin

        I don't think this is configurable, but - as usual - you could modify the sources.  In this case, it looks like changing the following snippet (search for "# Ticks and values for y axes" in

        if ($self->{y_long_ticks}) { $self->{graph}->line( $x, $self->{bottom}, $x, $self->{top}, $self->{fgci} ) unless ($axis-1); }

        might have the desired effect...

        The ->line method is from the GD module, so check its docs for what parameters are expected. The $self->{fgci} argument (foreground color) is presumably what you'd need to manipulate. See also ->setStyle for dotted/dashed lines.

Re: Two questions for GD::Graph
by moritz (Cardinal) on Jan 12, 2011 at 19:40 UTC
Re: Two questions for GD::Graph
by ambrus (Abbot) on Jan 13, 2011 at 14:24 UTC

    Yes, both rotating the values and adding a grid is possible with just a few set commands. The following code, modified from Plot a spiral with gnuplot, does both.

    use strict; use warnings; use 5.010; use IO::Handle; use File::Temp "tempfile"; my($T,$N) = tempfile("plot-XXXXXXXX", "UNLINK", 1); for my $t (100..500) { say $T $t*sin($t*0.1), " ", $t*cos($t*0.1); } open my $P, "|-", "gnuplot" or die; printflush $P qq[ unset key set xtics rotate by -45 #set grid # vertical and horizontal grid lines set grid xtics # vertical grid lines only plot "$N" with lines lw 3 ]; <STDIN>; close $P; __END__

    Update: rotating the text is tricky in that some of the terminals (output drivers of gnuplot) does not support it, or only supports rotating by multiples of right angles. This may also depend on the version of gnuplot you have. Rotating the text by any angle does work in at least the postscript driver though.

      You're using gnuplot. The question was about GD::Graph. This is a pretty significant difference. (That said, gnuplot is a good tool to use.)
Re: Two questions for GD::Graph
by expresspotato (Beadle) on Jan 13, 2011 at 16:23 UTC
    Also I seem to be having a problem with x_label_skip. It seems GD::Graph is always trying to add the last label and does so right ontop of the label that was skipped to. Does anyone know how to work around this?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://881960]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-05-20 20:47 GMT
Find Nodes?
    Voting Booth?