Imager-Chart-Radial

 view release on metacpan or  search on metacpan

Radial.pm  view on Meta::CPAN


    $axis->{X} = $x;
    $axis->{Y} = $y;

    # round down coords
    $x_outer =~ s/(\d+)\..*/$1/;
    $y_outer =~ s/(\d+)\..*/$1/;
    $x_label =~ s/(\d+)\..*/$1/;
    $y_label =~ s/(\d+)\..*/$1/;
    # draw value
    if ($i != 0) {
      my $r = 0;
      foreach my $record (@{$self->{records}}) {
        my $value = $record->{Values}->{$axis->{Label}};
        my $last_value = $record->{Values}->{$self->{axis}[$i-1]->{Label}};
        my $colour = $colours{$record->{Colour}};
        my $x_interval_1 = $x_centre + ($x * (100 / $scale{Max}) * $value);
        my $y_interval_1= $y_centre + ($y * (100 / $scale{Max}) * $value);
        my $shape = $record->{Shape};
#        $self->draw_shape($x_interval_1,$y_interval_1,$record->{Colour}, $r);
        my $x_interval_2 = $x_centre + ($self->{axis}[$i-1]->{X} * (100 / $scale{Max}) * $last_value);
        my $y_interval_2= $y_centre + ($self->{axis}[$i-1]->{Y} * (100 / $scale{Max}) * $last_value);
	$self->{_image}->line(color=>$colour, x1=>$x_interval_1,x2=>$x_interval_2,
			      y1=>$y_interval_1,y2=>$y_interval_2,antialias=>1);

#        $self->{_im}->line($x_interval_1,$y_interval_1,$x_interval_2,$y_interval_2,$colour);
        if ($i == scalar @{$self->{axis}} -1) {
          my $first_value = $record->{Values}->{$self->{axis}[0]->{Label}};
          my $x_interval_2 = $x_centre + ($self->{axis}[0]->{X} * (100 / $scale{Max}) * $first_value);
          my $y_interval_2= $y_centre + ($self->{axis}[0]->{Y} * (100 / $scale{Max}) * $first_value);
	  $self->{_image}->line(color=>$colour, x1=>$x_interval_1,x2=>$x_interval_2,
				y1=>$y_interval_1,y2=>$y_interval_2,antialias=>1);
#          $self->{_im}->line($x_interval_1,$y_interval_1,$x_interval_2,$y_interval_2,$colour);
#          $self->draw_shape($x_interval_2,$y_interval_2,$record->{Colour}, $r);
        }
        $r++;
      }
    }
    $i++;
  }
  return;
}

sub add_title {
  my ($self,$title) = @_;
  if (length $title > 30) {
    $_ = $title;
    my ($part_a,$part_b) = m/^(.{25,35})\s+(.*)$/;
      $self->{_image}->string( font  => $self->{fonts}{header}, text  => $part_a,
			       x => 45, y => 50, color => $colours{black}, aa => 1);
      $self->{_image}->string( font  => $self->{fonts}{header}, text  => $part_b,
			       x => 45, y => 75, color => $colours{black}, aa => 1);
  } else {
    $self->{_image}->string( font  => $self->{fonts}{header}, text  => $title,
			     x => 50, y => 50, color => $colours{black}, aa => 1);
  }
  return;
}


sub add_legend {
  my $Chart = shift;
  my $starty = 490;
  my $endy = 470 - (scalar @{$Chart->{records}} * 18);
  $Chart->{_image}->box( color=>$colours{black},xmin=>45,ymin=>$endy,
			 xmax=>250,ymax=>$starty+2,filled=>0);
  $Chart->{_image}->string( font  => $Chart->{fonts}{label}, text  => "Legend :",
			    x => 50, y => $endy+19, color => $colours{black}, aa => 1);
  foreach my $record (@{$Chart->{records}}) {
    $Chart->{_image}->string( font  => $Chart->{fonts}{label}, 
			      text  => "$record->{Label} : $record->{Colour}",
			      x => 50, y => $starty, color => $colours{$record->{Colour}}, aa => 1);
    $starty-=18;
  }
}

