This fully functional snippet shows how you can specify the colors used in a graph based on the data being used. I haven't tested it with multiple datasets but with a slight modification it should work.
Added: woozles.png
#!/usr/bin/perl use strict; use warnings; use GD::Graph::bars3d; # sample data my @data = (30, 45, 36, 42, 40, 44, 41, 39, 35, 46, 43, 38); my @dt = ('2002-10-10 00:00:00', '2002-10-10 01:00:00', '2002-10-10 02 +:00:00', '2002-10-10 03:00:00', '2002-10-10 04:00:00', '2002-10-10 0 +5:00:00', '2002-10-10 06:00:00', '2002-10-10 07:00:00', '2002-10-10 0 +8:00:00', '2002-10-10 09:00:00', '2002-10-10 10:00:00', '2002-10-10 1 +1:00:00'); # setup the color set my @colors; for my $datum (@data) { push(@colors, get_color($datum)); } # create the data set for the graph my @dataset = (\@dt, \@data); # create the new graph my $graph = GD::Graph::bars3d->new( 400, 300 ); # setup the graph options $graph->set( x_label => 'Date / Time', y_label => 'Number of Woozles Sold', title => 'World Wide Woozle Sales', dclrs => \@colors, cycle_clrs => 1, # this is mandatory for this to wo +rk x_labels_vertical => 1, ); # plot the graph my $gd = $graph->plot(\@dataset); # create a PNG file of the graph open(IMG, '>woozles.png') or die $!; binmode IMG; print IMG $gd->png; # returns the color name based on the value given # this is only an example # you should base your numbers off of your actual data sub get_color { my $value = shift; return "dpurple" if ($value < 34); return "lpurple" if ($value < 36); return "blue" if ($value < 38); return "green" if ($value < 40); return "yellow" if ($value < 42); return "orange" if ($value < 44); return "red"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Selective Graph Colors
by zentara (Cardinal) on Oct 12, 2002 at 14:54 UTC | |
|
Re: Selective Graph Colors
by Mr. Muskrat (Canon) on Oct 14, 2002 at 20:23 UTC |