blob: 6a529f0e1b1728051c38b1ca7796705701a6fe48 [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001package Spreadsheet::WriteExcel::Chart::Stock;
2
3###############################################################################
4#
5# Stock - A writer class for Excel Stock charts.
6#
7# Used in conjunction with Spreadsheet::WriteExcel::Chart.
8#
9# See formatting note in Spreadsheet::WriteExcel::Chart.
10#
11# Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
12#
13# Documentation after __END__
14#
15
16require Exporter;
17
18use strict;
19use Spreadsheet::WriteExcel::Chart;
20
21
22use vars qw($VERSION @ISA);
23@ISA = qw(Spreadsheet::WriteExcel::Chart Exporter);
24
25$VERSION = '2.37';
26
27###############################################################################
28#
29# new()
30#
31#
32sub new {
33
34 my $class = shift;
35 my $self = Spreadsheet::WriteExcel::Chart->new( @_ );
36
37 bless $self, $class;
38 return $self;
39}
40
41
42###############################################################################
43#
44# _store_chart_type()
45#
46# Implementation of the abstract method from the specific chart class.
47#
48# Write the LINE chart BIFF record. A stock chart uses the same LINE record
49# as a line chart but with additional DROPBAR and CHARTLINE records to define
50# the stock style.
51#
52sub _store_chart_type {
53
54 my $self = shift;
55
56 my $record = 0x1018; # Record identifier.
57 my $length = 0x0002; # Number of bytes to follow.
58 my $grbit = 0x0000; # Option flags.
59
60 my $header = pack 'vv', $record, $length;
61 my $data = pack 'v', $grbit;
62
63 $self->_append( $header, $data );
64}
65
66###############################################################################
67#
68# _store_marker_dataformat_stream(). Overridden.
69#
70# This is an implementation of the parent abstract method to define
71# properties of markers, linetypes, pie formats and other.
72#
73sub _store_marker_dataformat_stream {
74
75 my $self = shift;
76
77 $self->_store_dropbar();
78 $self->_store_begin();
79 $self->_store_lineformat( 0x00000000, 0x0000, 0xFFFF, 0x0001, 0x004F );
80 $self->_store_areaformat( 0x00FFFFFF, 0x0000, 0x01, 0x01, 0x09, 0x08 );
81 $self->_store_end();
82
83 $self->_store_dropbar();
84 $self->_store_begin();
85 $self->_store_lineformat( 0x00000000, 0x0000, 0xFFFF, 0x0001, 0x004F );
86 $self->_store_areaformat( 0x0000, 0x00FFFFFF, 0x01, 0x01, 0x08, 0x09 );
87 $self->_store_end();
88
89 $self->_store_chartline();
90 $self->_store_lineformat( 0x00000000, 0x0000, 0xFFFF, 0x0000, 0x004F );
91
92
93 $self->_store_dataformat( 0x0000, 0xFFFD, 0x0000 );
94 $self->_store_begin();
95 $self->_store_3dbarshape();
96 $self->_store_lineformat( 0x00000000, 0x0005, 0xFFFF, 0x0000, 0x004F );
97 $self->_store_areaformat( 0x00000000, 0x0000, 0x00, 0x01, 0x4D, 0x4D );
98 $self->_store_pieformat();
99 $self->_store_markerformat( 0x00, 0x00, 0x00, 0x00, 0x4D, 0x4D, 0x3C );
100 $self->_store_end();
101
102}
103
104
1051;
106
107
108__END__
109
110
111=head1 NAME
112
113Stock - A writer class for Excel Stock charts.
114
115=head1 SYNOPSIS
116
117To create a simple Excel file with a Stock chart using Spreadsheet::WriteExcel:
118
119 #!/usr/bin/perl -w
120
121 use strict;
122 use Spreadsheet::WriteExcel;
123
124 my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' );
125 my $worksheet = $workbook->add_worksheet();
126
127 my $chart = $workbook->add_chart( type => 'stock' );
128
129 # Add a series for each Open-High-Low-Close.
130 $chart->add_series( categories => '=Sheet1!$A$2:$A$6', values => '=Sheet1!$B$2:$B$6' );
131 $chart->add_series( categories => '=Sheet1!$A$2:$A$6', values => '=Sheet1!$C$2:$C$6' );
132 $chart->add_series( categories => '=Sheet1!$A$2:$A$6', values => '=Sheet1!$D$2:$D$6' );
133 $chart->add_series( categories => '=Sheet1!$A$2:$A$6', values => '=Sheet1!$E$2:$E$6' );
134
135 # Add the worksheet data the chart refers to.
136 # ... See the full example below.
137
138 __END__
139
140
141=head1 DESCRIPTION
142
143This module implements Stock charts for L<Spreadsheet::WriteExcel>. The chart object is created via the Workbook C<add_chart()> method:
144
145 my $chart = $workbook->add_chart( type => 'stock' );
146
147Once the object is created it can be configured via the following methods that are common to all chart classes:
148
149 $chart->add_series();
150 $chart->set_x_axis();
151 $chart->set_y_axis();
152 $chart->set_title();
153
154These methods are explained in detail in L<Spreadsheet::WriteExcel::Chart>. Class specific methods or settings, if any, are explained below.
155
156=head1 Stock Chart Methods
157
158There aren't currently any stock chart specific methods. See the TODO section of L<Spreadsheet::WriteExcel::Chart>.
159
160The default Stock chart is an Open-High-Low-Close chart. A series must be added for each of these data sources.
161
162The default Stock chart is in black and white. User defined colours will be added at a later stage.
163
164=head1 EXAMPLE
165
166Here is a complete example that demonstrates most of the available features when creating a Stock chart.
167
168 #!/usr/bin/perl -w
169
170 use strict;
171 use Spreadsheet::WriteExcel;
172
173 my $workbook = Spreadsheet::WriteExcel->new( 'chart_stock_ex.xls' );
174 my $worksheet = $workbook->add_worksheet();
175 my $bold = $workbook->add_format( bold => 1 );
176 my $date_format = $workbook->add_format( num_format => 'dd/mm/yyyy' );
177
178 # Add the worksheet data that the charts will refer to.
179 my $headings = [ 'Date', 'Open', 'High', 'Low', 'Close' ];
180 my @data = (
181 [ '2009-08-23', 110.75, 113.48, 109.05, 109.40 ],
182 [ '2009-08-24', 111.24, 111.60, 103.57, 104.87 ],
183 [ '2009-08-25', 104.96, 108.00, 103.88, 106.00 ],
184 [ '2009-08-26', 104.95, 107.95, 104.66, 107.91 ],
185 [ '2009-08-27', 108.10, 108.62, 105.69, 106.15 ],
186 );
187
188 $worksheet->write( 'A1', $headings, $bold );
189
190 my $row = 1;
191 for my $data ( @data ) {
192 $worksheet->write( $row, 0, $data->[0], $date_format );
193 $worksheet->write( $row, 1, $data->[1] );
194 $worksheet->write( $row, 2, $data->[2] );
195 $worksheet->write( $row, 3, $data->[3] );
196 $worksheet->write( $row, 4, $data->[4] );
197 $row++;
198 }
199
200 # Create a new chart object. In this case an embedded chart.
201 my $chart = $workbook->add_chart( type => 'stock', embedded => 1 );
202
203 # Add a series for each of the Open-High-Low-Close columns.
204 $chart->add_series(
205 categories => '=Sheet1!$A$2:$A$6',
206 values => '=Sheet1!$B$2:$B$6',
207 name => 'Open',
208 );
209
210 $chart->add_series(
211 categories => '=Sheet1!$A$2:$A$6',
212 values => '=Sheet1!$C$2:$C$6',
213 name => 'High',
214 );
215
216 $chart->add_series(
217 categories => '=Sheet1!$A$2:$A$6',
218 values => '=Sheet1!$D$2:$D$6',
219 name => 'Low',
220 );
221
222 $chart->add_series(
223 categories => '=Sheet1!$A$2:$A$6',
224 values => '=Sheet1!$E$2:$E$6',
225 name => 'Close',
226 );
227
228 # Add a chart title and some axis labels.
229 $chart->set_title( name => 'Open-High-Low-Close', );
230 $chart->set_x_axis( name => 'Date', );
231 $chart->set_y_axis( name => 'Share price', );
232
233 # Insert the chart into the worksheet (with an offset).
234 $worksheet->insert_chart( 'F2', $chart, 25, 10 );
235
236 __END__
237
238
239=begin html
240
241<p>This will produce a chart that looks like this:</p>
242
243<p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/stock1.jpg" width="527" height="320" alt="Chart example." /></center></p>
244
245=end html
246
247
248=head1 AUTHOR
249
250John McNamara jmcnamara@cpan.org
251
252=head1 COPYRIGHT
253
254Copyright MM-MMX, John McNamara.
255
256All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
257