Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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?


Re: GD::Graph::lines

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 ->[-1] <=>  $y->->[-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";
        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;

    if (open(IMG, ">$filename"))
        print IMG $image->gif();
        warn "$!";

Site Timeline