GD::Graph: How to invert Y axis

F

francescomoi

Hi.

Working with GD::Graph::lines, I want to make a graph of historical
ranking data. I've got:
@dataArray = ('4', '3', '5', '1');

But when making the graph, '1' appears at the bottom of the the image,
and I want it to appear at the top, because it's the first position
of the ranking.

I want to invert Y axis, and make the biggest numbers appear at the
bottom, not at the top.

I've been browsing GD::Graph docs:
http://search.cpan.org/~mverb/GDGraph-1.43/Graph.pm
but I didn't find any tip to do it.

Any suggestion? Thx.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
Working with GD::Graph::lines, I want to make a graph of historical
ranking data. I've got:
@dataArray = ('4', '3', '5', '1');

But when making the graph, '1' appears at the bottom of the the image,
and I want it to appear at the top, because it's the first position
of the ranking.

I want to invert Y axis, and make the biggest numbers appear at the
bottom, not at the top.

Well, this is a hack, but you can try something like code below. This is
still ugly, though, because I haven't been able to figure out how to get
the y-label ticks to be [1, 2, 3, ..., 10 ].

#!/usr/bin/perl

use strict;
use warnings;

use GD::Graph::linespoints;

my $graph = GD::Graph::linespoints->new(400,300);

my @years = (1995 .. 2005);
my @rankings = map { - (int(1 + rand 10)) } @years;

$graph->set(
y_max_value => 0,
y_min_value => -11,
y_label_skip => 1,
y_tick_number => 10,
y_number_format => sub { abs $_[0] },
long_ticks => 1,
);

my $gd = $graph->plot([\@years, \@rankings])
or die $graph->error;

open my $png, '>', 'test.png' or die $!;
binmode $png;
print $png $gd->png;
close $png or die $!;
 
A

A. Sinan Unur

ko said:
(e-mail address removed) wrote: ....
y_tick_number => 4, # assuming ranking range is 1 - 5
y_number_format => sub { - int shift },

You are truncating y tick labels. Which means, if GD::Graph computes two
labels as 3.1 and 3.99, there will be two 3's along the y-axis. Also, the
tick labels will not line up with the plotted points, because the lables
will be drawn at the (0,3.1) and (0, 3.99) coordinates.

The solution would be to find a way of telling GD::Graph to put integer
labels at the true integer coordinates on the y-axis.

Sinan
 
A

A. Sinan Unur

ko said:
That's *quite* obvious. But I'm using the OP's data and not trying to
read anything else into it. (e.g. the data *requires* decimal
representation, like your example above) From the original post:

@dataArray = ('4', '3', '5', '1');

Although the data is stringified, the array elements are all integers.
Furthermore, the OP stated "I want to make a graph of historical
ranking data". Simple ranking data typically uses integer values. e.g.

Quality of our customer service: 1 2 3 4 5

Not:

Quality of our customer service: 3.1 3.99 (decimal)

On the other hand, GD::Graph calculates y-tick mark positions with
decimal digits. Truncating them only changes *how* the labels appear,
not their positions. So, the truncation you use would put a 3 at y-tick
position, say, 3.1, whereas the data point plotted would have ordinate
3. Therefore, the tick-marks and the data points would not line up.

Now, with a y-range of only 1 .. 5, this does not seem to be a problem.
But in general, when the number of tick-marks increases, say, 1 .. 10,
this seems to be a problem, at least in the charts I produced on my
system.

Hence, while the truncation seems to produce the visual result one wants
in the particular case above, my caution for the general case.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,443
Members
47,089
Latest member
Bobby2025b

Latest Threads

Top