Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b30b38df

Von Bernd Bleßmann vor mehr als 1 Jahr hinzugefügt

  • ID b30b38df1b66095370ca7c9ab7f178abe5be1469
  • Vorgänger 28de1c46
  • Nachfolger 7059bbcd

Reports als Chart exportieren können

Unterschiede anzeigen:

SL/ReportGenerator.pm
10 10
use strict;
11 11
use SL::Helper::GlAttachments qw(append_gl_pdf_attachments);
12 12
use SL::Helper::CreatePDF     qw(merge_pdfs);
13
use SL::JSON qw(to_json);
13 14

  
14 15
# Cause locales.pl to parse these files:
15 16
# parse_html_template('report_generator/html_report')
......
29 30
    'controller_class   '   => '',
30 31
    'allow_pdf_export'      => 1,
31 32
    'allow_csv_export'      => 1,
33
    'allow_chart_export'    => 1,
32 34
    'html_template'         => 'report_generator/html_report',
33 35
    'pdf_export'            => {
34 36
      'paper_size'          => 'a4',
......
52 54
      'headers'             => 1,
53 55
      'encoding'            => 'UTF-8',
54 56
    },
57
    'chart_export'          => {
58
      'assignment_x'        => 'x',
59
      'assignment_y'        => 'y1',
60
    },
55 61
  };
56 62
  $self->{export}   = {
57 63
    'nextsub'       => '',
......
168 174
      $self->{options}->{pdf_export}->{$_} = $value->{$_} for keys %{ $value };
169 175
    } elsif ($key eq 'csv_export') {
170 176
      $self->{options}->{csv_export}->{$_} = $value->{$_} for keys %{ $value };
177
    } elsif ($key eq 'chart_export') {
178
      $self->{options}->{chart_export}->{$_} = $value->{$_} for keys %{ $value };
171 179
    } else {
172 180
      $self->{options}->{$key} = $value;
173 181
    }
......
185 193
    $self->{options}->{$key} = $form->{$full_key} if (defined $form->{$full_key});
186 194
  }
187 195

  
188
  foreach my $format (qw(pdf csv)) {
196
  foreach my $format (qw(pdf csv chart)) {
189 197
    my $opts = $self->{options}->{"${format}_export"};
190 198
    foreach my $key (keys %{ $opts }) {
191 199
      my $full_key = "report_generator_${format}_options_${key}";
......
255 263
  } elsif ($format eq 'pdf') {
256 264
    $self->generate_pdf_content();
257 265

  
266
  } elsif ($format eq 'chart') {
267
    $self->generate_chart_content();
268

  
258 269
  } else {
259
    $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF)');
270
    $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF, Chart)');
260 271
  }
261 272
}
262 273

  
......
401 412
    'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
402 413
    'ALLOW_PDF_EXPORT'     => $allow_pdf_export,
403 414
    'ALLOW_CSV_EXPORT'     => $opts->{allow_csv_export},
404
    'SHOW_EXPORT_BUTTONS'  => ($allow_pdf_export || $opts->{allow_csv_export}) && $self->{data_present},
415
    'ALLOW_CHART_EXPORT'   => $opts->{allow_chart_export},
416
    'SHOW_EXPORT_BUTTONS'  => ($allow_pdf_export || $opts->{allow_csv_export} || $opts->{allow_chart_export}) && $self->{data_present},
405 417
    'HEADER_ROWS'          => $header_rows,
406 418
    'NUM_COLUMNS'          => scalar @column_headers,
407 419
    'ROWS'                 => \@rows,
......
421 433
  my ($self, $variables, %params) = @_;
422 434

  
423 435
  my @actions;
424
  foreach my $type (qw(pdf csv)) {
436
  foreach my $type (qw(pdf csv chart)) {
425 437
    next unless $variables->{"ALLOW_" . uc($type) . "_EXPORT"};
426 438

  
427 439
    my $key   = $variables->{CONTROLLER_DISPATCH} ? 'action' : 'report_generator_dispatch_to';
......
429 441
    $value    = $variables->{CONTROLLER_DISPATCH} . "/${value}" if $variables->{CONTROLLER_DISPATCH};
430 442

  
431 443
    push @actions, action => [
432
      $type eq 'pdf' ? $::locale->text('PDF export') : $::locale->text('CSV export'),
444
      $type eq 'pdf' ? $::locale->text('PDF export') : $type eq 'csv' ? $::locale->text('CSV export') : $::locale->text('Chart export'),
433 445
      submit => [ '#report_generator_form', {(
434 446
            $key => $value,
435 447
            defined $params{action_bar_additional_submit_values}
......
471 483
  $params{action_bar} //= 1;
472 484

  
473 485
  my $variables = $self->prepare_html_content(%params);
474

  
475 486
  $self->setup_action_bar($variables, %params) if $params{action_bar};
476 487

  
477 488
  my $stuff  = $self->{form}->parse_html_template($self->{options}->{html_template}, $variables);
......
858 869
  }
859 870
}
860 871

  
872
sub generate_chart_content {
873
  my ($self, %params) = @_;
874

  
875
  $params{action_bar} //= 1;
876

  
877
  my $opts            = $self->{options};
878

  
879
  my $assignment_x = $opts->{chart_export}->{assignment_x};
880
  my $assignment_y = $opts->{chart_export}->{assignment_y};
881

  
882
  my @data_x;
883
  my @data_y;
884
  foreach my $row_set (@{ $self->{data} }) {
885
    next if ('ARRAY' ne ref $row_set);
886
    foreach my $row (@{ $row_set }) {
887
      my $x = $row->{$assignment_x}->{data}->[0];
888
      my $y = $row->{$assignment_y}->{data}->[0];
889
      if ($x) {
890
        push @data_x, $x;
891
        push @data_y, $y//0;
892
      }
893
    }
894
  }
895

  
896
  my $variables = {
897
    'TITLE'                => $opts->{title},
898
    'TOP_INFO_TEXT'        => $self->html_format($opts->{top_info_text}),
899
    'RAW_TOP_INFO_TEXT'    => $opts->{raw_top_info_text},
900
    'BOTTOM_INFO_TEXT'     => $self->html_format($opts->{bottom_info_text}),
901
    'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
902
    'EXPORT_VARIABLE_LIST' => join(' ', @{ $self->{export}->{variable_list} }),
903
    'EXPORT_NEXTSUB'       => $self->{export}->{nextsub},
904
    'DATA_PRESENT'         => $self->{data_present},
905
    'CONTROLLER_DISPATCH'  => $opts->{controller_class},
906
    'TABLE_CLASS'          => $opts->{table_class},
907
    'SKIP_BUTTONS'         => !!$params{action_bar},
908
  };
909

  
910
  $::request->layout->add_javascripts('chart.js', 'kivi.ChartReport.js');
911

  
912
  $::form->header;
913
  print $::form->parse_html_template('report_generator/chart_report',
914
                                      {
915
                                        data_x => to_json(\@data_x),
916
                                        data_y => to_json(\@data_y),
917
                                        label_x => $assignment_x,
918
                                        label_y => $assignment_y,
919
                                        %$variables,
920
                                      }
921
  );
922
}
923

  
861 924
sub check_for_pdf_api {
862 925
  return eval { require PDF::API2; 1; } ? 1 : 0;
863 926
}

Auch abrufbar als: Unified diff