Graphics-HotMap

 view release on metacpan or  search on metacpan

lib/Graphics/HotMap.pm  view on Meta::CPAN


   # Create a new HotMap
   my $hotMap = Graphics::HotMap->new(
         minValue => 1,
         maxValue => 50, 
         );  

   # Define scale
   $hotMap->scale(20);

   # Show legend
   $hotMap->legend(1);

   # Show CrossMarks and values
   $hotMap->crossMark(1,1);

   # Define a new size
   $hotMap->mapSize({ sizeX => 15, sizeY => 15 }); 

   # Add time
   $hotMap->addHorodatage(time, 15, 30);

lib/Graphics/HotMap.pm  view on Meta::CPAN


=for ref

Construct and return a new HotMap Object;

=for usage

   Graphics::HotMap->new(
      outfileGif        => <File path>, # file to write GIF
      outfilePng        => <File path>, # file to write PNG
      legend            => [0|1],       # activate lengend
      legendNbGrad      => <number>,    # Number a graduation
      cross             => <bool>,      # activate crossing of known values
      crossValues       => <bool>,      # activate values printing whith cross
      minValue          => <number>,    # minimum value
      maxValue          => <number>,    # maximum value
      font              => <path to font file>,
      fontSize          => <number>,    # font size
      scale             => <number>,    # scale values and coordonates
      sizeX             => <number>,    # X size
      sizeY             => <number>,    # Y size
   );

lib/Graphics/HotMap.pm  view on Meta::CPAN

   );


=cut

sub new {
   my ($class, %params) = (@_);
   my $self={};
   $self->{_outfileGif}   = $params{outfileGif}   || undef;
   $self->{_outfilePng}   = $params{outfilePng}   || undef;
   $self->{_legend}       = $params{legend}       || 0;
   $self->{_legendNbGrad} = $params{legendNbGrad} || 7;
   $self->{_crossMark}    = $params{cross}        || 0;
   $self->{_crossMarkTemp}= $params{crossTemp}    || 0;
   #$self->{_minValue}     = $params{minValue}     || 0;
   #$self->{_maxValue}     = $params{maxValue}     || 70;
   $self->{_font}         = $params{font}         || '/usr/share/fonts/truetype/freefont/FreeSans.ttf';
   $self->{_fontSize}     = $params{fontSize}     || 15;
   $self->{_text}         = ();
   $self->{_horodatage}   = $params{horodatage}   || [0, 0, 0];
   $self->{_scale}        = $params{echelle}      || 1;
   $self->{_verbose}      = $params{verbose}      || 0;

lib/Graphics/HotMap.pm  view on Meta::CPAN

   my $tmpPiddle = $self->{_mapPoints};
   #$tmpPiddle->where($tmpPiddle > PALETTE_SLICE) -= PALETTE_SLICE;

   if (defined $function) {
      return scalar (floor($tmpPiddle)) if ($function eq 'floor');
      die "toString: Unknown Function. => '$function'",$/;
   }
   return scalar ($tmpPiddle);
}

=item legend()

=for ref

Set or Return legend status. When enabled, the legend gradient will be drawn on the image.

=cut

sub legend {
   my $self = shift;
   my ($value) = @_;

   if (defined $value) {
      $self->{_legend} = 1;
   } else {
      return $self->{_legend};
   }
}

=item crossMark()

=for ref

Set or Return cross marks status. When enabled, a cross will be drawn on the image where points have been defined.

=cut

lib/Graphics/HotMap.pm  view on Meta::CPAN

         fill=>$color,
         text=>$text,
         align=>$align,
         x=>$x,
         y=>$y,
         rotate=>$rotate,
         );
}

=for comment
Internal function for generating legend bar on the image

=cut

