| 1 | package Excel::Writer::XLSX::CDF; | ||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use List::MoreUtils qw{first_index}; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | our $VERSION = '0.01'; | |||||||||||
| 7 | |||||||||||||
| 8 | =head1 NAME | ||||||||||||
| 9 | |||||||||||||
| 10 | Excel::Writer::XLSX::CDF - Generates Excel Document with Continuous Distribution Function Chart | ||||||||||||
| 11 | |||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||
| 13 | |||||||||||||
| 14 | use Excel::Writer::XLSX::CDF; | ||||||||||||
| 15 | my $writer = Excel::Writer::XLSX::CDF->new( | ||||||||||||
| 16 | chart_title => "My Title", | ||||||||||||
| 17 | group_names_sort => 1, | ||||||||||||
| 18 | ); | ||||||||||||
| 19 | my @data = ( | ||||||||||||
| 20 | [group_name_A => 0.11], #group name is used to lable the chart series | ||||||||||||
| 21 | [group_name_A => 0.21], | ||||||||||||
| 22 | [group_name_A => 0.31], | ||||||||||||
| 23 | [group_name_A => 0.41], | ||||||||||||
| 24 | [group_name_B => 0.07], | ||||||||||||
| 25 | [group_name_B => 0.13], | ||||||||||||
| 26 | [group_name_Z => 0.10], | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | my $blob = $writer->generate(\@data); #returns Excel File in memory | ||||||||||||
| 29 | my $filename = $writer->generate_file(\@data); #returns Excel File in tmp folder | ||||||||||||
| 30 | |||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||
| 32 | |||||||||||||
| 33 | Generates Excel Document with Continuous Distribution Function Chart from the supplied data. | ||||||||||||
| 34 | |||||||||||||
| 35 | =head1 CONSTRUCTOR | ||||||||||||
| 36 | |||||||||||||
| 37 | =head2 new | ||||||||||||
| 38 | |||||||||||||
| 39 | my $writer = Excel::Writer::XLSX::CDF->new( | ||||||||||||
| 40 | chart_title => "Continuous Distribution Function (CDF)", | ||||||||||||
| 41 | chart_y_label => "Distribution", | ||||||||||||
| 42 | chart_x_label => "", | ||||||||||||
| 43 | group_names_sort => 0, #default 0 is in order of appearance in data | ||||||||||||
| 44 | ); | ||||||||||||
| 45 | |||||||||||||
| 46 | =cut | ||||||||||||
| 47 | |||||||||||||
| 48 | sub new { | ||||||||||||
| 49 | 2 | ▶ | my $this = shift; | ||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 50 | 2 | ▶ | my $class = ref($this) ? ref($this) : $this; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 2 | my $self = {@_}; | |||||||||||
| 52 | 2 | bless $self, $class; | |||||||||||
| 53 | 2 | return $self; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 PROPERTIES | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 chart_title | ||||||||||||
| 59 | |||||||||||||
| 60 | Set and returns the title of the Excel chart | ||||||||||||
| 61 | |||||||||||||
| 62 | Default: Continuous Distribution Function (CDF) | ||||||||||||
| 63 | |||||||||||||
| 64 | =cut | ||||||||||||
| 65 | |||||||||||||
| 66 | sub chart_title { | ||||||||||||
| 67 | 4 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
chart_title: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 68 | 4 | ▶ | $self->{'chart_title'} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | 4 | ▶ | $self->{'chart_title'} = 'Continuous Distribution Function (CDF)' unless defined $self->{'chart_title'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 4 | return $self->{'chart_title'}; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | =head2 chart_y_label | ||||||||||||
| 74 | |||||||||||||
| 75 | Set and returns the Y axis label of the Excel chart | ||||||||||||
| 76 | |||||||||||||
| 77 | Default: Distribution | ||||||||||||
| 78 | |||||||||||||
| 79 | =cut | ||||||||||||
| 80 | |||||||||||||
| 81 | sub chart_y_label { | ||||||||||||
| 82 | 4 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
chart_y_label: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 83 | 4 | ▶ | $self->{'chart_y_label'} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 4 | ▶ | $self->{'chart_y_label'} = 'Distribution' unless defined $self->{'chart_y_label'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 4 | return $self->{'chart_y_label'}; | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | =head2 chart_x_label | ||||||||||||
| 89 | |||||||||||||
| 90 | Set and returns the X axis label of the Excel chart | ||||||||||||
| 91 | |||||||||||||
| 92 | Default: "" | ||||||||||||
| 93 | |||||||||||||
| 94 | =cut | ||||||||||||
| 95 | |||||||||||||
| 96 | sub chart_x_label { | ||||||||||||
| 97 | 4 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
chart_x_label: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 98 | 4 | ▶ | $self->{'chart_x_label'} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | 4 | ▶ | $self->{'chart_x_label'} = '' unless defined $self->{'chart_x_label'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 4 | return $self->{'chart_x_label'}; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | =head2 chart_legend_display | ||||||||||||
| 104 | |||||||||||||
| 105 | Set and returns the legend display property for the Excel chart | ||||||||||||
| 106 | |||||||||||||
| 107 | Default: "" | ||||||||||||
| 108 | |||||||||||||
| 109 | =cut | ||||||||||||
| 110 | |||||||||||||
| 111 | sub chart_legend_display { | ||||||||||||
| 112 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
chart_legend_display: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 113 | 0 | ▶ | $self->{'chart_legend_display'} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 0 | ▶ | $self->{'chart_legend_display'} = 1 unless defined $self->{'chart_legend_display'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 0 | return $self->{'chart_legend_display'}; | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | =head2 chart_colors | ||||||||||||
| 119 | |||||||||||||
| 120 | Set and Returns an array reference of Excel color codes to use for each CDF in group order. | ||||||||||||
| 121 | |||||||||||||
| 122 | Default: ['#FF0000', '#800000', '#FFFF00', '#808000', '#00FF00', '#008000', '#00FFFF', '#008080', '#0000FF', '#000080', '#FF00FF', '#800080'] | ||||||||||||
| 123 | |||||||||||||
| 124 | =cut | ||||||||||||
| 125 | |||||||||||||
| 126 | sub chart_colors { | ||||||||||||
| 127 | 4 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
chart_colors: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 128 | 4 | ▶ | $self->{'chart_colors'} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 129 | $self->{'chart_colors'} = ['#FF0000', '#800000', '#FFFF00', '#808000', | ||||||||||||
| 130 | '#00FF00', '#008000', '#00FFFF', '#008080', | ||||||||||||
| 131 | 4 | ▶ | '#0000FF', '#000080', '#FF00FF', '#800080'] unless $self->{'chart_colors'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 132 | 4 | ▶ | die('Error: chart_colors property must be and array reference') unless ref($self->{'chart_colors'}) eq 'ARRAY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | 4 | return $self->{'chart_colors'}; | |||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | =head2 group_names_sort | ||||||||||||
| 137 | |||||||||||||
| 138 | Set and returns the alphabetical sort option for the group names. A true value Perl-wise will sort the group names before generating the Excel Workbook and a false value will use the order in which the groups were discovered in the data to generate the group names order. | ||||||||||||
| 139 | |||||||||||||
| 140 | Default: "" | ||||||||||||
| 141 | |||||||||||||
| 142 | =cut | ||||||||||||
| 143 | |||||||||||||
| 144 | sub group_names_sort { | ||||||||||||
| 145 | 4 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
group_names_sort: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 146 | 4 | ▶ | $self->{'group_names_sort'} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 147 | 4 | ▶ | $self->{'group_names_sort'} = '' unless defined $self->{'group_names_sort'}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 4 | return $self->{'group_names_sort'}; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | =head1 METHODS | ||||||||||||
| 152 | |||||||||||||
| 153 | =head2 generate | ||||||||||||
| 154 | |||||||||||||
| 155 | Generates an Excel Workbook in memory and returns the Workbook as a data blob stored in the returned scalar variable. | ||||||||||||
| 156 | |||||||||||||
| 157 | my $blob = $writer->generate(\@data); | ||||||||||||
| 158 | |||||||||||||
| 159 | =cut | ||||||||||||
| 160 | |||||||||||||
| 161 | sub generate { | ||||||||||||
| 162 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
generate: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||
| 163 | 0 | my $data = shift; #isa [[group=>value], [], [], ...] | |||||||||||
| 164 | 0 | my @groups = (); | |||||||||||
| 165 | 0 | my $series = {}; | |||||||||||
| 166 | |||||||||||||
| 167 | 0 | foreach my $row (@$data) { | |||||||||||
| 168 | 0 | my $group = $row->[0]; | |||||||||||
| 169 | 0 | my $value = $row->[1]; | |||||||||||
| 170 | 0 | ▶ | if (not exists $series->{$group}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 171 | 0 | push @groups, $group; #keep order | |||||||||||
| 172 | 0 | $series->{$group} = {count=>0, values=>[]}; | |||||||||||
| 173 | } | ||||||||||||
| 174 | 0 | $series->{$group}->{'count'}++; | |||||||||||
| 175 | 0 | push @{$series->{$group}->{'values'}}, $value; | |||||||||||
| 176 | } | ||||||||||||
| 177 | |||||||||||||
| 178 | 0 | ▶ | if ($self->group_names_sort) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 179 | 0 | @groups = sort @groups; | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | #Open string scalar reference as file handle for Excel::Writer::XLSX to write to | ||||||||||||
| 183 | 0 | ▶ | open my $fh, '>', \my $content or die("Error: Filehandle open error: $!"); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 184 | |||||||||||||
| 185 | #Object for Excel Workbook | ||||||||||||
| 186 | 0 | require Excel::Writer::XLSX; | |||||||||||
| 187 | 0 | my $workbook = Excel::Writer::XLSX->new($fh); | |||||||||||
| 188 | |||||||||||||
| 189 | #Add a worksheet chart as first tab so it shows when document is opened | ||||||||||||
| 190 | 0 | my $chart = $workbook->add_chart(type=>'scatter', subtype=>'straight'); | |||||||||||
| 191 | |||||||||||||
| 192 | #Add worksheet for chart legend groups | ||||||||||||
| 193 | 0 | my $worksheet_groups = $workbook->add_worksheet('groups'); | |||||||||||
| 194 | 0 | $worksheet_groups->write_string(0, 0, 'Group'); | |||||||||||
| 195 | 0 | $worksheet_groups->write_string(0, 1, 'Index'); | |||||||||||
| 196 | 0 | $worksheet_groups->write_string(0, 2, 'Count'); | |||||||||||
| 197 | 0 | my $group_index = 0; | |||||||||||
| 198 | |||||||||||||
| 199 | #Colors for data series lines and legend | ||||||||||||
| 200 | 0 | my @colors = @{$self->chart_colors}; | |||||||||||
| 201 | |||||||||||||
| 202 | #foreach group add worksheet, data and chart series | ||||||||||||
| 203 | 0 | my @stats_groups = (); | |||||||||||
| 204 | |||||||||||||
| 205 | 0 | foreach my $group (@groups) { | |||||||||||
| 206 | |||||||||||||
| 207 | #Add series label for legend | ||||||||||||
| 208 | 0 | $group_index++; | |||||||||||
| 209 | 0 | $worksheet_groups->write_string($group_index, 0, $group); | |||||||||||
| 210 | 0 | $worksheet_groups->write_number($group_index, 1, $group_index); | |||||||||||
| 211 | |||||||||||||
| 212 | #Add worksheet | ||||||||||||
| 213 | 0 | my $worksheet = $workbook->add_worksheet("group_$group_index"); | |||||||||||
| 214 | |||||||||||||
| 215 | #Add data to worksheet | ||||||||||||
| 216 | 0 | my @values = sort {$a <=> $b} @{$series->{$group}->{'values'}}; | |||||||||||
| 217 | 0 | my $values_count = scalar(@values); | |||||||||||
| 218 | 0 | $worksheet_groups->write_number($group_index, 2, $values_count); | |||||||||||
| 219 | 0 | my @cdf = (); | |||||||||||
| 220 | 0 | my $loop = 0; | |||||||||||
| 221 | 0 | foreach my $value (@values) { | |||||||||||
| 222 | 0 | $loop++; | |||||||||||
| 223 | 0 | push @cdf, $loop/$values_count; | |||||||||||
| 224 | } | ||||||||||||
| 225 | 0 | $worksheet->write_row(A1 => [$group, 'Probability'] ); | |||||||||||
| 226 | 0 | $worksheet->write_row(A2 => [\@values, \@cdf] ); | |||||||||||
| 227 | |||||||||||||
| 228 | 0 | my $stat = {}; | |||||||||||
| 229 | 0 | $stat->{'min'} = $values[0]; | |||||||||||
| 230 | 0 | $stat->{'max'} = $values[-1]; | |||||||||||
| 231 | 0 | ▶ | my $p50_group_index = first_index {$_ >= 0.5} @cdf; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 232 | 0 | $stat->{'p50'} = $values[$p50_group_index]; | |||||||||||
| 233 | 0 | ▶ | my $p90_group_index = first_index {$_ >= 0.9} @cdf; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 234 | 0 | $stat->{'p90'} = $values[$p90_group_index]; | |||||||||||
| 235 | |||||||||||||
| 236 | 0 | push @stats_groups, $stat; | |||||||||||
| 237 | |||||||||||||
| 238 | #Add data references to chart | ||||||||||||
| 239 | 0 | ▶ | my $color = shift @colors || 'black'; | ||||||||||
|
Condition0%0/2
shift @colors || 'black'
MC/DC0%0/1
shift @colors || 'black'
shift @colors
Truth table0%0/2
A
A= shift @colors
| |||||||||||||
| 240 | 0 | $chart->add_series( | |||||||||||
| 241 | line => {color => $color}, | ||||||||||||
| 242 | name => sprintf('=%s!$A$%s', 'groups', $group_index + 1), #groups header row is 0 | ||||||||||||
| 243 | categories => sprintf('=%s!$A$2:$A$%s', $worksheet->get_name, $values_count + 1), | ||||||||||||
| 244 | values => sprintf('=%s!$B$2:$B$%s', $worksheet->get_name, $values_count + 1), | ||||||||||||
| 245 | ); | ||||||||||||
| 246 | |||||||||||||
| 247 | } #foreach group | ||||||||||||
| 248 | |||||||||||||
| 249 | 0 | my $maxset; | |||||||||||
| 250 | 0 | ▶ | if (@stats_groups) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 251 | 0 | require List::Util; | |||||||||||
| 252 | 0 | require Math::Round::SignificantFigures; | |||||||||||
| 253 | |||||||||||||
| 254 | 0 | my $max = List::Util::max(map {$_->{'max'}} @stats_groups); | |||||||||||
| 255 | 0 | my $p50 = Math::Round::SignificantFigures::ceilsigfigs(List::Util::max(map {$_->{'p50'}} @stats_groups) * 1.5, 2); | |||||||||||
| 256 | 0 | my $p90 = Math::Round::SignificantFigures::ceilsigfigs(List::Util::min(map {$_->{'p90'}} @stats_groups) * 1.5, 2); | |||||||||||
| 257 | 0 | $maxset = List::Util::max($p50, $p90); | |||||||||||
| 258 | 0 | ▶ | $maxset = undef if $maxset > $max; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 259 | } | ||||||||||||
| 260 | |||||||||||||
| 261 | #Configure chart | ||||||||||||
| 262 | 0 | $chart->set_title( name => $self->chart_title ); | |||||||||||
| 263 | 0 | $chart->set_y_axis(name => $self->chart_y_label, min => 0 , max => 1 ); | |||||||||||
| 264 | 0 | $chart->set_x_axis(name => $self->chart_x_label, max => $maxset); | |||||||||||
| 265 | 0 | ▶ | $chart->set_legend(none => 1) unless $self->chart_legend_display; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 266 | |||||||||||||
| 267 | #Write Excel output to filehandle | ||||||||||||
| 268 | 0 | $workbook->close; | |||||||||||
| 269 | |||||||||||||
| 270 | 0 | return $content; | |||||||||||
| 271 | |||||||||||||
| 272 | } | ||||||||||||
| 273 | |||||||||||||
| 274 | =head2 generate_file | ||||||||||||
| 275 | |||||||||||||
| 276 | Returns Excel file name in temp folder | ||||||||||||
| 277 | |||||||||||||
| 278 | use File::Copy qw{move}; | ||||||||||||
| 279 | my $filename = $writer->generate_file(\@data); | ||||||||||||
| 280 | move $filename, '.'; | ||||||||||||
| 281 | |||||||||||||
| 282 | =cut | ||||||||||||
| 283 | |||||||||||||
| 284 | sub generate_file { | ||||||||||||
| 285 | 0 | ▶ | require DateTime; | ||||||||||
|
Subroutine
generate_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 286 | 0 | require File::Temp; | |||||||||||
| 287 | |||||||||||||
| 288 | 0 | my $self = shift; | |||||||||||
| 289 | 0 | my $ymd = DateTime->now->ymd; | |||||||||||
| 290 | 0 | my ($fh, $filename) = File::Temp::tempfile("excel-cdf-$ymd-XXXXXX", SUFFIX => ".xlsx", DIR => File::Temp::tempdir()); | |||||||||||
| 291 | 0 | binmode($fh); | |||||||||||
| 292 | 0 | my $blob = $self->generate(@_); | |||||||||||
| 293 | 0 | print $fh $blob; | |||||||||||
| 294 | 0 | close($fh); | |||||||||||
| 295 | 0 | return $filename; | |||||||||||
| 296 | } | ||||||||||||
| 297 | |||||||||||||
| 298 | =head1 SEE ALSO | ||||||||||||
| 299 | |||||||||||||
| 300 | L<Excel::Writer::XLSX> | ||||||||||||
| 301 | |||||||||||||
| 302 | =head1 AUTHOR | ||||||||||||
| 303 | |||||||||||||
| 304 | Michael R. Davis | ||||||||||||
| 305 | |||||||||||||
| 306 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 307 | |||||||||||||
| 308 | MIT License | ||||||||||||
| 309 | |||||||||||||
| 310 | Copyright (c) 2022 Michael R. Davis | ||||||||||||
| 311 | |||||||||||||
| 312 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||||||||
| 313 | of this software and associated documentation files (the "Software"), to deal | ||||||||||||
| 314 | in the Software without restriction, including without limitation the rights | ||||||||||||
| 315 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||||||
| 316 | copies of the Software, and to permit persons to whom the Software is | ||||||||||||
| 317 | furnished to do so, subject to the following conditions: | ||||||||||||
| 318 | |||||||||||||
| 319 | The above copyright notice and this permission notice shall be included in all | ||||||||||||
| 320 | copies or substantial portions of the Software. | ||||||||||||
| 321 | |||||||||||||
| 322 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||||
| 323 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||||||||
| 324 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||||||||
| 325 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||||||
| 326 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||||||||
| 327 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||||||||
| 328 | SOFTWARE. | ||||||||||||
| 329 | |||||||||||||
| 330 | =cut | ||||||||||||
| 331 | |||||||||||||
| 332 | 1 | 1; | |||||||||||