Skip to content

Commit adeedc9

Browse files
authored
Merge pull request #1441 from pstaabp/statplot-fillcolors-barplot
This add the fill_colors option to the add_barplot method.
2 parents eef558b + 09316d5 commit adeedc9

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

macros/graph/StatisticalPlots.pl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,27 @@ =head3 OPTIONS
118118
This sets the width of the rectangle borders. This is an alias for the C<width>
119119
L<dataset option|plots.pl/DATASET OPTIONS>.
120120
121+
=item fill_colors
122+
123+
This is either the name of a color palette, a reference to an array of colors,
124+
or a reference to a hash containing the name of a color palette
125+
(C<palette_name>) and number of colors to generate (C<num_colors>) (see L<COLOR
126+
PALETTES> for more information). If this is a reference to an array and the
127+
length of the array is smaller than the number of data values in the array
128+
referenced to by C<$data>, then the colors will be cycled.
129+
130+
If C<fill_color> is included in the list of options, this will apply the same
131+
color to each bar. If neither C<fill_color> nor C<fill_colors> is included,
132+
then the 'rainbow' palette will be used as default.
133+
134+
For example,
135+
136+
fill_colors => 'rainbow'
137+
138+
fill_colors => ['green', 'OliveGreen', 'DarkGreen', 'ForestGreen', 'PineGreen']
139+
140+
fill_colors => { palette_name => 'random', num_colors => 7 }
141+
121142
=back
122143
123144
=head2 HISTOGRAMS
@@ -568,6 +589,13 @@ sub add_barplot {
568589
# TODO: Should arbitrarily spaced bars be handled?
569590
my $bar_width = $options{orientation} eq 'vertical' ? $xdata->[1] - $xdata->[0] : $ydata->[1] - $ydata->[0];
570591

592+
my $fill_colors =
593+
ref $options{fill_colors} eq 'HASH'
594+
? color_palette($options{fill_colors}{palette_name}, $options{fill_colors}{num_colors})
595+
: (!defined $options{fill_colors} || ref $options{fill_colors} ne 'ARRAY')
596+
? color_palette($options{fill_colors})
597+
: $options{fill_colors};
598+
571599
return $self->add_rectangle(
572600
map { [
573601
$options{orientation} eq 'vertical'
@@ -579,6 +607,7 @@ sub add_barplot {
579607
[ 0, $ydata->[$_] - 0.5 * $bar_width * $options{bar_width} ],
580608
[ $xdata->[$_], $ydata->[$_] + 0.5 * $bar_width * $options{bar_width} ]
581609
),
610+
fill_color => $fill_colors->[ $_ % @$fill_colors ],
582611
%options
583612
] } 0 .. $#$xdata
584613
);

0 commit comments

Comments
 (0)