sub _drawLegendBar {
   my $self = shift;
   my ($gradientName, $i, $im) = @_;
   my $repere = 10;

   my $legendBar = Graphics::HotMap->new(
      wall     => 1,
   );
   $legendBar->{_gradient} = $self->{_gradient};
   $legendBar->mapSize({
      sizeX => 10,
      sizeY => 500,
      });
   
   $legendBar->addLayer({ layerName => '_Legend'.$gradientName, visibility => 1, gradientName => $gradientName });

   my $nbGrad = $self->{_gradient}{$gradientName}{nbColors}; #$self->{_legendNbGrad}-1;
   my $min    = $self->{_gradient}{$gradientName}{minValue};
   my $max    = $self->{_gradient}{$gradientName}{maxValue};

   for (0..$nbGrad) {
      my $x = $legendBar->{_mapSize}{x}-1;
      my $y = $_/$nbGrad*($legendBar->{_mapSize}{y}-1);
      my $valeur = $max-(int(($nbGrad-$_)/$nbGrad*($max-$min)));
      my $unit   = $legendBar->{_gradient}{$gradientName}{unit};
      $legendBar->addPoint({
            layerName => '_Legend'.$gradientName,
            #x => $_/$nbGrad*($legendBar->{_mapSize}{x}-1),
            #y => $legendBar->{_mapSize}{y}-1-$repere*$i,
            x => $x,
            y => $y,
            value => $valeur,
            noScale => 1,
            unit => $unit,
            });
      $legendBar->addText ( {
              x => $x+15,
              y => $y+10,
              text => int($valeur).$unit,
              size => 10,
              align => 'center'
              } ) if ($nbGrad < 11 || $_%5 == 0);
   }

   $legendBar->addZone({
         layerName =>'_Legend'.$gradientName,
         zoneName => '_Legend'.$gradientName,
         coordonates => [
         1,
         1,
         $legendBar->{_mapSize}{x}-1,
         $legendBar->{_mapSize}{y}-1,
         ],
         noScale => 1 });
   $legendBar->_genDegradZone('_Legend'.$gradientName, $legendBar->{_zones}{'_Legend'.$gradientName}{'_Legend'.$gradientName}, 1);

   my $imag =  byte $legendBar->{_mapPoints};
   my $tmpName = new File::Temp( TEMPLATE => 'generated-XXXXX',
                                 DIR => '/tmp/',
                                 SUFFIX => '.png',
                                 OPEN => 0);
   #my $tmpName = tmpnam().'.png';
   my $cptLoop = 0;
   do {
      eval {$imag->wpic($tmpName, { LUT => $legendBar->{_gradient}{colors} }); };
   #        $imag->wpic($tmpName, { LUT => $legendBar->{_gradient}{colors} });
      ++$cptLoop;
   } while ($@ && $cptLoop < 10);

   if ($cptLoop > 2) {
   	print "ARgh ! Function: _saveImage ; nbErr for wpic:$cptLoop\n";
	exit;
   }

# read the temporary File in PerlMagick
   my $status = $im->ReadImage($tmpName);
   warn $status if $status;
   #unlink $tmpName;

# Flip the image
   $im->[$i+1]->Flip;
   $im->[$i+1]->Border(fill=>'black', width=>-1, height=>-1);
   $im->[$i+1]->Extent(
           background => 'white',
           geometry   => ($legendBar->{_mapSize}{x}+35).'x'.($legendBar->{_mapSize}{y}+15),
           gravity    => 'West',
           );
   $legendBar->_genText($im->[$i+1]);
   $im->[$i+1]->Extent(
           background => 'white',
           geometry   => ($legendBar->{_mapSize}{x}+35).'x'.$self->{_mapSize}{y},
           gravity    => 'Center',
           );

   $im->[$i+1]->Extent(
           background => 'white',
           geometry   => ($legendBar->{_mapSize}{x}+35+20).'x'.$self->{_mapSize}{y},
           gravity    => 'East',
           );
   $im->[$i+1]->Annotate(
         font=>$self->{_font},
         pointsize=>10,
         fill=>'black',
         text=>$gradientName,
         align=>'right',
         x=>10,
         y=>35,
         rotate=>270,
         );
   $self->{_im} = $im->Append(stack=>'false');
}

=for comment
Internal function for generating legend on the image

=cut

sub _genLegende {
   my $self = shift;
   my ($im) = @_;

   my $i=0;
   #print "Printing Gradient Bars",$/;
   foreach my $gradientName (sort keys %{$self->{_gradient}}) {

lib/Graphics/HotMap.pm  view on Meta::CPAN

   #my $im = $image->[0];
   $im->Flip;

# Gen CrossMarks
   $self->_genCrossMark($im) if $self->{_crossMark};
# Draw time on image
   $self->_drawTime($im) if $self->{_horodatage}[0];
# Draw texts
   $self->_genText($im);

# Gen legend in piddle
   $self->_genLegende($im) if $self->{_legend};
}

=for comment
Really compute the interpolation from known points.

=cut

sub _pdlDegrad {
   my $self = shift;
   my ($input, $output, $sliceColors, $gradientName) = @_;



( run in 1.179 second using v1.01-cache-2.11-cpan-49f99fa48dc )