GDGraph-Map

 view release on metacpan or  search on metacpan

CHANGES  view on Meta::CPAN

  of the map (mapName) and don't use IMG tag (noImgMarkup).
  (added by Tomas Andersson <tomas@ibissoft.se>)
- Added support of the GD::Graph::lines object.
- Changed handling of the GD::Graph:linespoints object.


Version 1.02	September 18 1999

- Added GIFgraph bar_spacing option handling.
- Fixed bug with showing information in status line.
- Fixed bug with formating info and legend strings.
- Fixed bug with opening new navigator window. See BUGS.
- Modifing constructor. Now it has following syntax:
  new GIFgraph::Map($gr_object, [key1 => value1, key2 => value2, ...]);
  and you can seting options in constructor immediately.


Version 1.01	August 4 1999

- Enable using the same format as use printf routine, for set %x
  and %y parameters. If x or y values is floating numbers your

lib/GD/Graph/Map.pm  view on Meta::CPAN

@EXPORT_OK = qw(set imagemap);
$VERSION = 1.05;

#--------------------------------------------- set defaults
my $ANGLE_OFFSET = 90;
my %Defaults = ( #Default Href is JavaScript code, which do nothing
                 href   => 'javascript:;',
                 lhref  => 'javascript:;',
                 hrefs  => [],
                 lhrefs => [],
                 #Default information and legend
                 info   => 'x=%x   y=%y',
                 legend => '%l',
                 #Line width for lines and linespoints graph
                 linewidth => 3,
               );

my %No_Tags = ('img_src'   => 0, 'img_usemap' => 0, 'img_ismap'   => 0,
               'img_width' => 0, 'img_height' => 0 , 'img_border' => 0);


#********************************************* PUBLIC methods of class

lib/GD/Graph/Map.pm  view on Meta::CPAN

      push @points, [$x, $y];
      $s .= "$x, @{[$y - $lw]}, ";
    }
    foreach (reverse @points)
    { my ($x, $y) = @$_;
      $s .= "$x, @{[$y + $lw]}, ";
    } #foreach
    chop $s; chop $s;
    my $href = $self->{lhrefs}->[$_ - 1];
    $href = $self->{lhref} unless defined($href);
    $href =~ s/%l/$gr->{legend}->[$_ - 1]/g;
    my $info = $self->{info};
    $info =~ s/%l/$gr->{legend}->[$_ - 1]/g;
    $s .= "\" Href=\"$href\" Title=\"$info\" Alt=\"$info\" onMouseOver=\"window.status=\'$info\'; return true;\" onMouseOut=\"window.status=\'\'; return true;\"";
    if ($self->{newWindow} and not $href =~ /javascript:/i)
    { my $s_;
      map
      { $s_ .= "$1=@{[$self->{$_}]}," if $_ =~ /window_(\w+)/i and $self->{$_};
      } keys %$self;
      chop $s_;
      $s .= " Target=\"_$name\" onClick=\"window.open(\'\', \'_$name\', \'$s_\'); return true;  \"";
    } #if
    $s .= ">\n";
  } #foreach
  $s .= $self->imagelegend($file, $data) if defined($gr->{legend});
  $s .= "</Map>\n";
  unless ($self->{noImgMarkup})
  { $s .= "<Img UseMap=#$name Src=\"$file\" border=0 Height=@{[$gr->{height}]} Width=@{[$gr->{width}]} ";
    map
   { $s .= "$1=@{[$self->{$_}]} " if $_ =~ /img_(\w+)/i and $self->{$_};
   } keys %$self;
   chop $s;
   $s .= ">\n";
  } #unless
  return $s;

lib/GD/Graph/Map.pm  view on Meta::CPAN

      { push @points, [$xp, $yp];
        $s1 .= "$xp, @{[$yp - $lw]}, ";
      } #if
      my ($l, $r, $b, $t) = $gr->marker_coordinates($xp, $yp);
      $s .= "\t<Area Shape=rect Coords=\"$l, $t, $r, $b\" ";
      my $href = ${$self->{hrefs}}[$_ - 1][$i];
      $href = $self->{href} unless defined($href);
      $href =~ s/%x/$data->[0][$i]/g; $href =~ s/%y/$data->[$_][$i]/g;
      $href = $1.(sprintf "%$2f", $data->[0][$i]).$3 if ($href =~ /(^.*)%(\.\d)x(.*&)/);
      $href = $1.(sprintf "%$2f", $data->[$_][$i]).$3 if ($href =~ /(^.*)%(\.\d)y(.*$)/);
      $href =~ s/%l/@{$gr->{legend}}->[$_ - 1]/g;
      my $info = $self->{info};
      $info =~ s/%x/$data->[0][$i]/g; $info =~ s/%y/$data->[$_][$i]/g;
      $info = $1.(sprintf "%$2f", $data->[0][$i]).$3 if ($info =~ /(^.*)%(\.\d)x(.*&)/);
      $info = $1.(sprintf "%$2f", $data->[$_][$i]).$3 if ($info =~ /(^.*)%(\.\d)y(.*$)/);
      $info =~ s/%l/@{$gr->{legend}}->[$_ - 1]/g;
      $s .= "Href=\"$href\" Title=\"$info\" Alt=\"$info\" onMouseOver=\"window.status=\'$info\'; return true;\" onMouseOut=\"window.status=\'\'; return true;\"";
      if ($self->{newWindow} and not $href =~ /javascript:/i)
      { my $s_;
        map
        { $s_ .= "$1=".$self->{$_}."," if (($_ =~ /window_(\w*)/i) and ($self->{$_} != 0))
        } keys %{$self};
        chop $s_;
        $s .= " Target=_$name";
        $s .= " onClick=\"window.open(\'\', \'_$name\', \'$s_\'); return true;\"";
      } #if
      $s .= ">\n";
    } #foreach
    if ($lines)
    { foreach (reverse @points)
      { my ($x, $y) = @$_;
        $s1 .= "$x, @{[$y + $lw]}, ";
      } #foreach
      chop $s1; chop $s1;
      my $lhref = $self->{lhrefs}->[$_ - 1];
      $lhref = $self->{lhref} unless defined($lhref);
      $lhref =~ s/%l/$gr->{legend}->[$_ - 1]/g;
      my $legend = $self->{legend};
      $legend =~ s/%l/$gr->{legend}->[$_ - 1]/g;
      $s .= "\t<Area Shape=polygon Coords=\"$s1\" Href=\"$lhref\" Title=\"$legend\" Alt=\"$legend\" onMouseOver=\"window.status=\'$legend\'; return true;\" onMouseOut=\"window.status=\'\'; return true;\"";
      if ($self->{newWindow} and not $lhref =~ /javascript:/i)
      { my $s_;
        map
        { $s_ .= "$1=@{[$self->{$_}]}," if $_ =~ /window_(\w+)/i and $self->{$_};
        } keys %$self;
        chop $s_;
        $s .= " Target=\"_$name\" onClick=\"window.open(\'\', \'_$name\', \'$s_\'); return true;  \"";
      } #if
      $s .= ">\n"; $s1 = "";
    } #if
  }
  $s .= $self->imagelegend($file, $data) if defined($gr->{legend});
  $s .= "</Map>\n";
  unless ($self->{noImgMarkup})
  { $s .= "<Img UseMap=#$name Src=\"$file\" border=0 Height=@{[$gr->{height}]} Width=@{[$gr->{width}]} ";
    map
    { $s .= "$1=".($self->{$_})." " if ($_ =~ /img_(\w*)/i) and defined($self->{$_})
    } keys %{$self};
    chop $s;
    $s .= ">\n";
  } #unless
  return $s;

lib/GD/Graph/Map.pm  view on Meta::CPAN

	$r = int($xp - $gr->{x_step}/2
	     + $_ * $gr->{x_step}/$gr->{_data}->num_sets - $bar_s);
      }
      $s .= "\t<Area Shape=rect Coords=\"";
      $s .= $value >= 0 ? "$l, $t, $r, $bottom\" " : "$l, $bottom, $r, $t\" ";
      my $href = ${$self->{hrefs}}[$_ - 1][$i];
      $href = $self->{href} unless defined($href);
      $href =~ s/%x/$data->[0][$i]/g; $href =~ s/%y/$data->[$_][$i]/g;
      $href = $1.(sprintf "%$2f", $data->[0][$i]).$3 if ($href =~ /(^.*)%(\.\d)x(.*$)/);
      $href = $1.(sprintf "%$2f", $data->[$_][$i]).$3 if ($href =~ /(^.*)%(\.\d)y(.*$)/);
      $href =~ s/%l/@{$gr->{legend}}->[$_ - 1]/g;
      my $info = $self->{info};
      $info =~ s/%x/$data->[0][$i]/g; $info =~ s/%y/$data->[$_][$i]/g;
      $info = $1.(sprintf "%$2f", $data->[0][$i]).$3 if ($info =~ /(^.*)%(\.\d)x(.*$)/);
      $info = $1.(sprintf "%$2f", $data->[$_][$i]).$3 if ($info =~ /(^.*)%(\.\d)y(.*$)/);
      $info =~ s/%l/@{$gr->{legend}}->[$_ - 1]/g;
      $s .= "Href=\"$href\" Title=\"$info\" Alt=\"$info\" onMouseOver=\"window.status=\'$info\'; return true;\" onMouseOut=\"window.status=\'\'; return true;\"";
      if ($self->{newWindow} and not $href =~ /javascript:/i)
      { my $s_;
        map
        { $s_ .= "$1=".$self->{$_}."," if (($_ =~ /window_(\w*)/i) and ($self->{$_} != 0))
        } keys %{$self};
        chop $s_;
        $s .= " Target=_$name";
        $s .= " onClick=\"window.open(\'\', \'_$name\', \'$s_\'); return true;\"";
      } #if
      $s .= ">\n";
    }
  }
  $s .= $self->imagelegend($file, $data) if defined($gr->{legend});
  $s .= "</Map>\n";
  unless ($self->{noImgMarkup})
  { $s .= "<Img UseMap=#$name Src=\"$file\" border=0 Height=@{[$gr->{height}]} Width=@{[$gr->{width}]} ";
    map
    { $s .= "$1=".($self->{$_})." " if ($_ =~ /img_(\w*)/i) and defined($self->{$_})
    } keys %{$self};
    chop $s;
    $s .= ">\n";
  } #unless
  return $s;

lib/GD/Graph/Map.pm  view on Meta::CPAN

  ($a < $ANGLE_OFFSET or $a > (360 - $ANGLE_OFFSET)) ? 1 : 0;
} #in_front

sub level_angle($) #(angle)
{ my $a = shift;
  return level_angle($a - 360) if $a > 360;
  return level_angle($a + 360) if $a < 0;
  return $a;
} #level_angle

sub imagelegend($$) #($file, \@data)
{ my $self = shift;
  my $file = shift;
  my $data = shift;
  my $gr = $self->{GDGraph};
  my $name = defined $self->{mapName} ? $self->{mapName} : time;
  my $s = '';
  my $xl = $gr->{lg_xs} + $gr->{legend_spacing};
  my $y  = $gr->{lg_ys} + $gr->{legend_spacing} - 1;
  my $i = 0;
  my $row = 1;
  my $x = $xl;
  foreach my $legend (@{$gr->{legend}})
  { $i++;
    last if $i > $gr->{_data}->num_sets;
    my $xe = $x;
    next unless defined($legend) && $legend ne "";
    my $lhref = @{$self->{lhrefs}}->[$i - 1];
    $lhref = $self->{lhref} unless defined($lhref);
    $lhref =~ s/%l/$_/g;
    $legend = $self->{legend};
    $legend =~ s/%l/$_/g;
    my $ye = $y + int($gr->{lg_el_height}/2 - $gr->{legend_marker_height}/2);
    $s .= "\t<Area Shape=rect Coords=\"$xe, $ye, ".($xe + $gr->{legend_marker_width}).", ".($ye + $gr->{legend_marker_height})."\" Href=\"$lhref\" Title=\"$legend\" Alt=\"$legend\" onMouseOver=\"window.status=\'$legend\'; return true;\" onMouseOut=\"...
    if ($self->{newWindow} and $lhref ne $self->{href}) #$xe + $gr->{legend_marker_width}
    { my $s_;
      map
      { $s_ .= "$1=".$self->{$_}."," if (($_ =~ /window_(\w*)/i) and ($self->{$_} != 0))
      } keys %{$self};
      chop $s_;
      $s .= " Target=_$name";
      $s .= " onClick=\"window.open(\'\', \'_$name\', \'$s_\'); return true;\"";
    } #if
    $s .= ">\n";
    $xe += $self->{legend_marker_width} + $self->{legend_spacing};
    $x += $gr->{lg_el_width};
    if (++$row > $gr->{lg_cols})
    { $row = 1;
      $y += $gr->{lg_el_height};
      $x = $xl;
    }
  }
  return $s;
} #imagelegend

1;

__END__

=head1 NAME

B<GD::Graph::Map> - generate HTML map text for GD::Graph diagramms.

=head1 SYNOPSIS

lib/GD/Graph/Map.pm  view on Meta::CPAN

  @data = (["1st","2nd","3rd","4th","5th","6th"],
           [    4,    2,    3,    4,    3,  3.5]);

  $PNGimage = 'Demo.png';
  open PNG, '>$pngimage';
  binmode PNG; #only for Windows like platforms
  print PNG $graph->plot(\@data)->png;
  close PNG;

Then create B<GD::Graph::Map> object. And set options using set routine, or set it
in constructor immediately. If it is necessary create hrefs and legend arrays:

  $map = new GD::Graph::Map($graph, newWindow => 1);

  $map->set(info => "%x slice contains %.1p% of %s (%x)");

Create HTML map text using the same array of data as use GD::Graph::plot routine 
and name of the your graph file:

  $HTML_map = $map->imagemap($GIFimage, \@data);

lib/GD/Graph/Map.pm  view on Meta::CPAN

Set options. See OPTIONS.

=back

=head1 OPTIONS

=over *

=item B<hrefs>, B<lhrefs>

Sets hyper reference for each data (hrefs), and for each legend (lhrefs).
Array @hrefs must the same size as arrays in @data list, otherwise null
elements of @hrefs will set to default. Similarly array @lhrefs must the same
size as the legend array. Default uses the simple JavaScript code 'javascript:;'
instead reference, which do nothing (but in the some browsers it can work incorrectly).

Example of I<@hrefs> array:

for the I<GD::Graph::pie> object:

if     @data  = ([  "1st",  "2nd",  "3rd"],
                 [      4,      2,      3]);

then   @hrefs =  ["1.htm","2.htm","3.htm"];

lib/GD/Graph/Map.pm  view on Meta::CPAN


if     @data  = ([  "1st",  "2nd",  "3rd"],
                 [      5,     12,     24],
                 [      1,      2,      5]);

then   @hrefs = (["1.htm","2.htm","3.htm"],
                 ["4.htm","5.htm","6.htm"]);

Example of I<@lhrefs> array;

if    @legend = [  'one',  'two','three'];

then  @lhrefs = ["1.htm","2.htm","3.htm"];



=item B<info>, B<legend>

Set information string for the data and for the legend. It will be displayed in the status line
of your browser. Format of this string the same for each data, but you can use special
symbols for receive individual information. Now available following symbols:
I<%x> - Will be replaced on the x values in @data (first array)
I<%y> - Will be replaced on the y values in @data (other arrays)
I<%s> - Will be replaced on the sum of all y values.
I<%l> - Will be replaced on the legend. For all objects, except the B<GD::Graph::pie> object.
I<%p> - Will be replaced on the value, which show what part from all contains this data
(in percentages).