sub print {
  my $Chart = shift;
  my $filename = shift || $Chart->{filename};
  $Chart->{_image}->write(file=>$filename)
    || warn "error: couldn't print chart ",$Chart->{_image}->{ERRSTR},"\n";
  return;
}


###########################################################################################

1;


###########################################################################################

=head1 NAME

Imager::Chart::Radial

=head1 SYNOPSIS

=item use Imager::Chart::Radial;

=item my $chart = Radial->new(axis => \@axis, fonts => \%fonts);

=item $chart->plot_axis();

=item $chart->plot_values( \@records );>

=item $chart->add_title("This is a chart, there are many like it but this is mine");>

=item $chart->add_legend();>

=item $chart->print('mychart.png');

=head1 DESCRIPTION

This module uses Imager to plot and output Radial or Radar charts.

=head1 ABOUT

I originally wrote a radial chart creator based on GD, but the GD library did not provide anti-aliasing and sufficient colours for a clean looking image until relatively recently. I wrote this version because I wanted to learn Imager and also provide...

=head1 USING

=head2 Creating a class

To create a new Radial object use the new method on the class

my $chart = Radial->new(axis => \@axis, fonts => \%fonts);

This requires two data structures, one for your axis and one for the fonts you wish to use:

 my %fonts = (
	     text => Imager::Font->new(file  => '/path/to/fonts/cour.ttf', size  => 8),
	     header => Imager::Font->new(file  => '/path/to/fonts/arial.ttf', size  => 18),
	     label => Imager::Font->new(file  => '/path/to/fonts/arial.ttf', size  => 14),
	    );

Fonts must be TrueType compatible fonts, for more information see Imager::Font.

 my @axis    = (
                { Label => "Reliability" },
		{ Label => "Ease of Use" },
		{ Label => "Information" },
		{ Label => "Layout" },
		{ Label => "Navigation" },
		{ Label => "Searching" },
	      );

The axis are labelled as above and provide the skeleton of the graph

=head2 Plotting the graph

$chart->plot_axis();

This plots the axis onto the chart

$chart->plot_values( \@records );

This plots the values themselves onto the chart using the records data structure as below :

 my @records = (
                     { Label => "Foo", Colour => "red", Values => {
                             "Reliability"=>5,"Ease of Use"=>3, "Response Speed"=>6,"Information"=>4,
                             "Layout"=>3,"Navigation"=>6,"Organisation"=>7,"Searching"=>8, },
                     },
                     { Label => "Bar", Colour => "blue", Values => {
                             "Reliability"=>9,"Ease of Use"=>8,"Response Speed"=>4,"Information"=>5,
                             "Layout"=>8,"Navigation"=>8,"Organisation"=>8,"Searching"=>7,
                             },
                     },
                     { Label => "Baz", Colour => "green", Values => {
                         "Reliability"=>7,"Ease of Use"=>2,"Response Speed"=>9,"Information"=>8,
                         "Layout"=>3,"Navigation"=>4,"Organisation"=>6,"Searching"=>3,
                         },
                    },
            );


=head2 Labelling the graph

You can add a title and a legend to the chart using add_title and add_legend

$chart->add_title("This is a radial chart using Imager and my own values");

The title should be short and uses the font specified as header in the fonts hash.

$chart->add_legend();

The legend is generated from the records, you must therefore plot the graph before adding the legend. The legend uses the label font.

=head2 Outputing the graph

To write out the graph just call the print method with the filename you wish to write to.

$chart->print('newchart.png');

=head1 SEE ALSO

Imager

Imager::Font

GD

GD::Graph

=head1 AUTHOR

Aaron J Trevena E<lt>F<aaron@droogs.org>E<gt>

=head1 COPYRIGHT

Copyright (C) 2003, Aaron Trevena

This module is free software; you can redistribute it or modify it
under the same terms as Perl itself.


=cut

###########################################################################################
###########################################################################################



( run in 0.410 second using v1.01-cache-2.11-cpan-5735350b133 )