SVG-TT-Graph
view release on metacpan or search on metacpan
lib/SVG/TT/Graph.pm view on Meta::CPAN
'data' => \%new_data,
);
$store{'title'} = $conf->{'title'} if defined $conf->{'title'};
push (@{$self->{'data'}},\%store);
return 1;
}
return undef;
}
=head2 clear_data()
my $graph->clear_data();
This method removes all data from the object so that you can
reuse it to create a new graph but with the same config options.
=cut
sub clear_data {
my $self = shift;
my @data;
$self->{'data'} = \@data;
}
=head2 get_template()
print $graph->get_template();
This method returns the TT template used for making the graph.
=cut
sub get_template {
my $self = shift;
# Template filehandle
my $template_fh_sr = $self->_get_template_fh_sr();
croak ref($self) . ' must have a template' if not $template_fh_sr;
my $template_fh = $$template_fh_sr;
# Read in TT template
my $start = tell $template_fh;
my $template = '';
while(<$template_fh>) {
chomp;
$template .= $_ . "\n";
}
# This method may be used again, so return to start of filehandle
seek $template_fh, $start, 0;
return $template;
}
sub _get_template_fh_sr {
my ($self) = @_;
my $ns_ref = \%main::;
for my $node ( split m<::>, ref $self ) {
$ns_ref = $ns_ref->{"${node}::"};
}
return *{$ns_ref->{'TEMPLATE_FH'}}{'SCALAR'};
}
=head2 burn()
print $graph->burn();
This method processes the template with the data and
config which has been set and returns the resulting SVG.
This method will croak unless at least one data set has
been added to the graph object.
=cut
sub burn {
my $self = shift;
# Check we have at least one data value
croak "No data available"
unless scalar(@{$self->{'data'}}) > 0;
# perform any calculations prior to burn
$self->calculations() if $self->can('calculations');
my $template = $self->get_template();
my %vals = (
'data' => $self->{'data'}, # the data
'config' => $self->{'config'}, # the configuration
'calc' => $self->{'calc'}, # the calculated values
'sin' => \&_sin_it,
'cos' => \&_cos_it,
'predefined_color' => \&_predefined_color,
'random_color' => \&_random_color,
);
# euu - hack!! - maybe should just be a method
$self->{sin} = \&_sin_it;
$self->{cos} = \&_cos_it;
# set up TT object
my %config = (
POST_CHOMP => 1,
INCLUDE_PATH => '/',
#STRICT => 1, # we should probably enable this for strict checking
#DEBUG => 'undef', # TT warnings on, useful for debugging, finding undef values
);
my $tt = Template->new( \%config );
my $file;
my $template_response = $tt->process( \$template, \%vals, \$file );
if($tt->error()) {
croak "Template error: " . $tt->error . "\n" if $tt->error;
}
# tidy SVG if required
if ($self->tidy()) {
( run in 2.116 seconds using v1.01-cache-2.11-cpan-71847e10f99 )