GDGraph-XY

 view release on metacpan or  search on metacpan

xylines.pm  view on Meta::CPAN


use strict;
 
use GD;
use GD::Graph::axestype;
use GD::Graph::utils qw(:all);

@GD::Graph::xylines::ISA = qw( GD::Graph::axestype );

use constant PI => 4 * atan2(1,1);

my %Defaults = (
 
  #Pad things a bit to make them look nicer
	b_margin      => 5,
	r_margin      => 5,

  # We want long ticks by default
	x_long_ticks			=> 1,
	y_long_ticks			=> 1,
 
	# Number of ticks for the y axis
	y_tick_number		=> 10,
	x_tick_number		=> 13,		
	x_precision  		=> undef,	
 
	# Skip every nth label. if 1 will print every label on the axes,
	# if 2 will print every second, etc..
	x_label_skip		=> 1,
	y_label_skip		=> 1,

	# Do we want ticks on the x axis?
	x_ticks				=> 1,
	x_all_ticks			=> 0,

	# Where to place the x and y labels
	x_label_position	=> 1/2,
	y_label_position	=> 1/2,

	# vertical printing of x labels
	x_labels_vertical	=> 1,

	# Draw axes as a box? (otherwise just left and bottom)
	box_axis			=> 1,
 
);

sub initialise
{
	my $self = shift;

	$self->SUPER::initialise();

	while (my($key, $val) = each %Defaults) 
		{ $self->{$key} = $val }

	$self->set_x_label_font(GD::gdSmallFont);
	$self->set_y_label_font(GD::gdSmallFont);
	$self->set_x_axis_font(GD::gdTinyFont);
	$self->set_y_axis_font(GD::gdTinyFont);
	$self->set_legend_font(GD::gdTinyFont);
	$self->set_values_font(GD::gdTinyFont);
}

# PRIVATE
sub set_max_min 
{
  my $self = shift;

  my $x_max = undef;
  my $x_min = undef;
  my $y_max = undef;
  my $y_min = undef;
  
  for my $i ( 1 .. $self->{_data}->num_sets )	# 1 because x-labels are [0]
  {
    # Contributed by Andrew Crabb - ahc@sol.jhoc1.jhmi.edu
    my $num_points_limit = $self->{_data}->num_points - 1;
    for my $j ( 0 .. $num_points_limit )
    {
      my $val = $self->{_data}->[$i][$j];
      $y_max = $val if ((not defined($y_max)) or ($val > $y_max));
      $y_min = $val if ((not defined($y_min)) or ($val < $y_min));
    }
  }

  # Contributed by Andrew Crabb - ahc@sol.jhoc1.jhmi.edu
  my $num_points_limit = $self->{_data}->num_points - 1;
  for my $k ( 0 .. $num_points_limit ) # x-values are at [0]
  {
    my $val = $self->{_data}->[0][$k];
    $x_max = $val if ((not defined($x_max)) or ($val > $x_max));
    $x_min = $val if ((not defined($x_min)) or ($val < $x_min));
  }

  # Set the min and max's
	$self->{y_min}[1] = $y_min;
	$self->{y_max}[1] = $y_max;

	$self->{y_min}[2] = $y_min;
	$self->{y_max}[2] = $y_max;

	$self->{x_min} = $x_min;
	$self->{x_max} = $x_max;

  # Calculate the needed precision. 

  my $x_pre =  int(log(abs(($self->{x_max} - $self->{x_min}) / ($self->{x_tick_number} - 1) + $self->{x_min}))) + 1;

  $x_pre = $x_pre < 0 ? 0 : $x_pre;

	# Overwrite these with any user supplied ones
	$self->{y_min}[1] = $self->{y_min_value}  if defined $self->{y_min_value};
	$self->{y_max}[1] = $self->{y_max_value}  if defined $self->{y_max_value};

	$self->{y_min}[1] = $self->{y1_min_value} if defined $self->{y1_min_value};
	$self->{y_max}[1] = $self->{y1_max_value} if defined $self->{y1_max_value};

	$self->{y_min}[2] = $self->{y2_min_value} if defined $self->{y2_min_value};
	$self->{y_max}[2] = $self->{y2_max_value} if defined $self->{y2_max_value};



( run in 4.546 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )