Hi,
Basically, I want to dynamically generate a graph using TT and GD. Not a file
to a /tmp folder, rather prints right to the browser.
I have searched through the newsgroup, on the web, read the docs, asked
several TT people here, as well as, tried a number of coding solutions.
However, I have not been able to resolve this. I even found an e-mail from
last year on the newsgroup that had a suggestion (pasted below).
I can generate the graph by printing directly from Perl:
print "Content-type: image/png\n\n";
print $graph->plot($data)->png;
This works just fine, but I was hoping to use TT to do this. TT does have a
plugin (Template::Plugin::GD::Graph::lines) which I tried but instead of the
image being printed to the screen, it gets printed in text format.
I call TT in this way:
my $template = Template->new();
print "Content-type: text/html\n\n";
$template->process('UserPromosOutput.tpl',$vars);
The UserPromosOutput.tpl is my template which has this (the content type was a
suggestion from the newsgroup last year):
Content-Type: image/png
[% FILTER null;
USE g = GD.Graph.lines(300,200);
x = [1, 2, 3, 4];
y = [5, 4, 2, 3];
g.set(
x_label => 'X Label',
y_label => 'Y label',
title => 'Title'
);
g.plot([x, y]).png | stdout(1);
END;
-%]
The result is the expected regular html, but at the top, the png in text
format.
Thanks for any help you can provide.
Here's that e-mail from last year:
-----------------------------e-mail-begins
At 05:23 PM 7/31/2001 -0400, Jeff Boes wrote:
>Sorry if this is a near-FAQ, but I haven't found it:
>
>Does anyone have a short example of how to embed a Plugin GD-generated
>graphic (say, a pie chart) in an HTML document?
Sorry about my delay in replying; I'm on vacation this week.
I don't have a self-contained example, but here's a quick set of notes.
Like any image, you include it with an <img src=...> tag. The parent
document could be static html, batch built via TT2 using ttree, or
dynamic via CGI, Apache::Template etc.
The image itself (the thing pointed to by the src attribute in the img tag)
also could be batch built via TT2 or dynamic. In the static case, the
example in Template::Plugin::GD::graph::pie shows you how to make a pie
chart with PNG output. Simply put the output from TT2 into a file with
a png extension where Apache can find it.
In the dynamic case (eg: using CGI or Apache::Template) you can again
use the example in Template::Plugin::GD::graph::pie, but you need to
precede the output by a Content-Type and two newlines, which you can
produce in perl prior to calling TT2 process, or as part of the template
itself, eg:
Content-Type: image/png
[% FILTER null; ..... -%]
You should test that the image itself renders correctly by plugging
it's URL into a browser. After that, the <img src=...> tag in the
parent document should then render correctly too.
Hope that's enough to get you up and running.
Craig
-----------------------------e-mail-ends
_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://www.template-toolkit.org/mailman/listinfo/templates