line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
############################################################################### |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Column - A class for writing Excel Column charts. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Used in conjunction with Excel::Writer::XLSX::Chart. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# See formatting note in Excel::Writer::XLSX::Chart. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# Copyright 2000-2021, John McNamara, jmcnamara@cpan.org |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# Documentation after __END__ |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# perltidy with the following options: -mbl=2 -pt=0 -nola |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use 5.008002; |
18
|
159
|
|
|
159
|
|
5268
|
use strict; |
|
159
|
|
|
|
|
485
|
|
19
|
159
|
|
|
159
|
|
666
|
use warnings; |
|
159
|
|
|
|
|
240
|
|
|
159
|
|
|
|
|
2702
|
|
20
|
159
|
|
|
159
|
|
578
|
use Carp; |
|
159
|
|
|
|
|
249
|
|
|
159
|
|
|
|
|
3266
|
|
21
|
159
|
|
|
159
|
|
594
|
use Excel::Writer::XLSX::Chart; |
|
159
|
|
|
|
|
250
|
|
|
159
|
|
|
|
|
7920
|
|
22
|
159
|
|
|
159
|
|
765
|
|
|
159
|
|
|
|
|
253
|
|
|
159
|
|
|
|
|
68272
|
|
23
|
|
|
|
|
|
|
our @ISA = qw(Excel::Writer::XLSX::Chart); |
24
|
|
|
|
|
|
|
our $VERSION = '1.09'; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
############################################################################### |
28
|
|
|
|
|
|
|
# |
29
|
|
|
|
|
|
|
# new() |
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
# |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $class = shift; |
34
|
|
|
|
|
|
|
my $self = Excel::Writer::XLSX::Chart->new( @_ ); |
35
|
161
|
|
|
161
|
0
|
986
|
|
36
|
161
|
|
|
|
|
601
|
$self->{_subtype} = $self->{_subtype} || 'clustered'; |
37
|
|
|
|
|
|
|
$self->{_horiz_val_axis} = 0; |
38
|
161
|
|
100
|
|
|
770
|
|
39
|
161
|
|
|
|
|
344
|
# Override and reset the default axis values. |
40
|
|
|
|
|
|
|
if ( $self->{_subtype} eq 'percent_stacked' ) { |
41
|
|
|
|
|
|
|
$self->{_y_axis}->{_defaults}->{num_format} = '0%'; |
42
|
161
|
100
|
|
|
|
488
|
} |
43
|
1
|
|
|
|
|
2
|
|
44
|
|
|
|
|
|
|
$self->set_y_axis(); |
45
|
|
|
|
|
|
|
|
46
|
161
|
|
|
|
|
571
|
# Set the available data label positions for this chart type. |
47
|
|
|
|
|
|
|
$self->{_label_position_default} = 'outside_end'; |
48
|
|
|
|
|
|
|
$self->{_label_positions} = { |
49
|
161
|
|
|
|
|
285
|
center => 'ctr', |
50
|
|
|
|
|
|
|
inside_base => 'inBase', |
51
|
161
|
|
|
|
|
662
|
inside_end => 'inEnd', |
52
|
|
|
|
|
|
|
outside_end => 'outEnd', |
53
|
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
bless $self, $class; |
56
|
|
|
|
|
|
|
|
57
|
161
|
|
|
|
|
336
|
return $self; |
58
|
|
|
|
|
|
|
} |
59
|
161
|
|
|
|
|
503
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
############################################################################## |
62
|
|
|
|
|
|
|
# |
63
|
|
|
|
|
|
|
# _write_chart_type() |
64
|
|
|
|
|
|
|
# |
65
|
|
|
|
|
|
|
# Override the virtual superclass method with a chart specific method. |
66
|
|
|
|
|
|
|
# |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $self = shift; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Write the c:barChart element. |
71
|
318
|
|
|
318
|
|
519
|
$self->_write_bar_chart( @_ ); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
318
|
|
|
|
|
840
|
|
75
|
|
|
|
|
|
|
############################################################################## |
76
|
|
|
|
|
|
|
# |
77
|
|
|
|
|
|
|
# _write_bar_chart() |
78
|
|
|
|
|
|
|
# |
79
|
|
|
|
|
|
|
# Write the <c:barChart> element. |
80
|
|
|
|
|
|
|
# |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $self = shift; |
83
|
|
|
|
|
|
|
my %args = @_; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my @series; |
86
|
318
|
|
|
318
|
|
479
|
if ( $args{primary_axes} ) { |
87
|
318
|
|
|
|
|
844
|
@series = $self->_get_primary_axes_series; |
88
|
|
|
|
|
|
|
} |
89
|
318
|
|
|
|
|
555
|
else { |
90
|
318
|
100
|
|
|
|
856
|
@series = $self->_get_secondary_axes_series; |
91
|
159
|
|
|
|
|
1183
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
return unless scalar @series; |
94
|
159
|
|
|
|
|
1001
|
|
95
|
|
|
|
|
|
|
my $subtype = $self->{_subtype}; |
96
|
|
|
|
|
|
|
$subtype = 'percentStacked' if $subtype eq 'percent_stacked'; |
97
|
318
|
100
|
|
|
|
1011
|
|
98
|
|
|
|
|
|
|
# Set a default overlap for stacked charts. |
99
|
161
|
|
|
|
|
403
|
if ($self->{_subtype} =~ /stacked/) { |
100
|
161
|
100
|
|
|
|
565
|
if (!defined $self->{_series_overlap_1}) { |
101
|
|
|
|
|
|
|
$self->{_series_overlap_1} = 100; |
102
|
|
|
|
|
|
|
} |
103
|
161
|
100
|
|
|
|
718
|
} |
104
|
3
|
50
|
|
|
|
12
|
|
105
|
3
|
|
|
|
|
9
|
$self->xml_start_tag( 'c:barChart' ); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Write the c:barDir element. |
108
|
|
|
|
|
|
|
$self->_write_bar_dir(); |
109
|
161
|
|
|
|
|
597
|
|
110
|
|
|
|
|
|
|
# Write the c:grouping element. |
111
|
|
|
|
|
|
|
$self->_write_grouping( $subtype ); |
112
|
161
|
|
|
|
|
626
|
|
113
|
|
|
|
|
|
|
# Write the c:ser elements. |
114
|
|
|
|
|
|
|
$self->_write_ser( $_ ) for @series; |
115
|
161
|
|
|
|
|
1039
|
|
116
|
|
|
|
|
|
|
if ( $args{primary_axes} ) { |
117
|
|
|
|
|
|
|
# Write the c:gapWidth element. |
118
|
161
|
|
|
|
|
1045
|
$self->_write_gap_width( $self->{_series_gap_1} ); |
119
|
|
|
|
|
|
|
|
120
|
161
|
100
|
|
|
|
1102
|
# Write the c:overlap element. |
121
|
|
|
|
|
|
|
$self->_write_overlap( $self->{_series_overlap_1} ); |
122
|
159
|
|
|
|
|
1550
|
} |
123
|
|
|
|
|
|
|
else { |
124
|
|
|
|
|
|
|
# Write the c:gapWidth element. |
125
|
159
|
|
|
|
|
1086
|
$self->_write_gap_width( $self->{_series_gap_2} ); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Write the c:overlap element. |
128
|
|
|
|
|
|
|
$self->_write_overlap( $self->{_series_overlap_2} ); |
129
|
2
|
|
|
|
|
8
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Write the c:axId elements |
132
|
2
|
|
|
|
|
15
|
$self->_write_axis_ids( %args ); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
$self->xml_end_tag( 'c:barChart' ); |
135
|
|
|
|
|
|
|
} |
136
|
161
|
|
|
|
|
1354
|
|
137
|
|
|
|
|
|
|
|
138
|
161
|
|
|
|
|
594
|
############################################################################## |
139
|
|
|
|
|
|
|
# |
140
|
|
|
|
|
|
|
# _write_bar_dir() |
141
|
|
|
|
|
|
|
# |
142
|
|
|
|
|
|
|
# Write the <c:barDir> element. |
143
|
|
|
|
|
|
|
# |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $self = shift; |
146
|
|
|
|
|
|
|
my $val = 'col'; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my @attributes = ( 'val' => $val ); |
149
|
|
|
|
|
|
|
|
150
|
161
|
|
|
161
|
|
332
|
$self->xml_empty_tag( 'c:barDir', @attributes ); |
151
|
161
|
|
|
|
|
304
|
} |
152
|
|
|
|
|
|
|
|
153
|
161
|
|
|
|
|
410
|
|
154
|
|
|
|
|
|
|
############################################################################## |
155
|
161
|
|
|
|
|
513
|
# |
156
|
|
|
|
|
|
|
# _write_err_dir() |
157
|
|
|
|
|
|
|
# |
158
|
|
|
|
|
|
|
# Write the <c:errDir> element. Overridden from Chart class since it is not |
159
|
|
|
|
|
|
|
# used in Bar charts. |
160
|
|
|
|
|
|
|
# |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
1
|
|
|
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 NAME |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Column - A class for writing Excel Column charts. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 SYNOPSIS |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
To create a simple Excel file with a Column chart using Excel::Writer::XLSX: |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
#!/usr/bin/perl |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
use strict; |
179
|
|
|
|
|
|
|
use warnings; |
180
|
|
|
|
|
|
|
use Excel::Writer::XLSX; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
my $workbook = Excel::Writer::XLSX->new( 'chart.xlsx' ); |
183
|
|
|
|
|
|
|
my $worksheet = $workbook->add_worksheet(); |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
my $chart = $workbook->add_chart( type => 'column' ); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# Configure the chart. |
188
|
|
|
|
|
|
|
$chart->add_series( |
189
|
|
|
|
|
|
|
categories => '=Sheet1!$A$2:$A$7', |
190
|
|
|
|
|
|
|
values => '=Sheet1!$B$2:$B$7', |
191
|
|
|
|
|
|
|
); |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
# Add the worksheet data the chart refers to. |
194
|
|
|
|
|
|
|
my $data = [ |
195
|
|
|
|
|
|
|
[ 'Category', 2, 3, 4, 5, 6, 7 ], |
196
|
|
|
|
|
|
|
[ 'Value', 1, 4, 5, 2, 1, 5 ], |
197
|
|
|
|
|
|
|
]; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
$worksheet->write( 'A1', $data ); |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
__END__ |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 DESCRIPTION |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
This module implements Column charts for L<Excel::Writer::XLSX>. The chart object is created via the Workbook C<add_chart()> method: |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
my $chart = $workbook->add_chart( type => 'column' ); |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Once the object is created it can be configured via the following methods that are common to all chart classes: |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
$chart->add_series(); |
212
|
|
|
|
|
|
|
$chart->set_x_axis(); |
213
|
|
|
|
|
|
|
$chart->set_y_axis(); |
214
|
|
|
|
|
|
|
$chart->set_title(); |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
These methods are explained in detail in L<Excel::Writer::XLSX::Chart>. Class specific methods or settings, if any, are explained below. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 Column Chart Subtypes |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
The C<Column> chart module also supports the following sub-types: |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
stacked |
223
|
|
|
|
|
|
|
percent_stacked |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
These can be specified at creation time via the C<add_chart()> Worksheet method: |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
my $chart = $workbook->add_chart( type => 'column', subtype => 'stacked' ); |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head1 EXAMPLE |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Here is a complete example that demonstrates most of the available features when creating a chart. |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
#!/usr/bin/perl |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
use strict; |
236
|
|
|
|
|
|
|
use warnings; |
237
|
|
|
|
|
|
|
use Excel::Writer::XLSX; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
my $workbook = Excel::Writer::XLSX->new( 'chart_column.xlsx' ); |
240
|
|
|
|
|
|
|
my $worksheet = $workbook->add_worksheet(); |
241
|
|
|
|
|
|
|
my $bold = $workbook->add_format( bold => 1 ); |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
# Add the worksheet data that the charts will refer to. |
244
|
|
|
|
|
|
|
my $headings = [ 'Number', 'Batch 1', 'Batch 2' ]; |
245
|
|
|
|
|
|
|
my $data = [ |
246
|
|
|
|
|
|
|
[ 2, 3, 4, 5, 6, 7 ], |
247
|
|
|
|
|
|
|
[ 10, 40, 50, 20, 10, 50 ], |
248
|
|
|
|
|
|
|
[ 30, 60, 70, 50, 40, 30 ], |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
]; |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
$worksheet->write( 'A1', $headings, $bold ); |
253
|
|
|
|
|
|
|
$worksheet->write( 'A2', $data ); |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
# Create a new chart object. In this case an embedded chart. |
256
|
|
|
|
|
|
|
my $chart = $workbook->add_chart( type => 'column', embedded => 1 ); |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
# Configure the first series. |
259
|
|
|
|
|
|
|
$chart->add_series( |
260
|
|
|
|
|
|
|
name => '=Sheet1!$B$1', |
261
|
|
|
|
|
|
|
categories => '=Sheet1!$A$2:$A$7', |
262
|
|
|
|
|
|
|
values => '=Sheet1!$B$2:$B$7', |
263
|
|
|
|
|
|
|
); |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
# Configure second series. Note alternative use of array ref to define |
266
|
|
|
|
|
|
|
# ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. |
267
|
|
|
|
|
|
|
$chart->add_series( |
268
|
|
|
|
|
|
|
name => '=Sheet1!$C$1', |
269
|
|
|
|
|
|
|
categories => [ 'Sheet1', 1, 6, 0, 0 ], |
270
|
|
|
|
|
|
|
values => [ 'Sheet1', 1, 6, 2, 2 ], |
271
|
|
|
|
|
|
|
); |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# Add a chart title and some axis labels. |
274
|
|
|
|
|
|
|
$chart->set_title ( name => 'Results of sample analysis' ); |
275
|
|
|
|
|
|
|
$chart->set_x_axis( name => 'Test number' ); |
276
|
|
|
|
|
|
|
$chart->set_y_axis( name => 'Sample length (mm)' ); |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
# Set an Excel chart style. Blue colors with white outline and shadow. |
279
|
|
|
|
|
|
|
$chart->set_style( 11 ); |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
# Insert the chart into the worksheet (with an offset). |
282
|
|
|
|
|
|
|
$worksheet->insert_chart( 'D2', $chart, 25, 10 ); |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
__END__ |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=begin html |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
<p>This will produce a chart that looks like this:</p> |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
<p><center><img src="http://jmcnamara.github.io/excel-writer-xlsx/images/examples/column1.jpg" width="483" height="291" alt="Chart example." /></center></p> |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=end html |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=head1 AUTHOR |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
John McNamara jmcnamara@cpan.org |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=head1 COPYRIGHT |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
Copyright MM-MMXXI, John McNamara. |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. |
305
|
|
|
|
|
|
|
|