GD::Graph::lines

N

neilsolent

I am trying to plot some line graphs using GD::Graph::lines

The trouble is, if there are a lot of lines, the colors inevitably
repeat and it is impossible to distinguish different lines. If I could
change the order of the labels in the legend (which seems to insist on
alphanumerical) to the height order or on the graph, OR label the
lines on the graph directly, all would be well ..

Anyone any ideas?

thanks..
 
N

neilsolent

I've sorted this now. My solution is to arrange the legend in the
order of the last value of each line - then it is fairly easy to read:

sub LineGraphGif
{
# Args: x, y, xlabel, ylabel, title, filename
# x is a reference to an array (points on x-axis)
# y is a reference to a hash (key=dataset name, value=reference to an
array of y values)

my ($x, $y, $xlabel, $ylabel, $title, $filename) = @_;
my ($graph, $image, @data, @colours, @legend);

push (@data, $x);

# Add lines to the graph in the order of the size of the last value
for each line - this helps visibility by using the legend if there is
colour clash
foreach (sort {$y->{$b}->[-1] <=> $y->{$a}->[-1]} keys %$y)
{
push (@data, $y->{$_});
push (@colours, DataColour($_));
push (@legend, $_);
}

$graph = GD::Graph::lines->new(800, 400);
if (! $graph)
{
warn "Could not create new graph";
return;
}
$graph->set(
x_label => $xlabel,
y_label => $ylabel,
title => $title,
line_width => 2
) or warn $!;

$graph->set_legend(@legend) or warn $!;

$graph->set(legend_placement => 'RT');

$graph->set(dclrs => \@colours);

$image = $graph->plot(\@data);
if (! $image)
{
warn $graph->error;
return
};

if (open(IMG, ">$filename"))
{
binmode(IMG);
print IMG $image->gif();
close(IMG);
}
else
{
warn "$!";
}
}
 

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
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top