我想在我的 Web 应用程序中添加一个 SVG 条形图。我必须使用 Catalyst Framework 和 Template::Toolkit 视图。在搜索 cpan 时,我发现了这个模块。
我按照上面网页中的示例说明创建了一个简单的图表。不幸的是,我的应用程序中没有呈现图表。
首先我创建了视图类
./script/myapp_create.pl view Chart SVG::TT::Graph
并在其中添加了这段代码
package myapp::View::Chart;
use Moose;
use namespace::autoclean;
use SVG::TT::Graph::BarHorizontal;
my @fields = qw(Jan Feb Mar);
my @data_sales_02 = qw(12 45 21);
my $graph = SVG::TT::Graph::BarHorizontal->new({
'height' => '500',
'width' => '300',
'fields' => \@fields,
});
$graph->add_data({
'data' => \@data_sales_02,
'title' => 'Sales 2002',
});
print "Content-type: image/svg+xml\n\n";
print $graph->burn();
然后我在我的配置中设置图表首选项
<View::Chart>
format png
<chart_conf>
style_sheet /home/john/myapp/root/static/css/style2.css
show_graph_title 1
</chart_conf>
</View::Chart>
最后我在我的控制器中创建了以下子例程
sub getchart :Local :Args(0){
my ( $self, $c ) = @_;
$c->stash->{chart_title} = 'Sales data'; # optional
$c->stash->{chart_type} = 'BarHorizontal'; # or Pie/Line/BarHorizontal
$c->stash->{chart_conf} = {
height => 400,
width => 600
};
$c->stash->{chart_fields} = [ qw(Jan Feb March) ];
$c->stash->{chart_data} = [ 12, 45, 21];
$c->forward($c->view('Chart'));
}
显然我错过了一些基本的东西,但我对 Catalyst 缺乏经验。非常感谢任何帮助。谢谢