Chart
view release on metacpan or search on metacpan
lib/Chart/Direction.pm view on Meta::CPAN
# Look a the number of ticks.
if ( $maxTicks < $tickCount )
{
# If it is too high, Backtrack.
$divisor = pop @divisorList;
# just for security:
if ( !defined($divisor) || $divisor == 0 ) { $divisor = 1; }
( $tickCount, $pMin, $pMax ) = $self->_countTicks( $min, $max, 1 / $divisor );
carp "Chart::Direction : Caution: Tick limit of $maxTicks exceeded. Backing of to an interval of "
. 1 / $divisor
. " which plots $tickCount ticks\n";
return ( 1 / $divisor, $tickCount, $pMin, $pMax );
}
elsif ( $minTicks > $tickCount )
{
# If it is too low, try again.
next TRY;
}
else
{
# Store the divisor for possible later backtracking.
push @divisorList, $divisor;
# if the min or max is fixed, check they will fit in the interval.
next TRY if ( $minF && ( int( $min * $divisor ) != ( $min * $divisor ) ) );
next TRY if ( $maxF && ( int( $max * $divisor ) != ( $max * $divisor ) ) );
# If everything passes the tests, return.
return ( 1 / $divisor, $tickCount, $pMin, $pMax );
}
}
}
die "can't happen!";
}
## @fn private int _draw_y_ticks()
# draw the circles and the axes
#
# Overwrites _draw_y_ticks() of Base class
#
# @return status
sub _draw_y_ticks
{
my $self = shift;
my $data = $self->{'dataref'};
my $misccolor = $self->_color_role_to_index('misc');
my $textcolor = $self->_color_role_to_index('text');
my $background = $self->_color_role_to_index('background');
my @labels = @{ $self->{'y_tick_labels'} };
my ( $width, $height, $centerX, $centerY, $diameter );
my ( $pi, $font, $fontW, $fontH, $labelX, $labelY, $label_offset );
my ( $dia_delta, $dia, $x, $y, @label_degrees, $arc, $angle_interval );
# set up initial constant values
$pi = 3.14159265358979323846,
$font = $self->{'legend_font'},
$fontW = $self->{'legend_font'}->width,
$fontH = $self->{'legend_font'}->height,
$angle_interval = $self->{'angle_interval'};
if ( $self->true( $self->{'grey_background'} ) )
{
$background = $self->_color_role_to_index('grey_background');
}
# init the imagemap data field if they wanted it
if ( $self->true( $self->{'imagemap'} ) )
{
$self->{'imagemap_data'} = [];
}
# find width and height
$width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
$height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
# find center point, from which the pie will be drawn around
$centerX = int( $width / 2 + $self->{'curr_x_min'} );
$centerY = int( $height / 2 + $self->{'curr_y_min'} );
# always draw a circle, which means the diameter will be the smaller
# of the width and height. let enough space for the labels.
## @todo calculate the width of the labels
if ( $width < $height )
{
$diameter = $width - 110;
}
else
{
$diameter = $height - 80;
}
#the difference between the diameter of two following circles;
$dia_delta = ceil( $diameter / ( $self->{'y_ticks'} - 1 ) );
#store the calculated data
$self->{'centerX'} = $centerX;
$self->{'centerY'} = $centerY;
$self->{'diameter'} = $diameter;
#draw the axes and its labels
# set up an array of labels for the axes
if ( $angle_interval == 0 )
{
@label_degrees = ();
}
elsif ( $angle_interval <= 5 && $angle_interval > 0 )
{
@label_degrees = qw(180 175 170 165 160 155 150 145 140 135 130 125 120 115
110 105 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 355 350
345 340 335 330 325 320 315 310 305 300 295 290 285 280 275 270 265 260 255
250 245 240 235 230 225 220 215 210 205 200 195 190 185);
$angle_interval = 5;
}
elsif ( $angle_interval <= 10 && $angle_interval > 5 )
{
@label_degrees = qw(180 170 160 150 140 130 120 110 100 90 80 70 60 50 40
lib/Chart/Direction.pm view on Meta::CPAN
# draw the last line to the first point
if ( $self->true( $self->{'line'} ) )
{
$self->{'gd_obj'}->line( $x, $y, $first_x, $first_y, gdBrushed );
}
$n += 2;
} # end for $j
} # end if pairs
# now outline it
$self->{'gd_obj'}
->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
return;
}
## @fn private int _prepare_brush($color,$type)
# set the gdBrush object to trick GD into drawing fat lines
#
#
# @param[in] color Color to be used
# @param[in] type Type of line
# @return status
sub _prepare_brush
{
my $self = shift;
my $color = shift;
my $type = shift;
my ( $radius, @rgb, $brush, $white, $newcolor );
@rgb = $self->{'gd_obj'}->rgb($color);
# get the appropriate brush size
if ( $type eq 'line' )
{
$radius = $self->{'brush_size'} / 2;
}
elsif ( $type eq 'point' )
{
$radius = $self->{'pt_size'} / 2;
}
# create the new image
$brush = GD::Image->new( $radius * 2, $radius * 2 );
# get the colors, make the background transparent
$white = $brush->colorAllocate( 255, 255, 255 );
$newcolor = $brush->colorAllocate(@rgb);
$brush->transparent($white);
# draw the circle
$brush->arc( $radius - 1, $radius - 1, $radius, $radius, 0, 360, $newcolor );
# fill it if we're using lines
$brush->fill( $radius - 1, $radius - 1, $newcolor );
# set the new image as the main object's brush
return $brush;
}
## @fn private int _draw_legend()
# let them know what all the pretty colors mean
# @return status
#
# Overwrite corresponding function of Base
#
sub _draw_legend
{
my $self = shift;
my $length;
# check to see if legend type is none..
if ( $self->{'legend'} =~ /^none$/ )
{
return 1;
}
# check to see if they have as many labels as datasets,
# warn them if not
if ( ( $#{ $self->{'legend_labels'} } >= 0 )
&& ( ( scalar( @{ $self->{'legend_labels'} } ) ) != $self->{'num_datasets'} ) )
{
carp "The number of legend labels and datasets doesn\'t match";
}
# init a field to store the length of the longest legend label
unless ( $self->{'max_legend_label'} )
{
$self->{'max_legend_label'} = 0;
}
# fill in the legend labels, find the longest one
if ( $self->false( $self->{'pairs'} ) )
{
for ( 1 .. $self->{'num_datasets'} )
{
unless ( $self->{'legend_labels'}[ $_ - 1 ] )
{
$self->{'legend_labels'}[ $_ - 1 ] = "Dataset $_";
}
$length = length( $self->{'legend_labels'}[ $_ - 1 ] );
if ( $length > $self->{'max_legend_label'} )
{
$self->{'max_legend_label'} = $length;
}
} #end for
}
if ( $self->true( $self->{'pairs'} ) )
{
for ( 1 .. ceil( $self->{'num_datasets'} / 2 ) )
{
unless ( $self->{'legend_labels'}[ $_ - 1 ] )
{
$self->{'legend_labels'}[ $_ - 1 ] = "Dataset $_";
}
$length = length( $self->{'legend_labels'}[ $_ - 1 ] );
if ( $length > $self->{'max_legend_label'} )
{
$self->{'max_legend_label'} = $length;
}
}
}
# different legend types
if ( $self->{'legend'} eq 'bottom' )
{
$self->_draw_bottom_legend;
}
elsif ( $self->{'legend'} eq 'right' )
{
$self->_draw_right_legend;
}
elsif ( $self->{'legend'} eq 'left' )
{
$self->_draw_left_legend;
}
elsif ( $self->{'legend'} eq 'top' )
{
$self->_draw_top_legend;
}
else
{
carp "I can't put a legend there (at " . $self->{'legend'} . ")\n";
}
# and return
return 1;
}
## @fn private array _find_y_range()
# Find minimum and maximum value of y data sets.
#
# @return ( min, max, flag_all_integers )
#
# Overwrites corresponding Base function
#
sub _find_y_range
{
my $self = shift;
my $data = $self->{'dataref'};
my $max = undef;
my $min = undef;
my $k = 1;
my $dataset = 1;
my $datum;
if ( $self->false( $self->{'pairs'} ) )
{
for $dataset ( @$data[ 1 .. $#$data ] )
{
# print "dataset @$dataset\n";
for $datum (@$dataset)
{
if ( defined $datum )
{
# Prettier, but probably slower:
# $max = $datum unless defined $max && $max >= $datum;
# $min = $datum unless defined $min && $min <= $datum;
if ( defined $max )
{
if ( $datum > $max ) { $max = $datum; }
elsif ( $datum < $min ) { $min = $datum; }
}
else
{
$min = $max = $datum;
}
} #endif defined
} # end for
}
}
if ( $self->true( $self->{'pairs'} ) )
{
# only every second dataset must be checked
for $dataset ( @$data[$k] )
{
for $datum (@$dataset)
{
( run in 1.296 second using v1.01-cache-2.11-cpan-d8267643d1d )