【发布时间】:2013-11-07 22:12:23
【问题描述】:
我想在我的 Web 应用程序中添加一个 SVG 条形图。 我必须使用 Catalyst Framework 和 Template::Toolkit 视图。 在搜索cpan时,我找到了this module。
我按照上面网页中的示例说明创建了一个简单的图表。 不幸的是,我的应用程序中没有呈现图表。
首先我创建了视图类
./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 缺乏经验。非常感谢任何帮助。谢谢
【问题讨论】:
-
你从哪里得到
myapp::View::Chart的内容?这不是myapp_create.pl脚本为你写的,它不是一个View 类,它甚至不是一个类。而且你永远不会在 Catalyst 中使用print。 -
你说得对,我已将this page 中的代码添加到myapp_create.pl 创建的文件中。感谢您的帮助。任何指导都会非常有帮助