I<%s> and I<%p> symbols can useing only in the B<GD::Graph::pie> object. I<%l> symbol
vice versa available for all objects, except the B<GD::Graph::pie> object. And I<%x>, I<%y>
symbols available for all objects, except the B<GD::Graph::lines> and the B<GD::Graph::area>
objects.
For the numerical parameters (%x, %y, %s and %p) you can use special format
(the same as uses sprintf routine) for round data: %.d{x|y|p|s}, where 'd' is a digit
from 0 to 9.
For example %.0p or %.3x. It is desirable uses if %x, %y, %s or %p is the floating numbers.
Default is 'x=%x y=%y' for info, and '%l' for legend.

=item B<img_*>

You can set any attribute in the IMG tag (except UseMap, Src, Width, Height and Border,
they will be set automatically) use set routine: set(img_option => value), where 'option'
is the IMG attribute. For instance: routine set(img_Alt => 'Example') will include Alt='Example'
in the IMG tag.

=item B<newWindow>, B<window_*>

samples/sample12.pl  view on Meta::CPAN

	x_label => 'X Label',
	y_label => 'Y label',
	title => 'Two data sets',
	long_ticks => 1,
	y_max_value => 40,
	y_tick_number => 8,
	y_label_skip => 2,
	bar_spacing => 4,
);

$my_graph->set_legend( 'Data set 1', 'Data set 2' );

open PNG, ">sample12.png";
binmode PNG; #only for Windows like platforms
print PNG $my_graph->plot(\@data)->png;
close PNG;

$map = new GD::Graph::Map($my_graph, info => '%l:  x=%x    y=%y');

open HTML, ">sample12.html";
print HTML "<HTML><BODY BGCOLOR=white>\n".

samples/sample13.pl  view on Meta::CPAN

	x_label => 'X Label',
	y_label => 'Y label',
	title => 'Bars in front of each other',
	y_tick_number => 8,
	y_label_skip => 2,
	overwrite => 1,
	bar_spacing => 8,
	shadow_depth => 4,
);

$my_graph->set_legend('Data set 1', 'Data set 2');

open PNG, ">sample13.png";
binmode PNG; #only for Windows like platforms
print PNG ($my_graph->plot(\@data))->png;
close PNG;

$map = new GD::Graph::Map($my_graph, info => '%l:  x=%x   y=%y');

open HTML, ">sample13.html";
print HTML "<HTML><BODY BGCOLOR=white>\n".

samples/sample14.pl  view on Meta::CPAN

	x_label => 'x label',
	y1_label => 'y1 label',
	y2_label => 'y2 label',
	title => 'Using two axes',
	y1_max_value => 40,
	y2_max_value => 8,
	y_tick_number => 8,
	y_label_skip => 2,
	long_ticks => 1,
	two_axes => 1,
	legend_placement => 'RT',
	x_labels_vertical => 1,
	x_label_position => 1/2,

	fgclr => 'white',
	boxclr => 'dblue',
	accentclr => 'dblue',
	dclrs => [qw(lgreen lred)],

	bar_spacing => 2,

	logo_position => 'BR',

	transparent => 0,

	l_margin => 10,
	b_margin => 10,
	r_margin => 10,
	t_margin => 10,
);

$my_graph->set_legend( 'left axis', 'right axis');

open PNG, ">sample14.png";
binmode PNG; #only for Windows like platforms
print PNG $my_graph->plot(\@data)->png;
close PNG;

$map = new GD::Graph::Map($my_graph, info => '%l:  x=%x   y=%y');

open HTML, ">sample14.html";
print HTML "<HTML><BODY BGCOLOR=white>\n".

samples/sample15.pl  view on Meta::CPAN

	y_label => 'Y label',
	title => 'Stacked Bars (incremental)',
	y_max_value => 50,
	y_tick_number => 10,
	y_label_skip => 2,
	cumulate => 2,
	borderclrs => $my_graph->{dclrs},
	bar_spacing => 4,
);

