+# This takes a set of tags, as returned by C<get_approval_rows> and divides
+# them up into a number of "strata" based on their weight. This is useful
+# to display them in a number of different sizes.
+#
+# Usage:
+# ($min, $max) = stratify_tags($strata, $tags);
+# $stratum: the number of divisions you want
+# $tags: the tags, as provided by get_approval_rows
+# $min: the minumum stratum value
+# $max: the maximum stratum value. This may be the same as $min if there
+# is only one weight. Beware of divide by zeros.
+# This will add a field to the tag called "stratum" containing the calculated
+# value.
+sub stratify_tags {
+ my ( $strata, $tags ) = @_;
+
+ my ( $min, $max );
+ foreach (@$tags) {
+ my $w = $_->{weight_total};
+ $min = $w if ( !defined($min) || $min > $w );
+ $max = $w if ( !defined($max) || $max < $w );
+ }
+
+ # normalise min to zero
+ $max = $max - $min;
+ my $orig_min = $min;
+ $min = 0;
+
+ # if min and max are the same, just make it 1
+ my $span = ( $strata - 1 ) / ( $max || 1 );
+ foreach (@$tags) {
+ my $w = $_->{weight_total};
+ $_->{stratum} = int( ( $w - $orig_min ) * $span );
+ }
+ return ( $min, $max );
+}
+