App-Widget-ChartDirector
view release on metacpan or search on metacpan
lib/App/Widget/ChartDirector.pm view on Meta::CPAN
my $spec_path = $spec->{spec_path};
if (open(App::Widget::ChartDirector::FILE, "> $spec_path")) {
$self->write_graph_spec(\*App::Widget::ChartDirector::FILE, $spec);
close(App::Widget::ChartDirector::FILE);
$html .= "<img src=\"$spec->{cgi_url}\"";
$html .= " height=\"$spec->{height}\"" if ($spec->{height});
$html .= " width=\"$spec->{width}\"" if ($spec->{width});
$html .= ">\n";
}
else {
$html .= "[Error creating graph spec $spec_path: $!]";
}
}
else { # generate graph image now
eval {
$html = $self->write_graph_image($spec);
};
if ($@) {
if ($@ =~ /(no columns in graph)/) {
$html .= "[Error creating graph image, $1.]";
}
else {
$html .= "[Error creating graph image $spec->{image_path}: $@]";
}
}
}
if ($self->{debug}) {
$html .= "<pre>\n";
foreach my $key (sort keys %$spec) {
$html .= "$key = ";
if (ref($spec->{$key}) eq "ARRAY") {
$html .= "[ " . join(", ", @{$spec->{$key}}) . " ]\n";
}
else {
$html .= "$spec->{$key}\n";
}
}
$html .= "</pre>\n";
}
&App::sub_exit($html) if ($App::trace);
$html;
}
my $serial = 1;
sub create_graph_spec {
&App::sub_entry if ($App::trace);
my ($self) = @_;
my $context = $self->{context};
my $options = $context->{options};
my $spec_tempdir = $options->{tempdir} || "$options->{prefix}/tmp";
my $image_tempdir = "$options->{html_dir}/temp";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon++;
$year += 1900;
my $datetime = sprintf("%04d%02d%02d-%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec);
my $unique_tag = $datetime . "-" . $$ . "-" . $serial;
my $spec_file = "$unique_tag.graph";
my $spec_path = "$spec_tempdir/$spec_file";
my $image_file = "$unique_tag.png";
my $image_path = "$image_tempdir/$image_file";
while (-f $spec_path || -f $image_path) {
$serial++;
$unique_tag = $datetime . "-" . $$ . "-" . $serial;
$spec_file = "$unique_tag.graph";
$spec_path = "$spec_tempdir/$spec_file";
$image_file = "$unique_tag.png";
$image_path = "$image_tempdir/$image_file";
}
my %spec = %$self;
delete $spec{context};
$spec{graphtype} ||= "bar";
if ($spec{graphtype} eq "area") {
$spec{graphtype} = "line";
$spec{area} = 1;
}
elsif ($spec{graphtype} eq "stacked_bar") {
$spec{graphtype} = "bar";
$spec{stacked} = 1;
}
$spec{height} ||= 280;
$spec{width} ||= 360;
$spec{spec_file} = $spec_file;
$spec{spec_path} = $spec_path;
$spec{image_file} = $image_file;
$spec{image_path} = $image_path;
$spec{image_url} = "$options->{html_url_dir}/temp/$image_file";
$spec{cgi_url} = "$options->{script_url_dir}/app-cdgraph/$spec_file";
&App::sub_exit(\%spec) if ($App::trace);
return(\%spec);
}
sub write_graph_spec {
&App::sub_entry if ($App::trace);
my ($self, $fh, $spec) = @_;
foreach my $key (%$spec) {
if (!ref($spec->{$key})) {
print $fh "\$data{$key} = [ ";
foreach my $value (@{$spec->{$key}}) {
print $fh "\"$spec->{$key}\", ";
}
print $fh "]\n";
}
else {
print $fh "\$data{$key} = \"$spec->{$key}\";\n";
}
}
&App::sub_exit() if ($App::trace);
}
sub read_graph_spec {
&App::sub_entry if ($App::trace);
my ($self, $fh, $spec) = @_;
foreach my $key (%$spec) {
if (!ref($spec->{$key})) {
print $fh "\$data{$key} = [ ";
foreach my $value (@{$spec->{$key}}) {
print $fh "\"$spec->{$key}\", ";
}
print $fh "]\n";
}
else {
print $fh "\$data{$key} = \"$spec->{$key}\";\n";
}
}
&App::sub_exit() if ($App::trace);
}
( run in 0.543 second using v1.01-cache-2.11-cpan-df04353d9ac )