5 include('elements/report.html',
7 'title' => 'Page title',
9 'data' => [ \@item1 \@item2 ... ],
11 #these run parallel to items, and can be given as hashes
12 'row_labels' => \@row_labels, #required
13 'colors' => \@colors, #required
14 'graph_labels' => \@graph_labels, #defaults to row_labels
16 'links' => \@links, #optional
17 'no_graph' => \@no_graph, #optional
19 #these run parallel to the elements of each @item
20 'col_labels' => \@col_labels, #required
21 'axis_labels' => \@axis_labels, #defaults to col_labels
25 'graph_type' => 'LinesPoints',
27 'sprintf' => '%u', #sprintf format, overrides default %.2f
31 About @links: Each element must be an arrayref, corresponding to an element of
32 @items. Within the array, the first element is a URL prefix, and the rest
33 are suffixes corresponding to data elements. These will be joined without
34 any delimiter and linked from the elements in @data.
37 % if ( $cgi->param('_type') =~ /^(csv)$/ ) {
39 % #http_header('Content-Type' => 'text/comma-separated-values' ); #IE chokes
40 % #http_header('Content-Type' => 'text/plain' );
41 % http_header('Content-Type' => 'text/csv');
42 % http_header('Content-Disposition' => "attachment;filename=$filename.csv");
44 % my $csv = new Text::CSV_XS { 'always_quote' => 1,
45 % 'eol' => "\n", #"\015\012", #"\012"
48 % $csv->combine('', @col_labels, $opt{'nototal'} ? () : 'Total');
52 % my @bottom_total = ();
53 % foreach ( @items ) {
58 % shift( @row_labels ),
59 % map { $total += $_; $bottom_total[$col++] += $_; sprintf($sprintf, $_); }
60 % ( @{ shift( @data ) } ),
61 % ( $opt{'nototal'} ? () : sprintf($sprintf, $total) ),
63 % unless ( $opt{'nototal'} ) {
64 % $bottom_total[$col++] += $total;
70 % if ( $opt{'bottom_total'} ) {
73 % map { sprintf($sprintf, $_) } @bottom_total,
80 % } elsif ( $cgi->param('_type') =~ /(xls)$/ ) {
81 % #false laziness w/ search/elements/search-xls
82 % my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
83 % $filename .= $format->{extension};
85 % http_header('Content-Type' => $format->{mime_type} );
86 % http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
89 % my $XLS = new IO::Scalar \$output;
90 % my $workbook = $format->{class}->new($XLS)
91 % or die "Error opening .xls file: $!";
93 % my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
97 % foreach ('', @col_labels, ($opt{'nototal'} ? () : 'Total') ) {
99 % $worksheet->write($r, $c++, $header)
102 % my @bottom_total = ();
103 % foreach ( @items ) {
107 % $worksheet->write( $r, $c++, shift( @row_labels ) );
108 % foreach ( @{ shift( @data ) } ) {
110 % $bottom_total[$c-1] += $_;
111 % $worksheet->write($r, $c++, sprintf($sprintf, $_) );
113 % unless ( $opt{'nototal'} ) {
114 % $bottom_total[$c-1] += $total;
115 % $worksheet->write($r, $c++, sprintf($sprintf, $total) );
120 % if ( $opt{'bottom_total'} ) {
122 % $worksheet->write($r, $c++, 'Total');
123 % $worksheet->write($r, $c++, sprintf($sprintf, $_)) foreach @bottom_total;
126 % $workbook->close();# or die "Error creating .xls file: $!";
128 % http_header('Content-Length' => length($output) );
131 % } elsif ( $cgi->param('_type') eq 'png' ) {
132 % # delete any items that shouldn't be on the graph
133 % if ( my $no_graph = $opt{'no_graph'} ) {
135 % while (@$no_graph) {
136 % if ( shift @$no_graph ) {
137 % splice @data, $i, 1;
138 % splice @{$opt{'graph_labels'}}, $i, 1;
139 % splice @{$opt{'colors'}}, $i, 1;
140 % $i--; # because everything is shifted down
145 % my $graph_type = 'LinesPoints';
146 % if ( $opt{'graph_type'} =~ /^(LinesPoints|Mountain|Bars)$/ ) {
149 % my $class = "Chart::$graph_type";
151 % my $chart = $class->new(976,384);
152 % # the chart area itself is 900 pixels wide, and the date labels are ~60 each.
153 % # staggered, we can fit about 28 of them.
154 % # they're about 12 pixels high, so vertically, we can fit about 60 (allowing
155 % # space for them to be readable).
156 % # after that we have to start skipping labels. also remove the dots, since
157 % # they're just a blob at that point.
158 % my $num_labels = scalar(@{ $opt{axis_labels} });
159 % my %chart_opt = %{ $opt{chart_options} || {} };
160 % if ( $num_labels > 28 ) {
161 % $chart_opt{x_ticks} = 'vertical';
162 % if ( $num_labels > 60 ) {
163 % $chart_opt{skip_x_ticks} = int($num_labels / 60) + 1;
164 % $chart_opt{pt_size} = 1;
170 % 'legend' => 'bottom',
172 % map { my $color = $_;
174 % [ map hex($_), unpack 'a2a2a2', $color ]
176 % @{ $opt{'colors'} }
178 % 'grey_background' => 'white',
179 % 'background' => [ 0xe8, 0xe8, 0xe8 ], #grey
181 % 'legend_labels' => $opt{'graph_labels'},
186 % http_header('Content-Type' => 'image/png' );
187 % http_header('Cache-Control' => 'no-cache' );
189 % $chart->_set_colors();
191 <% $chart->scalar_png([ $opt{'axis_labels'}, @data ]) %>
194 % # image and download links should use the cached data
195 % # just directly reference this component
196 % my $myself = $p.'graph/elements/report.html?session='.$session;
198 <% include('/elements/header.html', $opt{'title'} ) %>
199 % unless ( $opt{'graph_type'} eq 'none' ) {
201 <IMG SRC="<% "$myself;_type=png" %>" WIDTH="976" HEIGHT="384"
202 STYLE="page-break-after:always;">
204 <P ALIGN="right" CLASS="noprint">
206 % unless ( $opt{'disable_download'} ) {
207 Download full results<BR>
208 as <A HREF="<% "$myself;_type=xls" %>">Excel spreadsheet</A><BR>
209 as <A HREF="<% "$myself;_type=csv" %>">CSV file</A></P>
213 %# indexed by item, then by entry (the element indices of @{$data[$i]}).
216 % my $num_entries = scalar(@col_labels);
217 % my $num_items = scalar(@items);
218 % $cell[0] = ['']; #top left corner
219 % foreach my $column ( @col_labels ) {
220 % $column =~ s/ /\<BR\>/;
221 % push @{$cell[0]}, $column;
223 % if ( ! $opt{'nototal'} ) {
225 % push @{$cell[0]}, emt('Total');
228 % # i for item, e for entry
230 % foreach my $row ( @items ) {
232 % my $color = shift @{ $opt{'colors'} };
233 % push @styles, ".i$i { text-align: right; color: #$color; }";
234 % #create the data row
235 % my $links = shift @{$opt{'links'}} || [''];
236 % my $link_prefix = shift @$links;
237 % $link_prefix = '<A CLASS="cell" HREF="'.$link_prefix if $link_prefix;
238 % my $label = shift @row_labels;
239 % $cell[$i] = [ $label ];
241 % my $data_row = $data[$i-1];
242 %# my $data_row = shift @data;
243 % if ( ! $opt{'nototal'} ) {
244 % push @$data_row, sum(@$data_row);
246 % foreach ( @$data_row ) {
248 % $entry = $money_char . sprintf($sprintf, $entry);
249 % $entry = $link_prefix . shift(@$links) . "\">$entry</A>" if $link_prefix;
250 % push @{$cell[$i]}, $entry;
254 % if ( $opt{'bottom_total'} ) {
255 % # it's an extra item
257 % push @styles, ".i$i { text-align: right; background-color: #f5f6be; }";
258 % my $links = $opt{'bottom_link'} || [];
259 % my $link_prefix = shift @$links;
260 % $link_prefix = '<A CLASS="cell" HREF="'.$link_prefix if $link_prefix;
261 % $cell[$i] = [ emt('Total') ];
262 % for (my $e = 0; $e < $num_entries + 1; $e++) {
263 % my $entry = sum(map { $_->[$e] } @data);
264 % $entry = $money_char . sprintf($sprintf, $entry);
265 % $entry = $link_prefix . shift(@$links) . "\">$entry</A>" if $link_prefix;
266 % push @{$cell[$i]}, $entry;
270 <STYLE type="text/css">
272 color: inherit !important;
277 <% join("\n", @styles) %>
284 % if ( ! $opt{'nototal'} ) {
285 .e<% $num_entries %> {
287 background-color: #f5f6be;
297 <% include('/elements/table.html', 'f8f8f8') %>
298 % if ( $opt{'transpose'} ) {
299 % for ( my $e = 0; $e < $num_entries + 1; $e++ ) {
301 % for ( my $i = 0; $i < $num_items + 1; $i++ ) {
302 <TD CLASS="<%"cell i$i e$e"%>"><% $cell[$i][$e] %></TD>
307 % } else { #!transpose
309 % for (my $i = 0; $i < $num_items + 1; $i++) {
311 % for (my $e = 0; $e < $num_entries + 1; $e++) {
312 <TD CLASS="<%"cell i$i e$e"%>"><% $cell[$i][$e] %></TD>
319 <% include('/elements/footer.html') %>
325 # load from cache if possible, to avoid recalculating
326 if ( $cgi->param('session') =~ /^(\d+)$/ ) {
328 %opt = %{ $m->cache->get($session) };
331 $session = sprintf("%010d%06d", time, int(rand(1000000)));
332 $m->cache->set($session, \%opt, '1h');
335 my $sprintf = $opt{'sprintf'} || '%.2f';
337 my $conf = new FS::Conf;
338 my $money_char = $opt{'disable_money'} ? '' : $conf->config('money_char');
340 my @items = @{ $opt{'items'} };
342 foreach my $other (qw( col_labels row_labels graph_labels axis_labels colors links )) {
343 if ( ref($opt{$other}) eq 'HASH' ) {
344 $opt{$other} = [ map $opt{$other}{$_}, @items ];
348 my @col_labels = @{$opt{'col_labels'}};
349 my @row_labels = @{$opt{'row_labels'}};
350 my @data = @{$opt{'data'}};
352 $opt{'axis_labels'} ||= $opt{'col_labels'};
353 $opt{'graph_labels'} ||= $opt{'row_labels'};
355 my $filename = $cgi->url(-relative => 1);
356 $filename =~ s/\.(cgi|html)$//;