| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package AlignDB::ToXLSX; | 
| 2 | 6 |  |  | 6 |  | 901152 | use Moose; | 
|  | 6 |  |  |  |  | 1721159 |  | 
|  | 6 |  |  |  |  | 32 |  | 
| 3 | 6 |  |  | 6 |  | 27562 | use Carp; | 
|  | 6 |  |  |  |  | 9 |  | 
|  | 6 |  |  |  |  | 376 |  | 
| 4 | 6 |  |  | 6 |  | 3431 | use YAML::Syck; | 
|  | 6 |  |  |  |  | 8447 |  | 
|  | 6 |  |  |  |  | 292 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 6 |  |  | 6 |  | 6934 | use Excel::Writer::XLSX; | 
|  | 6 |  |  |  |  | 573000 |  | 
|  | 6 |  |  |  |  | 261 |  | 
| 7 | 6 |  |  | 6 |  | 3241 | use Statistics::Descriptive; | 
|  | 6 |  |  |  |  | 73744 |  | 
|  | 6 |  |  |  |  | 160 |  | 
| 8 | 6 |  |  | 6 |  | 2767 | use Chart::Math::Axis; | 
|  | 6 |  |  |  |  | 196427 |  | 
|  | 6 |  |  |  |  | 177 |  | 
| 9 | 6 |  |  | 6 |  | 48 | use List::Util qw(); | 
|  | 6 |  |  |  |  | 6 |  | 
|  | 6 |  |  |  |  | 91 |  | 
| 10 | 6 |  |  | 6 |  | 21 | use List::MoreUtils qw(); | 
|  | 6 |  |  |  |  | 6 |  | 
|  | 6 |  |  |  |  | 20748 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $VERSION = '1.2.0'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | # Mysql dbh | 
| 15 |  |  |  |  |  |  | has 'dbh' => ( is => 'ro', isa => 'Object' ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | # outfiles | 
| 18 |  |  |  |  |  |  | has 'outfile'  => ( is => 'ro', isa => 'Str' );        # output file, autogenerable | 
| 19 |  |  |  |  |  |  | has 'workbook' => ( is => 'ro', isa => 'Object' );     # excel workbook object | 
| 20 |  |  |  |  |  |  | has 'format'   => ( is => 'ro', isa => 'HashRef' );    # excel formats | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # worksheet cursor | 
| 23 |  |  |  |  |  |  | has 'row'    => ( is => 'rw', isa => 'Num', default => sub {0}, ); | 
| 24 |  |  |  |  |  |  | has 'column' => ( is => 'rw', isa => 'Num', default => sub {0}, ); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # charts | 
| 27 |  |  |  |  |  |  | has 'font_name' => ( is => 'rw', isa => 'Str', default => sub {'Arial'}, ); | 
| 28 |  |  |  |  |  |  | has 'font_size' => ( is => 'rw', isa => 'Num', default => sub {10}, ); | 
| 29 |  |  |  |  |  |  | has 'width'     => ( is => 'rw', isa => 'Num', default => sub {320}, ); | 
| 30 |  |  |  |  |  |  | has 'height'    => ( is => 'rw', isa => 'Num', default => sub {320}, ); | 
| 31 |  |  |  |  |  |  | has 'max_ticks' => ( is => 'rw', isa => 'Int', default => sub {6} ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | # Replace texts in titles | 
| 34 |  |  |  |  |  |  | has 'replace' => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub BUILD { | 
| 37 | 5 |  |  | 5 | 0 | 5034 | my $self = shift; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # set outfile | 
| 40 | 5 | 50 |  |  |  | 22 | unless ( $self->{outfile} ) { | 
| 41 | 0 |  |  |  |  | 0 | $self->{outfile} = "auto.xlsx"; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | # Create $workbook object | 
| 45 | 5 |  |  |  |  | 7 | my $workbook; | 
| 46 | 5 | 50 |  |  |  | 68 | unless ( $workbook = Excel::Writer::XLSX->new( $self->{outfile} ) ) { | 
| 47 | 0 |  |  |  |  | 0 | confess "Cannot create Excel file.\n"; | 
| 48 | 0 |  |  |  |  | 0 | return; | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 5 |  |  |  |  | 321997 | $self->{workbook} = $workbook; | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | # set $workbook format | 
| 53 |  |  |  |  |  |  | my %font = ( | 
| 54 |  |  |  |  |  |  | font => $self->{font_name}, | 
| 55 |  |  |  |  |  |  | size => $self->{font_size}, | 
| 56 | 5 |  |  |  |  | 25 | ); | 
| 57 | 5 |  |  |  |  | 28 | my %header = ( | 
| 58 |  |  |  |  |  |  | align    => 'center', | 
| 59 |  |  |  |  |  |  | bg_color => 42, | 
| 60 |  |  |  |  |  |  | bold     => 1, | 
| 61 |  |  |  |  |  |  | bottom   => 2, | 
| 62 |  |  |  |  |  |  | ); | 
| 63 | 5 |  |  |  |  | 51 | my $format = { | 
| 64 |  |  |  |  |  |  | HEADER => $workbook->add_format( %header, %font, ), | 
| 65 |  |  |  |  |  |  | HIGHLIGHT => $workbook->add_format( color => 'blue',  %font, ), | 
| 66 |  |  |  |  |  |  | NORMAL    => $workbook->add_format( color => 'black', %font, ), | 
| 67 |  |  |  |  |  |  | NAME      => $workbook->add_format( bold  => 1,       color => 57, %font, ), | 
| 68 |  |  |  |  |  |  | TOTAL     => $workbook->add_format( bold  => 1,       top => 2, %font, ), | 
| 69 |  |  |  |  |  |  | DATE => $workbook->add_format( | 
| 70 |  |  |  |  |  |  | align      => 'left', | 
| 71 |  |  |  |  |  |  | bg_color   => 42, | 
| 72 |  |  |  |  |  |  | bold       => 1, | 
| 73 |  |  |  |  |  |  | num_format => 'yyyy-mm-dd hh:mm', | 
| 74 |  |  |  |  |  |  | %font, | 
| 75 |  |  |  |  |  |  | ), | 
| 76 |  |  |  |  |  |  | URL       => $workbook->add_format( color => 'blue', underline => 1, %font, ), | 
| 77 |  |  |  |  |  |  | URLHEADER => $workbook->add_format( color => 'blue', underline => 1, %header, %font, ), | 
| 78 |  |  |  |  |  |  | }; | 
| 79 | 5 |  |  |  |  | 3747 | $self->{format} = $format; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 5 |  |  |  |  | 24 | return; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub increase_row { | 
| 85 | 39 |  |  | 39 | 0 | 196 | my $self = shift; | 
| 86 | 39 |  | 50 |  |  | 103 | my $step = shift || 1; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 39 |  |  |  |  | 150 | $self->{row} += $step; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub increase_column { | 
| 92 | 9 |  |  | 9 | 0 | 10 | my $self = shift; | 
| 93 | 9 |  | 50 |  |  | 25 | my $step = shift || 1; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 9 |  |  |  |  | 11 | $self->{column} += $step; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | #@returns Excel::Writer::XLSX::Worksheet | 
| 99 |  |  |  |  |  |  | sub write_header { | 
| 100 | 4 |  |  | 4 | 0 | 180 | my $self       = shift; | 
| 101 | 4 |  |  |  |  | 6 | my $sheet_name = shift; | 
| 102 | 4 |  |  |  |  | 5 | my $opt        = shift; | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | # init | 
| 105 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Workbook | 
| 106 | 4 |  |  |  |  | 7 | my $workbook = $self->{workbook}; | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 109 | 4 |  |  |  |  | 36 | my $sheet  = $workbook->add_worksheet($sheet_name); | 
| 110 | 4 |  |  |  |  | 740 | my $format = $self->{format}; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 4 |  |  |  |  | 7 | my $header     = $opt->{header}; | 
| 113 | 4 |  |  |  |  | 7 | my $query_name = $opt->{query_name}; | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | # create table header | 
| 116 | 4 |  |  |  |  | 18 | for ( my $i = 0; $i < $self->{column}; $i++ ) { | 
| 117 | 2 |  |  |  |  | 12 | $sheet->write( $self->{row}, $i, $query_name, $format->{HEADER} ); | 
| 118 |  |  |  |  |  |  | } | 
| 119 | 4 |  |  |  |  | 160 | for ( my $i = 0; $i < scalar @{$header}; $i++ ) { | 
|  | 21 |  |  |  |  | 739 |  | 
| 120 | 17 |  |  |  |  | 48 | $sheet->write( $self->{row}, $i + $self->{column}, $header->[$i], $format->{HEADER} ); | 
| 121 |  |  |  |  |  |  | } | 
| 122 | 4 |  |  |  |  | 29 | $sheet->freeze_panes( 1, 0 );    # freeze table | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 4 |  |  |  |  | 61 | $self->increase_row; | 
| 125 | 4 |  |  |  |  | 9 | return $sheet; | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | sub sql2names { | 
| 129 | 1 |  |  | 1 | 0 | 12 | my $self = shift; | 
| 130 | 1 |  |  |  |  | 1 | my $sql  = shift; | 
| 131 | 1 |  |  |  |  | 2 | my $opt  = shift; | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | # bind value | 
| 134 | 1 |  |  |  |  | 2 | my $bind_value = $opt->{bind_value}; | 
| 135 | 1 | 50 |  |  |  | 6 | if ( !defined $bind_value ) { | 
| 136 | 1 |  |  |  |  | 2 | $bind_value = []; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | #@type DBI | 
| 140 | 1 |  |  |  |  | 2 | my $dbh = $self->{dbh}; | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | #@type DBI | 
| 143 | 1 |  |  |  |  | 14 | my $sth = $dbh->prepare($sql); | 
| 144 | 1 |  |  |  |  | 5471 | $sth->execute( @{$bind_value} ); | 
|  | 1 |  |  |  |  | 10 |  | 
| 145 | 1 |  |  |  |  | 99780 | my @names = @{ $sth->{'NAME'} }; | 
|  | 1 |  |  |  |  | 18 |  | 
| 146 |  |  |  |  |  |  |  | 
| 147 | 1 |  |  |  |  | 58 | return @names; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | sub write_row { | 
| 151 | 1 |  |  | 1 | 0 | 8 | my $self = shift; | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 154 | 1 |  |  |  |  | 1 | my $sheet = shift; | 
| 155 | 1 |  |  |  |  | 1 | my $opt   = shift; | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | # init | 
| 158 | 1 |  |  |  |  | 2 | my $format = $self->{format}; | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | # query name | 
| 161 | 1 |  |  |  |  | 2 | my $query_name = $opt->{query_name}; | 
| 162 | 1 | 50 |  |  |  | 3 | if ( defined $query_name ) { | 
| 163 | 1 |  |  |  |  | 3 | $sheet->write( $self->{row}, $self->{column} - 1, $query_name, $format->{NAME} ); | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | # array_ref | 
| 167 | 1 |  |  |  |  | 40 | my $row = $opt->{row}; | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | # insert table | 
| 170 | 1 |  |  |  |  | 4 | for ( my $i = 0; $i < scalar @$row; $i++ ) { | 
| 171 | 5 |  |  |  |  | 116 | $sheet->write( $self->{row}, $i + $self->{column}, $row->[$i], $format->{NORMAL} ); | 
| 172 |  |  |  |  |  |  | } | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 1 |  |  |  |  | 26 | $self->increase_row; | 
| 175 | 1 |  |  |  |  | 3 | return; | 
| 176 |  |  |  |  |  |  | } | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | sub write_column { | 
| 179 | 9 |  |  | 9 | 0 | 58 | my $self = shift; | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 182 | 9 |  |  |  |  | 11 | my $sheet = shift; | 
| 183 | 9 |  |  |  |  | 3 | my $opt   = shift; | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | # init | 
| 186 | 9 |  |  |  |  | 11 | my $format = $self->{format}; | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | # query name | 
| 189 | 9 |  |  |  |  | 9 | my $query_name = $opt->{query_name}; | 
| 190 | 9 | 100 |  |  |  | 14 | if ( defined $query_name ) { | 
| 191 | 2 |  |  |  |  | 5 | $sheet->write( $self->{row} - 1, $self->{column}, $query_name, $format->{NAME} ); | 
| 192 |  |  |  |  |  |  | } | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | # array_ref | 
| 195 | 9 |  |  |  |  | 87 | my $column = $opt->{column}; | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | # insert table | 
| 198 | 9 |  |  |  |  | 20 | $sheet->write( $self->{row}, $self->{column}, [$column], $format->{NORMAL} ); | 
| 199 |  |  |  |  |  |  |  | 
| 200 | 9 |  |  |  |  | 1263 | $self->increase_column; | 
| 201 | 9 |  |  |  |  | 18 | return; | 
| 202 |  |  |  |  |  |  | } | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | sub write_sql { | 
| 205 | 3 |  |  | 3 | 0 | 22 | my $self = shift; | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 208 | 3 |  |  |  |  | 6 | my $sheet = shift; | 
| 209 | 3 |  |  |  |  | 3 | my $opt   = shift; | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | # init | 
| 212 | 3 |  |  |  |  | 6 | my $format = $self->{format}; | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | # query name | 
| 215 | 3 |  |  |  |  | 4 | my $query_name = $opt->{query_name}; | 
| 216 | 3 | 100 |  |  |  | 10 | if ( defined $query_name ) { | 
| 217 | 2 |  |  |  |  | 7 | $sheet->write( $self->{row}, $self->{column} - 1, $query_name, $format->{NAME} ); | 
| 218 |  |  |  |  |  |  | } | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | # bind value | 
| 221 | 3 |  |  |  |  | 96 | my $bind_value = $opt->{bind_value}; | 
| 222 | 3 | 50 |  |  |  | 9 | if ( !defined $bind_value ) { | 
| 223 | 3 |  |  |  |  | 5 | $bind_value = []; | 
| 224 |  |  |  |  |  |  | } | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | # init DBI query | 
| 227 | 3 |  |  |  |  | 4 | my $sql_query = $opt->{sql_query}; | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | #@type DBI | 
| 230 | 3 |  |  |  |  | 7 | my $dbh = $self->{dbh}; | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | #@type DBI | 
| 233 | 3 |  |  |  |  | 42 | my $sth = $dbh->prepare($sql_query); | 
| 234 | 3 |  |  |  |  | 10240 | $sth->execute( @{$bind_value} ); | 
|  | 3 |  |  |  |  | 19 |  | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | # init $data | 
| 237 | 3 |  |  |  |  | 287857 | my $data; | 
| 238 | 3 | 50 |  |  |  | 12 | if ( exists $opt->{data} ) { | 
| 239 | 3 | 50 | 33 |  |  | 27 | if ( defined $opt->{data} and ref( $opt->{data} ) eq 'ARRAY' ) { | 
| 240 | 0 |  |  |  |  | 0 | $data = $opt->{data}; | 
| 241 |  |  |  |  |  |  | } | 
| 242 |  |  |  |  |  |  | else { | 
| 243 | 3 |  |  |  |  | 5 | $data = []; | 
| 244 | 3 |  |  |  |  | 5 | push @{$data}, [] for @{ $sth->{'NAME'} }; | 
|  | 3 |  |  |  |  | 24 |  | 
|  | 9 |  |  |  |  | 119 |  | 
| 245 |  |  |  |  |  |  | } | 
| 246 |  |  |  |  |  |  | } | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  | # insert table rows | 
| 249 | 3 |  |  |  |  | 135 | while ( my @row = $sth->fetchrow_array ) { | 
| 250 | 32 |  |  |  |  | 641 | for ( my $i = 0; $i < scalar @row; $i++ ) { | 
| 251 | 96 | 50 |  |  |  | 2023 | if ( exists $opt->{data} ) { | 
| 252 | 96 |  |  |  |  | 54 | push @{ $data->[$i] }, $row[$i]; | 
|  | 96 |  |  |  |  | 130 |  | 
| 253 |  |  |  |  |  |  | } | 
| 254 | 96 |  |  |  |  | 203 | $sheet->write( $self->{row}, $i + $self->{column}, $row[$i], $format->{NORMAL} ); | 
| 255 |  |  |  |  |  |  | } | 
| 256 | 32 |  |  |  |  | 840 | $self->increase_row; | 
| 257 |  |  |  |  |  |  | } | 
| 258 |  |  |  |  |  |  |  | 
| 259 | 3 |  |  |  |  | 60 | return $data; | 
| 260 |  |  |  |  |  |  | } | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | sub make_combine { | 
| 263 | 1 |  |  | 1 | 0 | 13 | my $self = shift; | 
| 264 | 1 |  |  |  |  | 1 | my $opt  = shift; | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  | # init parameters | 
| 267 | 1 |  |  |  |  | 2 | my $sql_query  = $opt->{sql_query}; | 
| 268 | 1 |  |  |  |  | 2 | my $threshold  = $opt->{threshold}; | 
| 269 | 1 |  |  |  |  | 1 | my $standalone = $opt->{standalone}; | 
| 270 |  |  |  |  |  |  |  | 
| 271 |  |  |  |  |  |  | # bind value | 
| 272 | 1 |  |  |  |  | 2 | my $bind_value = $opt->{bind_value}; | 
| 273 | 1 | 50 |  |  |  | 6 | unless ( defined $bind_value ) { | 
| 274 | 1 |  |  |  |  | 1 | $bind_value = []; | 
| 275 |  |  |  |  |  |  | } | 
| 276 |  |  |  |  |  |  |  | 
| 277 |  |  |  |  |  |  | # merge_last | 
| 278 | 1 |  |  |  |  | 1 | my $merge_last = $opt->{merge_last}; | 
| 279 | 1 | 50 |  |  |  | 3 | unless ( defined $merge_last ) { | 
| 280 | 0 |  |  |  |  | 0 | $merge_last = 0; | 
| 281 |  |  |  |  |  |  | } | 
| 282 |  |  |  |  |  |  |  | 
| 283 |  |  |  |  |  |  | # init DBI query | 
| 284 |  |  |  |  |  |  | #@type DBI | 
| 285 | 1 |  |  |  |  | 2 | my $dbh = $self->{dbh}; | 
| 286 |  |  |  |  |  |  |  | 
| 287 |  |  |  |  |  |  | #@type DBI | 
| 288 | 1 |  |  |  |  | 19 | my $sth = $dbh->prepare($sql_query); | 
| 289 | 1 |  |  |  |  | 5535 | $sth->execute(@$bind_value); | 
| 290 |  |  |  |  |  |  |  | 
| 291 | 1 |  |  |  |  | 117284 | my @row_count = (); | 
| 292 | 1 |  |  |  |  | 38 | while ( my @row = $sth->fetchrow_array ) { | 
| 293 | 58 |  |  |  |  | 955 | push @row_count, \@row; | 
| 294 |  |  |  |  |  |  | } | 
| 295 |  |  |  |  |  |  |  | 
| 296 | 1 |  |  |  |  | 14 | my @combined;    # return these | 
| 297 | 1 |  |  |  |  | 2 | my @temp_combined = (); | 
| 298 | 1 |  |  |  |  | 2 | my $temp_count    = 0; | 
| 299 | 1 |  |  |  |  | 2 | foreach my $row_ref (@row_count) { | 
| 300 | 58 | 100 |  | 115 |  | 72 | if ( List::MoreUtils::PP::any { $_ eq $row_ref->[0] } @{$standalone} ) { | 
|  | 115 | 50 |  |  |  | 292 |  | 
|  | 58 |  |  |  |  | 82 |  | 
| 301 | 2 |  |  |  |  | 10 | push @combined, [ $row_ref->[0] ]; | 
| 302 |  |  |  |  |  |  | } | 
| 303 |  |  |  |  |  |  | elsif ( $temp_count < $threshold ) { | 
| 304 | 56 |  |  |  |  | 160 | push @temp_combined, $row_ref->[0]; | 
| 305 | 56 |  |  |  |  | 39 | $temp_count += $row_ref->[1]; | 
| 306 |  |  |  |  |  |  |  | 
| 307 | 56 | 100 |  |  |  | 46 | if ( $temp_count < $threshold ) { | 
| 308 | 48 |  |  |  |  | 61 | next; | 
| 309 |  |  |  |  |  |  | } | 
| 310 |  |  |  |  |  |  | else { | 
| 311 | 8 |  |  |  |  | 16 | push @combined, [@temp_combined]; | 
| 312 | 8 |  |  |  |  | 9 | @temp_combined = (); | 
| 313 | 8 |  |  |  |  | 10 | $temp_count    = 0; | 
| 314 |  |  |  |  |  |  | } | 
| 315 |  |  |  |  |  |  | } | 
| 316 |  |  |  |  |  |  | else { | 
| 317 | 0 |  |  |  |  | 0 | warn "Errors occured in calculating combined distance.\n"; | 
| 318 |  |  |  |  |  |  | } | 
| 319 |  |  |  |  |  |  | } | 
| 320 |  |  |  |  |  |  |  | 
| 321 |  |  |  |  |  |  | # Write the last weighted row which COUNT might | 
| 322 |  |  |  |  |  |  | #   be smaller than $threshold | 
| 323 | 1 | 50 |  |  |  | 5 | if ( $temp_count > 0 ) { | 
| 324 | 1 | 50 |  |  |  | 2 | if ($merge_last) { | 
| 325 | 1 | 50 |  |  |  | 4 | if ( @combined == 0 ) { | 
| 326 | 0 |  |  |  |  | 0 | @combined = ( [] ); | 
| 327 |  |  |  |  |  |  | } | 
| 328 | 1 |  |  |  |  | 1 | push @{ $combined[-1] }, @temp_combined; | 
|  | 1 |  |  |  |  | 3 |  | 
| 329 |  |  |  |  |  |  | } | 
| 330 |  |  |  |  |  |  | else { | 
| 331 | 0 |  |  |  |  | 0 | push @combined, [@temp_combined]; | 
| 332 |  |  |  |  |  |  | } | 
| 333 |  |  |  |  |  |  | } | 
| 334 |  |  |  |  |  |  |  | 
| 335 | 1 |  |  |  |  | 21 | return \@combined; | 
| 336 |  |  |  |  |  |  | } | 
| 337 |  |  |  |  |  |  |  | 
| 338 |  |  |  |  |  |  | sub make_combine_piece { | 
| 339 | 1 |  |  | 1 | 0 | 1358 | my ( $self, $opt ) = @_; | 
| 340 |  |  |  |  |  |  |  | 
| 341 |  |  |  |  |  |  | #@type DBI | 
| 342 | 1 |  |  |  |  | 2 | my $dbh = $self->{dbh}; | 
| 343 |  |  |  |  |  |  |  | 
| 344 |  |  |  |  |  |  | # init parameters | 
| 345 | 1 |  |  |  |  | 2 | my $sql_query = $opt->{sql_query}; | 
| 346 | 1 |  |  |  |  | 2 | my $piece     = $opt->{piece}; | 
| 347 |  |  |  |  |  |  |  | 
| 348 |  |  |  |  |  |  | # bind value | 
| 349 | 1 |  |  |  |  | 2 | my $bind_value = $opt->{bind_value}; | 
| 350 | 1 | 50 |  |  |  | 4 | unless ( defined $bind_value ) { | 
| 351 | 1 |  |  |  |  | 2 | $bind_value = []; | 
| 352 |  |  |  |  |  |  | } | 
| 353 |  |  |  |  |  |  |  | 
| 354 |  |  |  |  |  |  | # init DBI query | 
| 355 |  |  |  |  |  |  | #@type DBI | 
| 356 | 1 |  |  |  |  | 9 | my $sth = $dbh->prepare($sql_query); | 
| 357 | 1 |  |  |  |  | 1520 | $sth->execute(@$bind_value); | 
| 358 |  |  |  |  |  |  |  | 
| 359 | 1 |  |  |  |  | 81402 | my @row_count = (); | 
| 360 | 1 |  |  |  |  | 7 | while ( my @row = $sth->fetchrow_array ) { | 
| 361 | 1000 |  |  |  |  | 15763 | push @row_count, \@row; | 
| 362 |  |  |  |  |  |  | } | 
| 363 |  |  |  |  |  |  |  | 
| 364 | 1 |  |  |  |  | 13 | my $sum; | 
| 365 | 1 |  |  |  |  | 219 | $sum += $_->[1] for @row_count; | 
| 366 | 1 |  |  |  |  | 3 | my $small_chunk = $sum / $piece; | 
| 367 |  |  |  |  |  |  |  | 
| 368 | 1 |  |  |  |  | 1 | my @combined;    # return these | 
| 369 | 1 |  |  |  |  | 2 | my @temp_combined = (); | 
| 370 | 1 |  |  |  |  | 2 | my $temp_count    = 0; | 
| 371 | 1 |  |  |  |  | 3 | for my $row_ref (@row_count) { | 
| 372 | 1000 | 50 |  |  |  | 781 | if ( $temp_count < $small_chunk ) { | 
| 373 | 1000 |  |  |  |  | 691 | push @temp_combined, $row_ref->[0]; | 
| 374 | 1000 |  |  |  |  | 557 | $temp_count += $row_ref->[1]; | 
| 375 |  |  |  |  |  |  |  | 
| 376 | 1000 | 100 |  |  |  | 1155 | if ( $temp_count >= $small_chunk ) { | 
| 377 | 19 |  |  |  |  | 95 | push @combined, [@temp_combined]; | 
| 378 | 19 |  |  |  |  | 50 | @temp_combined = (); | 
| 379 | 19 |  |  |  |  | 13 | $temp_count    = 0; | 
| 380 |  |  |  |  |  |  | } | 
| 381 |  |  |  |  |  |  | } | 
| 382 |  |  |  |  |  |  | else { | 
| 383 | 0 |  |  |  |  | 0 | warn "Errors occured in calculating combined distance.\n"; | 
| 384 |  |  |  |  |  |  | } | 
| 385 |  |  |  |  |  |  | } | 
| 386 |  |  |  |  |  |  |  | 
| 387 |  |  |  |  |  |  | # Write the last weighted row which COUNT might | 
| 388 |  |  |  |  |  |  | #   be smaller than $threshold | 
| 389 | 1 | 50 |  |  |  | 4 | if ( $temp_count > 0 ) { | 
| 390 | 1 |  |  |  |  | 7 | push @combined, [@temp_combined]; | 
| 391 |  |  |  |  |  |  | } | 
| 392 |  |  |  |  |  |  |  | 
| 393 | 1 |  |  |  |  | 107 | return \@combined; | 
| 394 |  |  |  |  |  |  | } | 
| 395 |  |  |  |  |  |  |  | 
| 396 |  |  |  |  |  |  | sub make_last_portion { | 
| 397 | 1 |  |  | 1 | 0 | 799 | my ( $self, $opt ) = @_; | 
| 398 |  |  |  |  |  |  |  | 
| 399 |  |  |  |  |  |  | #@type DBI | 
| 400 | 1 |  |  |  |  | 3 | my $dbh = $self->{dbh}; | 
| 401 |  |  |  |  |  |  |  | 
| 402 |  |  |  |  |  |  | # init parameters | 
| 403 | 1 |  |  |  |  | 1 | my $sql_query = $opt->{sql_query}; | 
| 404 | 1 |  |  |  |  | 2 | my $portion   = $opt->{portion}; | 
| 405 |  |  |  |  |  |  |  | 
| 406 |  |  |  |  |  |  | # init DBI query | 
| 407 |  |  |  |  |  |  | #@type DBI | 
| 408 | 1 |  |  |  |  | 8 | my $sth = $dbh->prepare($sql_query); | 
| 409 | 1 |  |  |  |  | 1768 | $sth->execute; | 
| 410 |  |  |  |  |  |  |  | 
| 411 | 1 |  |  |  |  | 117491 | my @row_count = (); | 
| 412 | 1 |  |  |  |  | 11 | while ( my @row = $sth->fetchrow_array ) { | 
| 413 | 58 |  |  |  |  | 980 | push @row_count, \@row; | 
| 414 |  |  |  |  |  |  | } | 
| 415 |  |  |  |  |  |  |  | 
| 416 | 1 |  |  |  |  | 16 | my @last_portion;    # return @last_portion | 
| 417 | 1 |  |  |  |  | 1 | my $all_length = 0;  # return $all_length | 
| 418 | 1 |  |  |  |  | 3 | foreach (@row_count) { | 
| 419 | 58 |  |  |  |  | 39 | $all_length += $_->[2]; | 
| 420 |  |  |  |  |  |  | } | 
| 421 | 1 |  |  |  |  | 5 | my @rev_row_count = reverse @row_count; | 
| 422 | 1 |  |  |  |  | 1 | my $temp_length   = 0; | 
| 423 | 1 |  |  |  |  | 2 | foreach (@rev_row_count) { | 
| 424 | 13 |  |  |  |  | 15 | push @last_portion, $_->[0]; | 
| 425 | 13 |  |  |  |  | 8 | $temp_length += $_->[2]; | 
| 426 | 13 | 100 |  |  |  | 17 | if ( $temp_length >= $all_length * $portion ) { | 
| 427 | 1 |  |  |  |  | 2 | last; | 
| 428 |  |  |  |  |  |  | } | 
| 429 |  |  |  |  |  |  | } | 
| 430 |  |  |  |  |  |  |  | 
| 431 | 1 |  |  |  |  | 15 | return ( $all_length, \@last_portion ); | 
| 432 |  |  |  |  |  |  | } | 
| 433 |  |  |  |  |  |  |  | 
| 434 |  |  |  |  |  |  | sub excute_sql { | 
| 435 | 1 |  |  | 1 | 0 | 935 | my ( $self, $opt ) = @_; | 
| 436 |  |  |  |  |  |  |  | 
| 437 |  |  |  |  |  |  | # bind value | 
| 438 | 1 |  |  |  |  | 2 | my $bind_value = $opt->{bind_value}; | 
| 439 | 1 | 50 |  |  |  | 4 | unless ( defined $bind_value ) { | 
| 440 | 1 |  |  |  |  | 4 | $bind_value = []; | 
| 441 |  |  |  |  |  |  | } | 
| 442 |  |  |  |  |  |  |  | 
| 443 |  |  |  |  |  |  | # init DBI query | 
| 444 | 1 |  |  |  |  | 1 | my $sql_query = $opt->{sql_query}; | 
| 445 |  |  |  |  |  |  |  | 
| 446 |  |  |  |  |  |  | #@type DBI | 
| 447 | 1 |  |  |  |  | 2 | my $dbh = $self->{dbh}; | 
| 448 |  |  |  |  |  |  |  | 
| 449 |  |  |  |  |  |  | #@type DBI | 
| 450 | 1 |  |  |  |  | 7 | my $sth = $dbh->prepare($sql_query); | 
| 451 | 1 |  |  |  |  | 374 | $sth->execute( @{$bind_value} ); | 
|  | 1 |  |  |  |  | 5 |  | 
| 452 |  |  |  |  |  |  | } | 
| 453 |  |  |  |  |  |  |  | 
| 454 |  |  |  |  |  |  | sub check_column { | 
| 455 | 0 |  |  | 0 | 0 | 0 | my ( $self, $table, $column ) = @_; | 
| 456 |  |  |  |  |  |  |  | 
| 457 |  |  |  |  |  |  | # init | 
| 458 |  |  |  |  |  |  | #@type DBI | 
| 459 | 0 |  |  |  |  | 0 | my $dbh = $self->{dbh}; | 
| 460 |  |  |  |  |  |  |  | 
| 461 |  |  |  |  |  |  | {    # check table existing | 
| 462 | 0 |  |  |  |  | 0 | my @table_names = $dbh->tables( '', '', '' ); | 
| 463 |  |  |  |  |  |  |  | 
| 464 |  |  |  |  |  |  | # table names are quoted by ` (back-quotes) which is the | 
| 465 |  |  |  |  |  |  | #   quote_identifier | 
| 466 | 0 |  |  |  |  | 0 | my $table_name = "`$table`"; | 
| 467 | 0 | 0 |  | 0 |  | 0 | unless ( List::MoreUtils::PP::any { $_ =~ /$table_name/i } @table_names ) { | 
|  | 0 |  |  |  |  | 0 |  | 
| 468 | 0 |  |  |  |  | 0 | print " " x 4, "Table $table does not exist\n"; | 
| 469 | 0 |  |  |  |  | 0 | return 0; | 
| 470 |  |  |  |  |  |  | } | 
| 471 |  |  |  |  |  |  | } | 
| 472 |  |  |  |  |  |  |  | 
| 473 |  |  |  |  |  |  | {    # check column existing | 
| 474 | 0 |  |  |  |  | 0 | my $sql_query = qq{ | 
|  | 0 |  |  |  |  | 0 |  | 
| 475 |  |  |  |  |  |  | SHOW FIELDS | 
| 476 |  |  |  |  |  |  | FROM $table | 
| 477 |  |  |  |  |  |  | LIKE "$column" | 
| 478 |  |  |  |  |  |  | }; | 
| 479 |  |  |  |  |  |  |  | 
| 480 |  |  |  |  |  |  | #@type DBI | 
| 481 | 0 |  |  |  |  | 0 | my $sth = $dbh->prepare($sql_query); | 
| 482 | 0 |  |  |  |  | 0 | $sth->execute(); | 
| 483 | 0 |  |  |  |  | 0 | my ($field) = $sth->fetchrow_array; | 
| 484 |  |  |  |  |  |  |  | 
| 485 | 0 | 0 |  |  |  | 0 | if ( not $field ) { | 
| 486 | 0 |  |  |  |  | 0 | print " " x 4, "Column $column does not exist\n"; | 
| 487 | 0 |  |  |  |  | 0 | return 0; | 
| 488 |  |  |  |  |  |  | } | 
| 489 |  |  |  |  |  |  | } | 
| 490 |  |  |  |  |  |  |  | 
| 491 |  |  |  |  |  |  | {    # check values in column | 
| 492 | 0 |  |  |  |  | 0 | my $sql_query = qq{ | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 493 |  |  |  |  |  |  | SELECT COUNT($column) | 
| 494 |  |  |  |  |  |  | FROM $table | 
| 495 |  |  |  |  |  |  | }; | 
| 496 |  |  |  |  |  |  |  | 
| 497 |  |  |  |  |  |  | #@type DBI | 
| 498 | 0 |  |  |  |  | 0 | my $sth = $dbh->prepare($sql_query); | 
| 499 | 0 |  |  |  |  | 0 | $sth->execute; | 
| 500 | 0 |  |  |  |  | 0 | my ($count) = $sth->fetchrow_array; | 
| 501 |  |  |  |  |  |  |  | 
| 502 | 0 | 0 |  |  |  | 0 | if ( not $count ) { | 
| 503 | 0 |  |  |  |  | 0 | print " " x 4, "Column $column has no records\n"; | 
| 504 |  |  |  |  |  |  | } | 
| 505 |  |  |  |  |  |  |  | 
| 506 | 0 |  |  |  |  | 0 | return $count; | 
| 507 |  |  |  |  |  |  | } | 
| 508 |  |  |  |  |  |  | } | 
| 509 |  |  |  |  |  |  |  | 
| 510 |  |  |  |  |  |  | sub quantile { | 
| 511 | 1 |  |  | 1 | 0 | 2 | my ( $self, $data, $part_number ) = @_; | 
| 512 |  |  |  |  |  |  |  | 
| 513 | 1 |  |  |  |  | 21 | my $stat = Statistics::Descriptive::Full->new(); | 
| 514 |  |  |  |  |  |  |  | 
| 515 | 1 |  |  |  |  | 95 | $stat->add_data(@$data); | 
| 516 |  |  |  |  |  |  |  | 
| 517 | 1 |  |  |  |  | 789 | my $min = $stat->min; | 
| 518 | 1 |  |  |  |  | 4 | my @quantiles; | 
| 519 | 1 |  |  |  |  | 2 | my $base = 100 / $part_number; | 
| 520 | 1 |  |  |  |  | 4 | for ( 1 .. $part_number - 1 ) { | 
| 521 | 3 |  |  |  |  | 8 | my $percentile = $stat->percentile( $_ * $base ); | 
| 522 | 3 |  |  |  |  | 1462 | push @quantiles, $percentile; | 
| 523 |  |  |  |  |  |  | } | 
| 524 | 1 |  |  |  |  | 2 | my $max = $stat->max; | 
| 525 |  |  |  |  |  |  |  | 
| 526 | 1 |  |  |  |  | 50 | return [ $min, @quantiles, $max, ]; | 
| 527 |  |  |  |  |  |  | } | 
| 528 |  |  |  |  |  |  |  | 
| 529 |  |  |  |  |  |  | sub quantile_sql { | 
| 530 | 1 |  |  | 1 | 0 | 475 | my ( $self, $opt, $part_number ) = @_; | 
| 531 |  |  |  |  |  |  |  | 
| 532 |  |  |  |  |  |  | #@type DBI | 
| 533 | 1 |  |  |  |  | 2 | my $dbh = $self->{dbh}; | 
| 534 |  |  |  |  |  |  |  | 
| 535 |  |  |  |  |  |  | # bind value | 
| 536 | 1 |  |  |  |  | 2 | my $bind_value = $opt->{bind_value}; | 
| 537 | 1 | 50 |  |  |  | 4 | unless ( defined $bind_value ) { | 
| 538 | 1 |  |  |  |  | 2 | $bind_value = []; | 
| 539 |  |  |  |  |  |  | } | 
| 540 |  |  |  |  |  |  |  | 
| 541 |  |  |  |  |  |  | # init DBI query | 
| 542 | 1 |  |  |  |  | 2 | my $sql_query = $opt->{sql_query}; | 
| 543 |  |  |  |  |  |  |  | 
| 544 |  |  |  |  |  |  | #@type DBI | 
| 545 | 1 |  |  |  |  | 6 | my $sth = $dbh->prepare($sql_query); | 
| 546 | 1 |  |  |  |  | 836 | $sth->execute(@$bind_value); | 
| 547 |  |  |  |  |  |  |  | 
| 548 | 1 |  |  |  |  | 70639 | my @data; | 
| 549 |  |  |  |  |  |  |  | 
| 550 | 1 |  |  |  |  | 8 | while ( my @row = $sth->fetchrow_array ) { | 
| 551 | 1000 |  |  |  |  | 15500 | push @data, $row[0]; | 
| 552 |  |  |  |  |  |  | } | 
| 553 |  |  |  |  |  |  |  | 
| 554 | 1 |  |  |  |  | 15 | return $self->quantile( \@data, $part_number ); | 
| 555 |  |  |  |  |  |  | } | 
| 556 |  |  |  |  |  |  |  | 
| 557 |  |  |  |  |  |  | sub calc_threshold { | 
| 558 | 0 |  |  | 0 | 0 | 0 | my $self = shift; | 
| 559 |  |  |  |  |  |  |  | 
| 560 | 0 |  |  |  |  | 0 | my ( $combine, $piece ); | 
| 561 |  |  |  |  |  |  |  | 
| 562 |  |  |  |  |  |  | #@type DBI | 
| 563 | 0 |  |  |  |  | 0 | my $dbh = $self->{dbh}; | 
| 564 |  |  |  |  |  |  |  | 
| 565 |  |  |  |  |  |  | #@type DBI | 
| 566 | 0 |  |  |  |  | 0 | my $sth = $dbh->prepare( | 
| 567 |  |  |  |  |  |  | q{ | 
| 568 |  |  |  |  |  |  | SELECT SUM(FLOOR(align_comparables / 500) * 500) | 
| 569 |  |  |  |  |  |  | FROM align | 
| 570 |  |  |  |  |  |  | } | 
| 571 |  |  |  |  |  |  | ); | 
| 572 | 0 |  |  |  |  | 0 | $sth->execute; | 
| 573 | 0 |  |  |  |  | 0 | my ($total_length) = $sth->fetchrow_array; | 
| 574 |  |  |  |  |  |  |  | 
| 575 | 0 | 0 |  |  |  | 0 | if ( $total_length <= 5_000_000 ) { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 576 | 0 |  |  |  |  | 0 | $piece = 10; | 
| 577 |  |  |  |  |  |  | } | 
| 578 |  |  |  |  |  |  | elsif ( $total_length <= 10_000_000 ) { | 
| 579 | 0 |  |  |  |  | 0 | $piece = 10; | 
| 580 |  |  |  |  |  |  | } | 
| 581 |  |  |  |  |  |  | elsif ( $total_length <= 100_000_000 ) { | 
| 582 | 0 |  |  |  |  | 0 | $piece = 20; | 
| 583 |  |  |  |  |  |  | } | 
| 584 |  |  |  |  |  |  | elsif ( $total_length <= 1_000_000_000 ) { | 
| 585 | 0 |  |  |  |  | 0 | $piece = 50; | 
| 586 |  |  |  |  |  |  | } | 
| 587 |  |  |  |  |  |  | else { | 
| 588 | 0 |  |  |  |  | 0 | $piece = 100; | 
| 589 |  |  |  |  |  |  | } | 
| 590 |  |  |  |  |  |  |  | 
| 591 | 0 | 0 |  |  |  | 0 | if ( $total_length <= 1_000_000 ) { | 
|  |  | 0 |  |  |  |  |  | 
| 592 | 0 |  |  |  |  | 0 | $combine = 100; | 
| 593 |  |  |  |  |  |  | } | 
| 594 |  |  |  |  |  |  | elsif ( $total_length <= 5_000_000 ) { | 
| 595 | 0 |  |  |  |  | 0 | $combine = 500; | 
| 596 |  |  |  |  |  |  | } | 
| 597 |  |  |  |  |  |  | else { | 
| 598 | 0 |  |  |  |  | 0 | $combine = 1000; | 
| 599 |  |  |  |  |  |  | } | 
| 600 |  |  |  |  |  |  |  | 
| 601 | 0 |  |  |  |  | 0 | return ( $combine, $piece ); | 
| 602 |  |  |  |  |  |  | } | 
| 603 |  |  |  |  |  |  |  | 
| 604 |  |  |  |  |  |  | # See HACK #7 in OReilly.Excel.Hacks.2nd.Edition. | 
| 605 |  |  |  |  |  |  | sub add_index_sheet { | 
| 606 | 1 |  |  | 1 | 0 | 34 | my $self = shift; | 
| 607 |  |  |  |  |  |  |  | 
| 608 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Workbook | 
| 609 | 1 |  |  |  |  | 3 | my $workbook = $self->{workbook}; | 
| 610 | 1 |  |  |  |  | 2 | my $format   = $self->{format}; | 
| 611 |  |  |  |  |  |  |  | 
| 612 |  |  |  |  |  |  | # existing sheets | 
| 613 | 1 |  |  |  |  | 17 | my @sheets = $workbook->sheets(); | 
| 614 |  |  |  |  |  |  |  | 
| 615 |  |  |  |  |  |  | # create a new worksheet named "INDEX" | 
| 616 | 1 |  |  |  |  | 7 | my $sheet_name = "INDEX"; | 
| 617 |  |  |  |  |  |  |  | 
| 618 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 619 | 1 |  |  |  |  | 5 | my $index_sheet = $workbook->add_worksheet($sheet_name); | 
| 620 |  |  |  |  |  |  |  | 
| 621 |  |  |  |  |  |  | # set hyperlink column with large width | 
| 622 | 1 |  |  |  |  | 183 | $index_sheet->set_column( 'A:A', 20 ); | 
| 623 |  |  |  |  |  |  |  | 
| 624 |  |  |  |  |  |  | #   0    1    2     3     4    5     6     7     8 | 
| 625 |  |  |  |  |  |  | #($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = | 
| 626 |  |  |  |  |  |  | #                                            localtime(time); | 
| 627 | 1 |  |  |  |  | 162 | my $date = sprintf "%4d-%02d-%02dT%02d:%02d", (localtime)[5] + 1900, (localtime)[4] + 1, | 
| 628 |  |  |  |  |  |  | (localtime)[ 3, 2, 1 ]; | 
| 629 | 1 |  |  |  |  | 6 | $index_sheet->write_date_time( 'A1', $date, $format->{DATE} ); | 
| 630 |  |  |  |  |  |  |  | 
| 631 | 1 |  |  |  |  | 130 | for my $i ( 0 .. $#sheets ) { | 
| 632 |  |  |  |  |  |  |  | 
| 633 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 634 | 1 |  |  |  |  | 1 | my $cur_sheet = $sheets[$i]; | 
| 635 | 1 |  |  |  |  | 7 | my $cur_name  = $cur_sheet->get_name; | 
| 636 |  |  |  |  |  |  |  | 
| 637 |  |  |  |  |  |  | # $worksheet->write_url( $row, $col, $url, $format, $label ) | 
| 638 | 1 |  |  |  |  | 9 | $index_sheet->write_url( $i + 1, 0, "internal:$cur_name!A1", $format->{URL}, $cur_name ); | 
| 639 |  |  |  |  |  |  |  | 
| 640 |  |  |  |  |  |  | $cur_sheet->write_url( "A1", "internal:INDEX!A" . ( $i + 2 ), | 
| 641 | 1 |  |  |  |  | 101 | $format->{URLHEADER}, "INDEX" ); | 
| 642 |  |  |  |  |  |  | } | 
| 643 |  |  |  |  |  |  |  | 
| 644 | 1 |  |  |  |  | 99 | return; | 
| 645 |  |  |  |  |  |  | } | 
| 646 |  |  |  |  |  |  |  | 
| 647 |  |  |  |  |  |  | sub draw_y { | 
| 648 | 1 |  |  | 1 | 0 | 110 | my $self = shift; | 
| 649 |  |  |  |  |  |  |  | 
| 650 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 651 | 1 |  |  |  |  | 2 | my $sheet = shift; | 
| 652 | 1 |  |  |  |  | 1 | my $opt   = shift; | 
| 653 |  |  |  |  |  |  |  | 
| 654 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Workbook | 
| 655 | 1 |  |  |  |  | 2 | my $workbook   = $self->{workbook}; | 
| 656 | 1 |  |  |  |  | 6 | my $sheet_name = $sheet->get_name; | 
| 657 |  |  |  |  |  |  |  | 
| 658 | 1 |  | 33 |  |  | 9 | my $font_name = $opt->{font_name} || $self->{font_name}; | 
| 659 | 1 |  | 33 |  |  | 5 | my $font_size = $opt->{font_size} || $self->{font_size}; | 
| 660 | 1 |  | 33 |  |  | 4 | my $height    = $opt->{height}    || $self->{height}; | 
| 661 | 1 |  | 33 |  |  | 5 | my $width     = $opt->{width}     || $self->{width}; | 
| 662 |  |  |  |  |  |  |  | 
| 663 |  |  |  |  |  |  | # E2 | 
| 664 | 1 |  | 50 |  |  | 3 | my $top  = $opt->{top}  || 1; | 
| 665 | 1 |  | 50 |  |  | 5 | my $left = $opt->{left} || 4; | 
| 666 |  |  |  |  |  |  |  | 
| 667 |  |  |  |  |  |  | # 0 based | 
| 668 | 1 |  |  |  |  | 2 | my $first_row     = $opt->{first_row}; | 
| 669 | 1 |  |  |  |  | 1 | my $last_row      = $opt->{last_row}; | 
| 670 | 1 |  |  |  |  | 1 | my $x_column      = $opt->{x_column}; | 
| 671 | 1 |  |  |  |  | 1 | my $y_column      = $opt->{y_column}; | 
| 672 | 1 |  |  |  |  | 2 | my $y_last_column = $opt->{y_last_column}; | 
| 673 | 1 | 50 |  |  |  | 2 | unless ( defined $y_last_column ) { | 
| 674 | 1 |  |  |  |  | 2 | $y_last_column = $y_column; | 
| 675 |  |  |  |  |  |  | } | 
| 676 |  |  |  |  |  |  |  | 
| 677 |  |  |  |  |  |  | # Set axes' scale | 
| 678 | 1 |  |  |  |  | 1 | my $x_max_scale = $opt->{x_max_scale}; | 
| 679 | 1 |  |  |  |  | 2 | my $x_min_scale = $opt->{x_min_scale}; | 
| 680 | 1 | 50 |  |  |  | 2 | if ( !defined $x_min_scale ) { | 
| 681 | 1 |  |  |  |  | 2 | $x_min_scale = 0; | 
| 682 |  |  |  |  |  |  | } | 
| 683 | 1 | 50 | 33 |  |  | 5 | if ( !defined $x_max_scale and exists $opt->{x_scale_unit} ) { | 
| 684 | 1 |  |  |  |  | 1 | my $x_scale_unit = $opt->{x_scale_unit}; | 
| 685 | 1 |  |  |  |  | 2 | my $x_min_value  = List::Util::min( @{ $opt->{x_data} } ); | 
|  | 1 |  |  |  |  | 10 |  | 
| 686 | 1 |  |  |  |  | 2 | my $x_max_value  = List::Util::max( @{ $opt->{x_data} } ); | 
|  | 1 |  |  |  |  | 2 |  | 
| 687 | 1 |  |  |  |  | 146 | $x_min_scale = int( $x_min_value / $x_scale_unit ) * $x_scale_unit; | 
| 688 | 1 |  |  |  |  | 47 | $x_max_scale = ( int( $x_max_value / $x_scale_unit ) + 1 ) * $x_scale_unit; | 
| 689 |  |  |  |  |  |  | } | 
| 690 |  |  |  |  |  |  |  | 
| 691 | 1 |  |  |  |  | 3 | my $y_scale; | 
| 692 | 1 | 50 |  |  |  | 4 | if ( exists $opt->{y_data} ) { | 
| 693 | 1 |  |  |  |  | 6 | $y_scale = $self->_find_scale( $opt->{y_data}, $first_row, $last_row ); | 
| 694 |  |  |  |  |  |  | } | 
| 695 |  |  |  |  |  |  |  | 
| 696 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Chart | 
| 697 | 1 |  |  |  |  | 39 | my $chart = $workbook->add_chart( | 
| 698 |  |  |  |  |  |  | type     => 'scatter', | 
| 699 |  |  |  |  |  |  | subtype  => 'straight_with_markers', | 
| 700 |  |  |  |  |  |  | embedded => 1 | 
| 701 |  |  |  |  |  |  | ); | 
| 702 |  |  |  |  |  |  |  | 
| 703 |  |  |  |  |  |  | # [ $sheetname, $row_start, $row_end, $col_start, $col_end ] | 
| 704 |  |  |  |  |  |  | #  #"=$sheetname" . '!$A$2:$A$7', | 
| 705 | 1 |  |  |  |  | 2173 | for my $y_col ( $y_column .. $y_last_column ) { | 
| 706 | 1 |  |  |  |  | 8 | $chart->add_series( | 
| 707 |  |  |  |  |  |  | categories => [ $sheet_name, $first_row, $last_row, $x_column, $x_column ], | 
| 708 |  |  |  |  |  |  | values     => [ $sheet_name, $first_row, $last_row, $y_col,    $y_col ], | 
| 709 |  |  |  |  |  |  | ); | 
| 710 |  |  |  |  |  |  | } | 
| 711 | 1 |  |  |  |  | 250 | $chart->set_size( width => $width, height => $height ); | 
| 712 |  |  |  |  |  |  |  | 
| 713 |  |  |  |  |  |  | # Remove title and legend | 
| 714 | 1 |  |  |  |  | 17 | $chart->set_title( none => 1 ); | 
| 715 | 1 |  |  |  |  | 34 | $chart->set_legend( none => 1 ); | 
| 716 |  |  |  |  |  |  |  | 
| 717 |  |  |  |  |  |  | # Blank data is shown as a gap | 
| 718 | 1 |  |  |  |  | 22 | $chart->show_blanks_as('gap'); | 
| 719 |  |  |  |  |  |  |  | 
| 720 |  |  |  |  |  |  | # set axis | 
| 721 |  |  |  |  |  |  | $chart->set_x_axis( | 
| 722 |  |  |  |  |  |  | name      => $self->_replace_text( $opt->{x_title} ), | 
| 723 |  |  |  |  |  |  | name_font => { name => $font_name, size => $font_size, }, | 
| 724 |  |  |  |  |  |  | num_font  => { name => $font_name, size => $font_size, }, | 
| 725 |  |  |  |  |  |  | line            => { color   => 'black', }, | 
| 726 |  |  |  |  |  |  | major_gridlines => { visible => 0, }, | 
| 727 |  |  |  |  |  |  | minor_gridlines => { visible => 0, }, | 
| 728 |  |  |  |  |  |  | major_tick_mark => 'inside', | 
| 729 |  |  |  |  |  |  | min             => $x_min_scale, | 
| 730 |  |  |  |  |  |  | max             => $x_max_scale, | 
| 731 | 1 | 50 |  |  |  | 15 | exists $opt->{cross} ? ( crossing => $opt->{cross}, ) : (), | 
| 732 |  |  |  |  |  |  | ); | 
| 733 |  |  |  |  |  |  | $chart->set_y_axis( | 
| 734 |  |  |  |  |  |  | name      => $self->_replace_text( $opt->{y_title} ), | 
| 735 |  |  |  |  |  |  | name_font => { name => $font_name, size => $font_size, }, | 
| 736 |  |  |  |  |  |  | num_font  => { name => $font_name, size => $font_size, }, | 
| 737 |  |  |  |  |  |  | line            => { color   => 'black', }, | 
| 738 |  |  |  |  |  |  | major_gridlines => { visible => 0, }, | 
| 739 |  |  |  |  |  |  | minor_gridlines => { visible => 0, }, | 
| 740 |  |  |  |  |  |  | major_tick_mark => 'inside', | 
| 741 |  |  |  |  |  |  | defined $y_scale | 
| 742 |  |  |  |  |  |  | ? ( min => $y_scale->{min}, max => $y_scale->{max}, major_unit => $y_scale->{unit}, ) | 
| 743 | 1 | 50 |  |  |  | 106 | : (), | 
| 744 |  |  |  |  |  |  | ); | 
| 745 |  |  |  |  |  |  |  | 
| 746 |  |  |  |  |  |  | # plorarea | 
| 747 | 1 |  |  |  |  | 83 | $chart->set_plotarea( border => { color => 'black', }, ); | 
| 748 |  |  |  |  |  |  |  | 
| 749 | 1 |  |  |  |  | 61 | $sheet->insert_chart( $top, $left, $chart ); | 
| 750 |  |  |  |  |  |  |  | 
| 751 | 1 |  |  |  |  | 36 | return; | 
| 752 |  |  |  |  |  |  | } | 
| 753 |  |  |  |  |  |  |  | 
| 754 |  |  |  |  |  |  | sub draw_2y { | 
| 755 | 1 |  |  | 1 | 0 | 20 | my $self = shift; | 
| 756 |  |  |  |  |  |  |  | 
| 757 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 758 | 1 |  |  |  |  | 1 | my $sheet = shift; | 
| 759 | 1 |  |  |  |  | 2 | my $opt   = shift; | 
| 760 |  |  |  |  |  |  |  | 
| 761 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Workbook | 
| 762 | 1 |  |  |  |  | 2 | my $workbook   = $self->{workbook}; | 
| 763 | 1 |  |  |  |  | 4 | my $sheet_name = $sheet->get_name; | 
| 764 |  |  |  |  |  |  |  | 
| 765 | 1 |  | 33 |  |  | 9 | my $font_name = $opt->{font_name} || $self->{font_name}; | 
| 766 | 1 |  | 33 |  |  | 3 | my $font_size = $opt->{font_size} || $self->{font_size}; | 
| 767 | 1 |  | 33 |  |  | 4 | my $height    = $opt->{height}    || $self->{height}; | 
| 768 | 1 |  | 33 |  |  | 4 | my $width     = $opt->{width}     || $self->{width}; | 
| 769 |  |  |  |  |  |  |  | 
| 770 |  |  |  |  |  |  | # E2 | 
| 771 | 1 |  | 50 |  |  | 4 | my $top  = $opt->{top}  || 1; | 
| 772 | 1 |  | 50 |  |  | 2 | my $left = $opt->{left} || 4; | 
| 773 |  |  |  |  |  |  |  | 
| 774 |  |  |  |  |  |  | # 0 based | 
| 775 | 1 |  |  |  |  | 2 | my $first_row = $opt->{first_row}; | 
| 776 | 1 |  |  |  |  | 1 | my $last_row  = $opt->{last_row}; | 
| 777 | 1 |  |  |  |  | 1 | my $x_column  = $opt->{x_column}; | 
| 778 | 1 |  |  |  |  | 1 | my $y_column  = $opt->{y_column}; | 
| 779 | 1 |  |  |  |  | 1 | my $y2_column = $opt->{y2_column}; | 
| 780 |  |  |  |  |  |  |  | 
| 781 |  |  |  |  |  |  | # Set axes' scale | 
| 782 | 1 |  |  |  |  | 1 | my $x_max_scale = $opt->{x_max_scale}; | 
| 783 | 1 |  |  |  |  | 2 | my $x_min_scale = $opt->{x_min_scale}; | 
| 784 | 1 | 50 |  |  |  | 3 | if ( !defined $x_min_scale ) { | 
| 785 | 1 |  |  |  |  | 1 | $x_min_scale = 0; | 
| 786 |  |  |  |  |  |  | } | 
| 787 | 1 | 50 | 33 |  |  | 5 | if ( !defined $x_max_scale and exists $opt->{x_scale_unit} ) { | 
| 788 | 1 |  |  |  |  | 1 | my $x_scale_unit = $opt->{x_scale_unit}; | 
| 789 | 1 |  |  |  |  | 1 | my $x_min_value  = List::Util::min( @{ $opt->{x_data} } ); | 
|  | 1 |  |  |  |  | 5 |  | 
| 790 | 1 |  |  |  |  | 1 | my $x_max_value  = List::Util::max( @{ $opt->{x_data} } ); | 
|  | 1 |  |  |  |  | 2 |  | 
| 791 | 1 |  |  |  |  | 109 | $x_min_scale = int( $x_min_value / $x_scale_unit ) * $x_scale_unit; | 
| 792 | 1 |  |  |  |  | 44 | $x_max_scale = ( int( $x_max_value / $x_scale_unit ) + 1 ) * $x_scale_unit; | 
| 793 |  |  |  |  |  |  | } | 
| 794 |  |  |  |  |  |  |  | 
| 795 | 1 |  |  |  |  | 2 | my $y_scale; | 
| 796 | 1 | 50 |  |  |  | 6 | if ( exists $opt->{y_data} ) { | 
| 797 | 1 |  |  |  |  | 4 | $y_scale = $self->_find_scale( $opt->{y_data}, $first_row, $last_row ); | 
| 798 |  |  |  |  |  |  | } | 
| 799 |  |  |  |  |  |  |  | 
| 800 | 1 |  |  |  |  | 13 | my $y2_scale; | 
| 801 | 1 | 50 |  |  |  | 13 | if ( exists $opt->{y2_data} ) { | 
| 802 | 1 |  |  |  |  | 3 | $y2_scale = $self->_find_scale( $opt->{y2_data}, $first_row, $last_row ); | 
| 803 |  |  |  |  |  |  | } | 
| 804 |  |  |  |  |  |  |  | 
| 805 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Chart | 
| 806 | 1 |  |  |  |  | 18 | my $chart = $workbook->add_chart( | 
| 807 |  |  |  |  |  |  | type     => 'scatter', | 
| 808 |  |  |  |  |  |  | subtype  => 'straight_with_markers', | 
| 809 |  |  |  |  |  |  | embedded => 1 | 
| 810 |  |  |  |  |  |  | ); | 
| 811 |  |  |  |  |  |  |  | 
| 812 |  |  |  |  |  |  | # [ $sheetname, $row_start, $row_end, $col_start, $col_end ] | 
| 813 |  |  |  |  |  |  | #  #"=$sheetname" . '!$A$2:$A$7', | 
| 814 | 1 |  |  |  |  | 469 | $chart->add_series( | 
| 815 |  |  |  |  |  |  | categories => [ $sheet_name, $first_row, $last_row, $x_column, $x_column ], | 
| 816 |  |  |  |  |  |  | values     => [ $sheet_name, $first_row, $last_row, $y_column, $y_column ], | 
| 817 |  |  |  |  |  |  | ); | 
| 818 |  |  |  |  |  |  |  | 
| 819 |  |  |  |  |  |  | # second Y axis | 
| 820 | 1 |  |  |  |  | 210 | $chart->add_series( | 
| 821 |  |  |  |  |  |  | categories => [ $sheet_name, $first_row, $last_row, $x_column,  $x_column ], | 
| 822 |  |  |  |  |  |  | values     => [ $sheet_name, $first_row, $last_row, $y2_column, $y2_column ], | 
| 823 |  |  |  |  |  |  | marker  => { type => 'square', size => 6, fill => { color => 'white', }, }, | 
| 824 |  |  |  |  |  |  | y2_axis => 1, | 
| 825 |  |  |  |  |  |  | ); | 
| 826 | 1 |  |  |  |  | 174 | $chart->set_size( width => $width, height => $height ); | 
| 827 |  |  |  |  |  |  |  | 
| 828 |  |  |  |  |  |  | # Remove title and legend | 
| 829 | 1 |  |  |  |  | 15 | $chart->set_title( none => 1 ); | 
| 830 | 1 |  |  |  |  | 29 | $chart->set_legend( none => 1 ); | 
| 831 |  |  |  |  |  |  |  | 
| 832 |  |  |  |  |  |  | # Blank data is shown as a gap | 
| 833 | 1 |  |  |  |  | 19 | $chart->show_blanks_as('gap'); | 
| 834 |  |  |  |  |  |  |  | 
| 835 |  |  |  |  |  |  | # set axis | 
| 836 |  |  |  |  |  |  | $chart->set_x_axis( | 
| 837 | 1 |  |  |  |  | 14 | name      => $self->_replace_text( $opt->{x_title} ), | 
| 838 |  |  |  |  |  |  | name_font => { name => $font_name, size => $font_size, }, | 
| 839 |  |  |  |  |  |  | num_font  => { name => $font_name, size => $font_size, }, | 
| 840 |  |  |  |  |  |  | line            => { color   => 'black', }, | 
| 841 |  |  |  |  |  |  | major_gridlines => { visible => 0, }, | 
| 842 |  |  |  |  |  |  | minor_gridlines => { visible => 0, }, | 
| 843 |  |  |  |  |  |  | major_tick_mark => 'inside', | 
| 844 |  |  |  |  |  |  | min             => $x_min_scale, | 
| 845 |  |  |  |  |  |  | max             => $x_max_scale, | 
| 846 |  |  |  |  |  |  | ); | 
| 847 |  |  |  |  |  |  | $chart->set_y_axis( | 
| 848 |  |  |  |  |  |  | name      => $self->_replace_text( $opt->{y_title} ), | 
| 849 |  |  |  |  |  |  | name_font => { name => $font_name, size => $font_size, }, | 
| 850 |  |  |  |  |  |  | num_font  => { name => $font_name, size => $font_size, }, | 
| 851 |  |  |  |  |  |  | line            => { color   => 'black', }, | 
| 852 |  |  |  |  |  |  | major_gridlines => { visible => 0, }, | 
| 853 |  |  |  |  |  |  | minor_gridlines => { visible => 0, }, | 
| 854 |  |  |  |  |  |  | major_tick_mark => 'inside', | 
| 855 |  |  |  |  |  |  | defined $y_scale | 
| 856 |  |  |  |  |  |  | ? ( min => $y_scale->{min}, max => $y_scale->{max}, major_unit => $y_scale->{unit}, ) | 
| 857 | 1 | 50 |  |  |  | 95 | : (), | 
| 858 |  |  |  |  |  |  | ); | 
| 859 |  |  |  |  |  |  | $chart->set_y2_axis( | 
| 860 |  |  |  |  |  |  | name      => $self->_replace_text( $opt->{y2_title} ), | 
| 861 |  |  |  |  |  |  | name_font => { name => $font_name, size => $font_size, }, | 
| 862 |  |  |  |  |  |  | num_font  => { name => $font_name, size => $font_size, }, | 
| 863 |  |  |  |  |  |  | line            => { color   => 'black', }, | 
| 864 |  |  |  |  |  |  | major_gridlines => { visible => 0, }, | 
| 865 |  |  |  |  |  |  | minor_gridlines => { visible => 0, }, | 
| 866 |  |  |  |  |  |  | major_tick_mark => 'inside', | 
| 867 |  |  |  |  |  |  | defined $y2_scale | 
| 868 |  |  |  |  |  |  | ? ( min => $y2_scale->{min}, max => $y2_scale->{max}, major_unit => $y2_scale->{unit}, ) | 
| 869 | 1 | 50 |  |  |  | 74 | : (), | 
| 870 |  |  |  |  |  |  | ); | 
| 871 |  |  |  |  |  |  |  | 
| 872 |  |  |  |  |  |  | # plorarea | 
| 873 | 1 |  |  |  |  | 77 | $chart->set_plotarea( border => { color => 'black', }, ); | 
| 874 |  |  |  |  |  |  |  | 
| 875 | 1 |  |  |  |  | 51 | $sheet->insert_chart( $top, $left, $chart ); | 
| 876 |  |  |  |  |  |  |  | 
| 877 | 1 |  |  |  |  | 32 | return; | 
| 878 |  |  |  |  |  |  | } | 
| 879 |  |  |  |  |  |  |  | 
| 880 |  |  |  |  |  |  | sub draw_xy { | 
| 881 | 1 |  |  | 1 | 0 | 6 | my $self = shift; | 
| 882 |  |  |  |  |  |  |  | 
| 883 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 884 | 1 |  |  |  |  | 2 | my $sheet = shift; | 
| 885 | 1 |  |  |  |  | 1 | my $opt   = shift; | 
| 886 |  |  |  |  |  |  |  | 
| 887 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Workbook | 
| 888 | 1 |  |  |  |  | 2 | my $workbook   = $self->{workbook}; | 
| 889 | 1 |  |  |  |  | 5 | my $sheet_name = $sheet->get_name; | 
| 890 |  |  |  |  |  |  |  | 
| 891 | 1 |  | 33 |  |  | 8 | my $font_name = $opt->{font_name} || $self->{font_name}; | 
| 892 | 1 |  | 33 |  |  | 7 | my $font_size = $opt->{font_size} || $self->{font_size}; | 
| 893 | 1 |  | 33 |  |  | 4 | my $height    = $opt->{height}    || $self->{height}; | 
| 894 | 1 |  | 33 |  |  | 4 | my $width     = $opt->{width}     || $self->{width}; | 
| 895 |  |  |  |  |  |  |  | 
| 896 |  |  |  |  |  |  | # trendline | 
| 897 | 1 |  |  |  |  | 1 | my $add_trend = $opt->{add_trend}; | 
| 898 |  |  |  |  |  |  |  | 
| 899 |  |  |  |  |  |  | # E2 | 
| 900 | 1 |  | 50 |  |  | 4 | my $top  = $opt->{top}  || 1; | 
| 901 | 1 |  | 50 |  |  | 5 | my $left = $opt->{left} || 4; | 
| 902 |  |  |  |  |  |  |  | 
| 903 |  |  |  |  |  |  | # 0 based | 
| 904 | 1 |  |  |  |  | 1 | my $first_row = $opt->{first_row}; | 
| 905 | 1 |  |  |  |  | 3 | my $last_row  = $opt->{last_row}; | 
| 906 | 1 |  |  |  |  | 2 | my $x_column  = $opt->{x_column}; | 
| 907 | 1 |  |  |  |  | 2 | my $y_column  = $opt->{y_column}; | 
| 908 |  |  |  |  |  |  |  | 
| 909 | 1 |  |  |  |  | 1 | my $x_scale; | 
| 910 | 1 | 50 |  |  |  | 3 | if ( exists $opt->{x_data} ) { | 
| 911 | 1 |  |  |  |  | 3 | $x_scale = $self->_find_scale( $opt->{x_data}, $first_row, $last_row ); | 
| 912 |  |  |  |  |  |  |  | 
| 913 |  |  |  |  |  |  | } | 
| 914 | 1 |  |  |  |  | 15 | my $y_scale; | 
| 915 | 1 | 50 |  |  |  | 5 | if ( exists $opt->{y_data} ) { | 
| 916 | 1 |  |  |  |  | 4 | $y_scale = $self->_find_scale( $opt->{y_data}, $first_row, $last_row ); | 
| 917 |  |  |  |  |  |  | } | 
| 918 |  |  |  |  |  |  |  | 
| 919 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Chart | 
| 920 | 1 |  |  |  |  | 20 | my $chart = $workbook->add_chart( type => 'scatter', embedded => 1 ); | 
| 921 |  |  |  |  |  |  |  | 
| 922 |  |  |  |  |  |  | # [ $sheetname, $row_start, $row_end, $col_start, $col_end ] | 
| 923 |  |  |  |  |  |  | #  #"=$sheetname" . '!$A$2:$A$7', | 
| 924 | 1 | 50 |  |  |  | 488 | $chart->add_series( | 
| 925 |  |  |  |  |  |  | categories => [ $sheet_name, $first_row, $last_row, $x_column, $x_column ], | 
| 926 |  |  |  |  |  |  | values     => [ $sheet_name, $first_row, $last_row, $y_column, $y_column ], | 
| 927 |  |  |  |  |  |  | marker => { type => 'diamond' }, | 
| 928 |  |  |  |  |  |  | $add_trend | 
| 929 |  |  |  |  |  |  | ? ( trendline => { | 
| 930 |  |  |  |  |  |  | type => 'linear', | 
| 931 |  |  |  |  |  |  | name => 'Linear Trend', | 
| 932 |  |  |  |  |  |  | } | 
| 933 |  |  |  |  |  |  | ) | 
| 934 |  |  |  |  |  |  | : (), | 
| 935 |  |  |  |  |  |  | ); | 
| 936 | 1 |  |  |  |  | 258 | $chart->set_size( width => $width, height => $height ); | 
| 937 |  |  |  |  |  |  |  | 
| 938 |  |  |  |  |  |  | # Remove title and legend | 
| 939 | 1 |  |  |  |  | 15 | $chart->set_title( none => 1 ); | 
| 940 | 1 |  |  |  |  | 28 | $chart->set_legend( none => 1 ); | 
| 941 |  |  |  |  |  |  |  | 
| 942 |  |  |  |  |  |  | # Blank data is shown as a gap | 
| 943 | 1 |  |  |  |  | 19 | $chart->show_blanks_as('gap'); | 
| 944 |  |  |  |  |  |  |  | 
| 945 |  |  |  |  |  |  | # set axis | 
| 946 |  |  |  |  |  |  | $chart->set_x_axis( | 
| 947 |  |  |  |  |  |  | name      => $self->_replace_text( $opt->{x_title} ), | 
| 948 |  |  |  |  |  |  | name_font => { name => $font_name, size => $font_size, }, | 
| 949 |  |  |  |  |  |  | num_font  => { name => $font_name, size => $font_size, }, | 
| 950 |  |  |  |  |  |  | line            => { color   => 'black', }, | 
| 951 |  |  |  |  |  |  | major_gridlines => { visible => 0, }, | 
| 952 |  |  |  |  |  |  | minor_gridlines => { visible => 0, }, | 
| 953 |  |  |  |  |  |  | major_tick_mark => 'inside', | 
| 954 |  |  |  |  |  |  | defined $x_scale | 
| 955 |  |  |  |  |  |  | ? ( min => $x_scale->{min}, max => $x_scale->{max}, major_unit => $x_scale->{unit}, ) | 
| 956 | 1 | 50 |  |  |  | 14 | : (), | 
| 957 |  |  |  |  |  |  | ); | 
| 958 |  |  |  |  |  |  | $chart->set_y_axis( | 
| 959 |  |  |  |  |  |  | name      => $self->_replace_text( $opt->{y_title} ), | 
| 960 |  |  |  |  |  |  | name_font => { name => $font_name, size => $font_size, }, | 
| 961 |  |  |  |  |  |  | num_font  => { name => $font_name, size => $font_size, }, | 
| 962 |  |  |  |  |  |  | line            => { color   => 'black', }, | 
| 963 |  |  |  |  |  |  | major_gridlines => { visible => 0, }, | 
| 964 |  |  |  |  |  |  | minor_gridlines => { visible => 0, }, | 
| 965 |  |  |  |  |  |  | major_tick_mark => 'inside', | 
| 966 |  |  |  |  |  |  | defined $y_scale | 
| 967 |  |  |  |  |  |  | ? ( min => $y_scale->{min}, max => $y_scale->{max}, major_unit => $y_scale->{unit}, ) | 
| 968 | 1 | 50 |  |  |  | 105 | : (), | 
| 969 |  |  |  |  |  |  | ); | 
| 970 |  |  |  |  |  |  |  | 
| 971 |  |  |  |  |  |  | # plorarea | 
| 972 | 1 |  |  |  |  | 79 | $chart->set_plotarea( border => { color => 'black', }, ); | 
| 973 |  |  |  |  |  |  |  | 
| 974 | 1 |  |  |  |  | 53 | $sheet->insert_chart( $top, $left, $chart ); | 
| 975 |  |  |  |  |  |  |  | 
| 976 | 1 |  |  |  |  | 51 | return; | 
| 977 |  |  |  |  |  |  | } | 
| 978 |  |  |  |  |  |  |  | 
| 979 |  |  |  |  |  |  | sub draw_dd { | 
| 980 | 1 |  |  | 1 | 0 | 32 | my $self = shift; | 
| 981 |  |  |  |  |  |  |  | 
| 982 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Worksheet | 
| 983 | 1 |  |  |  |  | 1 | my $sheet = shift; | 
| 984 | 1 |  |  |  |  | 2 | my $opt   = shift; | 
| 985 |  |  |  |  |  |  |  | 
| 986 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Workbook | 
| 987 | 1 |  |  |  |  | 2 | my $workbook   = $self->{workbook}; | 
| 988 | 1 |  |  |  |  | 5 | my $sheet_name = $sheet->get_name; | 
| 989 |  |  |  |  |  |  |  | 
| 990 | 1 |  | 33 |  |  | 8 | my $font_name = $opt->{font_name} || $self->{font_name}; | 
| 991 | 1 |  | 33 |  |  | 4 | my $font_size = $opt->{font_size} || $self->{font_size}; | 
| 992 | 1 |  | 33 |  |  | 4 | my $height    = $opt->{height}    || $self->{height}; | 
| 993 | 1 |  | 33 |  |  | 3 | my $width     = $opt->{width}     || $self->{width}; | 
| 994 |  |  |  |  |  |  |  | 
| 995 |  |  |  |  |  |  | # E2 | 
| 996 | 1 |  | 50 |  |  | 3 | my $top  = $opt->{top}  || 1; | 
| 997 | 1 |  | 50 |  |  | 2 | my $left = $opt->{left} || 4; | 
| 998 |  |  |  |  |  |  |  | 
| 999 |  |  |  |  |  |  | # 0 based | 
| 1000 | 1 |  |  |  |  | 2 | my $first_row     = $opt->{first_row}; | 
| 1001 | 1 |  |  |  |  | 1 | my $last_row      = $opt->{last_row}; | 
| 1002 | 1 |  |  |  |  | 2 | my $x_column      = $opt->{x_column}; | 
| 1003 | 1 |  |  |  |  | 1 | my $y_column      = $opt->{y_column}; | 
| 1004 | 1 |  |  |  |  | 1 | my $y_last_column = $opt->{y_last_column}; | 
| 1005 | 1 | 50 |  |  |  | 2 | unless ( defined $y_last_column ) { | 
| 1006 | 0 |  |  |  |  | 0 | $y_last_column = $y_column; | 
| 1007 |  |  |  |  |  |  | } | 
| 1008 |  |  |  |  |  |  |  | 
| 1009 |  |  |  |  |  |  | # Set axes' scale | 
| 1010 | 1 |  |  |  |  | 2 | my $x_max_scale = $opt->{x_max_scale}; | 
| 1011 | 1 |  |  |  |  | 1 | my $x_min_scale = $opt->{x_min_scale}; | 
| 1012 | 1 | 50 |  |  |  | 2 | if ( !defined $x_min_scale ) { | 
| 1013 | 0 |  |  |  |  | 0 | $x_min_scale = 0; | 
| 1014 |  |  |  |  |  |  | } | 
| 1015 | 1 | 0 | 33 |  |  | 3 | if ( !defined $x_max_scale and exists $opt->{x_scale_unit} ) { | 
| 1016 | 0 |  |  |  |  | 0 | my $x_scale_unit = $opt->{x_scale_unit}; | 
| 1017 | 0 |  |  |  |  | 0 | my $x_min_value  = List::Util::min( @{ $opt->{x_data} } ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 1018 | 0 |  |  |  |  | 0 | my $x_max_value  = List::Util::max( @{ $opt->{x_data} } ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 1019 | 0 |  |  |  |  | 0 | $x_min_scale = int( $x_min_value / $x_scale_unit ) * $x_scale_unit; | 
| 1020 | 0 |  |  |  |  | 0 | $x_max_scale = ( int( $x_max_value / $x_scale_unit ) + 1 ) * $x_scale_unit; | 
| 1021 |  |  |  |  |  |  | } | 
| 1022 |  |  |  |  |  |  |  | 
| 1023 | 1 |  |  |  |  | 2 | my $y_scale; | 
| 1024 | 1 | 50 |  |  |  | 3 | if ( exists $opt->{y_data} ) { | 
| 1025 | 1 |  |  |  |  | 4 | $y_scale = $self->_find_scale( $opt->{y_data} ); | 
| 1026 |  |  |  |  |  |  | } | 
| 1027 |  |  |  |  |  |  |  | 
| 1028 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Chart | 
| 1029 | 1 |  |  |  |  | 65 | my $chart = $workbook->add_chart( | 
| 1030 |  |  |  |  |  |  | type     => 'line', | 
| 1031 |  |  |  |  |  |  | embedded => 1 | 
| 1032 |  |  |  |  |  |  | ); | 
| 1033 |  |  |  |  |  |  |  | 
| 1034 |  |  |  |  |  |  | # [ $sheetname, $row_start, $row_end, $col_start, $col_end ] | 
| 1035 |  |  |  |  |  |  | #  #"=$sheetname" . '!$A$2:$A$7', | 
| 1036 | 1 |  |  |  |  | 1477 | for my $y_col ( $y_column .. $y_last_column ) { | 
| 1037 | 2 |  |  |  |  | 252 | $chart->add_series( | 
| 1038 |  |  |  |  |  |  | categories => [ $sheet_name, $first_row, $last_row, $x_column, $x_column ], | 
| 1039 |  |  |  |  |  |  | values     => [ $sheet_name, $first_row, $last_row, $y_col,    $y_col ], | 
| 1040 |  |  |  |  |  |  | ); | 
| 1041 |  |  |  |  |  |  | } | 
| 1042 | 1 |  |  |  |  | 136 | $chart->set_size( width => $width, height => $height ); | 
| 1043 |  |  |  |  |  |  |  | 
| 1044 |  |  |  |  |  |  | # Remove title and legend | 
| 1045 | 1 |  |  |  |  | 17 | $chart->set_title( none => 1 ); | 
| 1046 | 1 |  |  |  |  | 35 | $chart->set_legend( none => 1 ); | 
| 1047 |  |  |  |  |  |  |  | 
| 1048 |  |  |  |  |  |  | # Blank data is shown as a gap | 
| 1049 | 1 |  |  |  |  | 20 | $chart->show_blanks_as('gap'); | 
| 1050 |  |  |  |  |  |  |  | 
| 1051 |  |  |  |  |  |  | # set axis | 
| 1052 |  |  |  |  |  |  | $chart->set_x_axis( | 
| 1053 |  |  |  |  |  |  | name      => $self->_replace_text( $opt->{x_title} ), | 
| 1054 |  |  |  |  |  |  | name_font => { name => $font_name, size => $font_size, }, | 
| 1055 |  |  |  |  |  |  | num_font  => { name => $font_name, size => $font_size, }, | 
| 1056 |  |  |  |  |  |  | line            => { color   => 'black', }, | 
| 1057 |  |  |  |  |  |  | major_gridlines => { visible => 0, }, | 
| 1058 |  |  |  |  |  |  | minor_gridlines => { visible => 0, }, | 
| 1059 |  |  |  |  |  |  | major_tick_mark => 'inside', | 
| 1060 |  |  |  |  |  |  | min             => $x_min_scale, | 
| 1061 |  |  |  |  |  |  | max             => $x_max_scale, | 
| 1062 | 1 | 50 |  |  |  | 13 | exists $opt->{cross} ? ( crossing => $opt->{cross}, ) : (), | 
| 1063 |  |  |  |  |  |  | ); | 
| 1064 |  |  |  |  |  |  | $chart->set_y_axis( | 
| 1065 |  |  |  |  |  |  | name      => $self->_replace_text( $opt->{y_title} ), | 
| 1066 |  |  |  |  |  |  | name_font => { name => $font_name, size => $font_size, }, | 
| 1067 |  |  |  |  |  |  | num_font  => { name => $font_name, size => $font_size, }, | 
| 1068 |  |  |  |  |  |  | line            => { color   => 'black', }, | 
| 1069 |  |  |  |  |  |  | major_gridlines => { visible => 0, }, | 
| 1070 |  |  |  |  |  |  | minor_gridlines => { visible => 0, }, | 
| 1071 |  |  |  |  |  |  | major_tick_mark => 'inside', | 
| 1072 |  |  |  |  |  |  | defined $y_scale | 
| 1073 |  |  |  |  |  |  | ? ( min => $y_scale->{min}, max => $y_scale->{max}, major_unit => $y_scale->{unit}, ) | 
| 1074 | 1 | 50 |  |  |  | 114 | : (), | 
| 1075 |  |  |  |  |  |  | ); | 
| 1076 |  |  |  |  |  |  |  | 
| 1077 |  |  |  |  |  |  | # plorarea | 
| 1078 | 1 |  |  |  |  | 86 | $chart->set_plotarea( border => { color => 'black', }, ); | 
| 1079 |  |  |  |  |  |  |  | 
| 1080 | 1 |  |  |  |  | 56 | $sheet->insert_chart( $top, $left, $chart ); | 
| 1081 |  |  |  |  |  |  |  | 
| 1082 | 1 |  |  |  |  | 48 | return; | 
| 1083 |  |  |  |  |  |  | } | 
| 1084 |  |  |  |  |  |  |  | 
| 1085 |  |  |  |  |  |  | sub _find_scale { | 
| 1086 | 6 |  |  | 6 |  | 11 | my $self      = shift; | 
| 1087 | 6 |  |  |  |  | 8 | my $dataset   = shift; | 
| 1088 | 6 |  |  |  |  | 7 | my $first_row = shift; | 
| 1089 | 6 |  |  |  |  | 40 | my $last_row  = shift; | 
| 1090 |  |  |  |  |  |  |  | 
| 1091 | 6 |  |  |  |  | 42 | my $axis = Chart::Math::Axis->new; | 
| 1092 |  |  |  |  |  |  |  | 
| 1093 | 6 |  |  |  |  | 63 | my @data; | 
| 1094 | 6 | 100 |  |  |  | 15 | if ( !defined $first_row ) { | 
| 1095 | 1 | 50 |  |  |  | 3 | if ( ref $dataset->[0] eq 'ARRAY' ) { | 
| 1096 | 1 |  |  |  |  | 2 | for ( @{$dataset} ) { | 
|  | 1 |  |  |  |  | 2 |  | 
| 1097 | 2 |  |  |  |  | 3 | push @data, @{$_}; | 
|  | 2 |  |  |  |  | 5 |  | 
| 1098 |  |  |  |  |  |  | } | 
| 1099 |  |  |  |  |  |  | } | 
| 1100 |  |  |  |  |  |  | else { | 
| 1101 | 0 |  |  |  |  | 0 | push @data, @{$dataset}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 1102 |  |  |  |  |  |  | } | 
| 1103 |  |  |  |  |  |  | } | 
| 1104 |  |  |  |  |  |  | else { | 
| 1105 | 5 | 50 |  |  |  | 15 | if ( ref $dataset->[0] eq 'ARRAY' ) { | 
| 1106 | 0 |  |  |  |  | 0 | for ( @{$dataset} ) { | 
|  | 0 |  |  |  |  | 0 |  | 
| 1107 | 0 |  |  |  |  | 0 | my @copy = @{$_}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 1108 | 0 |  |  |  |  | 0 | push @data, splice( @copy, $first_row - 1, $last_row - $first_row + 1 ); | 
| 1109 |  |  |  |  |  |  | } | 
| 1110 |  |  |  |  |  |  | } | 
| 1111 |  |  |  |  |  |  | else { | 
| 1112 | 5 |  |  |  |  | 6 | my @copy = @{$dataset}; | 
|  | 5 |  |  |  |  | 24 |  | 
| 1113 | 5 |  |  |  |  | 21 | push @data, splice( @copy, $first_row - 1, $last_row - $first_row + 1 ); | 
| 1114 |  |  |  |  |  |  | } | 
| 1115 |  |  |  |  |  |  | } | 
| 1116 |  |  |  |  |  |  |  | 
| 1117 | 6 |  |  |  |  | 26 | $axis->add_data(@data); | 
| 1118 | 6 |  |  |  |  | 59696 | $axis->set_maximum_intervals( $self->{max_ticks} ); | 
| 1119 |  |  |  |  |  |  |  | 
| 1120 |  |  |  |  |  |  | return { | 
| 1121 | 6 |  |  |  |  | 39081 | max  => $axis->top, | 
| 1122 |  |  |  |  |  |  | min  => $axis->bottom, | 
| 1123 |  |  |  |  |  |  | unit => $axis->interval_size, | 
| 1124 |  |  |  |  |  |  | }; | 
| 1125 |  |  |  |  |  |  | } | 
| 1126 |  |  |  |  |  |  |  | 
| 1127 |  |  |  |  |  |  | sub _replace_text { | 
| 1128 | 9 |  |  | 9 |  | 11 | my $self    = shift; | 
| 1129 | 9 |  |  |  |  | 12 | my $text    = shift; | 
| 1130 | 9 |  |  |  |  | 12 | my $replace = $self->{replace}; | 
| 1131 |  |  |  |  |  |  |  | 
| 1132 | 9 |  |  |  |  | 18 | for my $key ( keys %$replace ) { | 
| 1133 | 2 |  |  |  |  | 3 | my $value = $replace->{$key}; | 
| 1134 | 2 |  |  |  |  | 17 | $text =~ s/$key/$value/gi; | 
| 1135 |  |  |  |  |  |  | } | 
| 1136 |  |  |  |  |  |  |  | 
| 1137 | 9 |  |  |  |  | 106 | return $text; | 
| 1138 |  |  |  |  |  |  | } | 
| 1139 |  |  |  |  |  |  |  | 
| 1140 |  |  |  |  |  |  | # instance destructor | 
| 1141 |  |  |  |  |  |  | # invoked only as object method | 
| 1142 |  |  |  |  |  |  | sub DESTROY { | 
| 1143 | 5 |  |  | 5 |  | 1764 | my $self = shift; | 
| 1144 |  |  |  |  |  |  |  | 
| 1145 |  |  |  |  |  |  | # close excel objects | 
| 1146 |  |  |  |  |  |  | #@type Excel::Writer::XLSX::Workbook | 
| 1147 | 5 |  |  |  |  | 13 | my $workbook = $self->{workbook}; | 
| 1148 | 5 | 50 |  |  |  | 57 | $workbook->close if $workbook; | 
| 1149 |  |  |  |  |  |  |  | 
| 1150 |  |  |  |  |  |  | # close dbh | 
| 1151 |  |  |  |  |  |  | #@type DBI | 
| 1152 | 5 |  |  |  |  | 130523 | my $dbh = $self->{dbh}; | 
| 1153 | 5 | 100 |  |  |  | 99 | $dbh->disconnect if $dbh; | 
| 1154 |  |  |  |  |  |  |  | 
| 1155 | 5 |  |  |  |  | 231 | return; | 
| 1156 |  |  |  |  |  |  | } | 
| 1157 |  |  |  |  |  |  |  | 
| 1158 |  |  |  |  |  |  | 1; | 
| 1159 |  |  |  |  |  |  |  | 
| 1160 |  |  |  |  |  |  | __END__ | 
| 1161 |  |  |  |  |  |  |  | 
| 1162 |  |  |  |  |  |  | =head1 NAME | 
| 1163 |  |  |  |  |  |  |  | 
| 1164 |  |  |  |  |  |  | AlignDB::ToXLSX - Create xlsx files from arrays or SQL queries. | 
| 1165 |  |  |  |  |  |  |  | 
| 1166 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 1167 |  |  |  |  |  |  |  | 
| 1168 |  |  |  |  |  |  | # Mysql | 
| 1169 |  |  |  |  |  |  | my $write_obj = AlignDB::ToXLSX->new( | 
| 1170 |  |  |  |  |  |  | outfile => $outfile, | 
| 1171 |  |  |  |  |  |  | dbh     => $dbh, | 
| 1172 |  |  |  |  |  |  | ); | 
| 1173 |  |  |  |  |  |  |  | 
| 1174 |  |  |  |  |  |  | # MongoDB | 
| 1175 |  |  |  |  |  |  | my $write_obj = AlignDB::ToXLSX->new( | 
| 1176 |  |  |  |  |  |  | outfile => $outfile, | 
| 1177 |  |  |  |  |  |  | ); | 
| 1178 |  |  |  |  |  |  |  | 
| 1179 |  |  |  |  |  |  | =head1 AUTHOR | 
| 1180 |  |  |  |  |  |  |  | 
| 1181 |  |  |  |  |  |  | Qiang Wang <wang-q@outlook.com> | 
| 1182 |  |  |  |  |  |  |  | 
| 1183 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 1184 |  |  |  |  |  |  |  | 
| 1185 |  |  |  |  |  |  | This software is copyright (c) 2008 by Qiang Wang. | 
| 1186 |  |  |  |  |  |  |  | 
| 1187 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 1188 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 1189 |  |  |  |  |  |  |  | 
| 1190 |  |  |  |  |  |  | =cut |