$my_graph->set_legend( qw(offset increment more));

open PNG, ">sample15.png";
binmode PNG; #only for Windows like platforms
print PNG $my_graph->plot(\@data)->png;
close PNG;

$map = new GD::Graph::Map($my_graph, info => '%l:  x=%x   y=%y');

open HTML, ">sample15.html";
print HTML "<HTML><BODY BGCOLOR=white>\n".

samples/sample16.pl  view on Meta::CPAN

	x_label => 'Day',
	y_label => 'AUD',
	title => 'Credits and Debits',
	y_max_value => 35,
	y_min_value => -15,
	y_tick_number => 10,
	y_label_skip => 2,
	overwrite => 1, 
	dclrs => [ qw( green lred ) ],
	axislabelclr => 'black',
	legend_placement => 'RB',
	zero_axis_only => 0,
	y_number_format => \&y_format,
	x_label_position => 1/2,
);

my $refit = 4;

sub y_format
{
	my $value = shift;

samples/sample16.pl  view on Meta::CPAN

	if ($value >= 0)
	{
		$ret = sprintf("\$%3d", $value * $refit);
	}
	else
	{
		$ret = sprintf("-\$%3d", abs($value) * $refit);
	}
}

$my_graph->set_legend( 'credits', 'debets' );

open PNG, ">sample16.png";
binmode PNG; #only for Windows like platforms
print PNG $my_graph->plot(\@data)->png;
close PNG;

$map = new GD::Graph::Map($my_graph, info => '%l:  x=%x  y=%y');

open HTML, ">sample16.html";
print HTML "<HTML><BODY BGCOLOR=white>\n".

samples/sample31.pl  view on Meta::CPAN


$my_graph = new GD::Graph::points();

$my_graph->set( 
	x_label => 'X Label',
	y_label => 'Y label',
	title => 'A Points Graph',
	y_max_value => 40,
	y_tick_number => 8,
	y_label_skip => 2, 
	legend_placement => 'RC',
	long_ticks => 1,
	marker_size => 6,
	markers => [ 1, 7, 5 ],
);

$my_graph->set_legend( qw( one two ) );

open PNG, ">sample31.png";
binmode PNG; #only for Windows like platforms
print PNG $my_graph->plot(\@data)->png;
close PNG;

$map = new GD::Graph::Map($my_graph, info => '%l:  x=%x   y=%y');

open HTML, ">sample31.html";
print HTML "<HTML><BODY BGCOLOR=white>\n".

samples/sample41.pl  view on Meta::CPAN

	y_label => 'Y label',
	title => 'A Lines and Points Graph',
	y_max_value => 80,
	y_tick_number => 6,
	y_label_skip => 2,
	y_long_ticks => 1,
	x_tick_length => 2,
	markers => [ 1, 5 ],
);

$my_graph->set_legend( 'data set 1', 'data set 2' );

open PNG, ">sample41.png";
binmode PNG; #only for Windows like platforms
print PNG $my_graph->plot(\@data)->png;
close PNG;

$map = new GD::Graph::Map($my_graph, info => '%l');

open HTML, ">sample41.html";
print HTML "<HTML><BODY BGCOLOR=white>\n".

samples/sample52.pl  view on Meta::CPAN

	box_axis => 0,
	line_width => 3,
	zero_axis_only => 1,
	x_label_position => 1,
	y_label_position => 1,

	x_label_skip => 3,
	x_tick_offset => 2,
);

$my_graph->set_legend("Us", "Them", "Others");

open PNG, ">sample52.png";
binmode PNG; #only for Windows like platforms
print PNG $my_graph->plot(\@data)->png;
close PNG;

$map = new GD::Graph::Map($my_graph, info => '%l');

open HTML, ">sample52.html";
print HTML "<HTML><BODY BGCOLOR=white>\n".



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