blob: 60320b6b7b412d23e294f4876f36f397ed76ab2c [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001package Spreadsheet::WriteExcel::Chart::Line;
2
3###############################################################################
4#
5# Line - A writer class for Excel Line 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. Defines a line chart type.
49#
50sub _store_chart_type {
51
52 my $self = shift;
53
54 my $record = 0x1018; # Record identifier.
55 my $length = 0x0002; # Number of bytes to follow.
56 my $grbit = 0x0000; # Option flags.
57
58 my $header = pack 'vv', $record, $length;
59 my $data = pack 'v', $grbit;
60
61 $self->_append( $header, $data );
62}
63
64
651;
66
67
68__END__
69
70
71=head1 NAME
72
73Line - A writer class for Excel Line charts.
74
75=head1 SYNOPSIS
76
77To create a simple Excel file with a Line chart using Spreadsheet::WriteExcel:
78
79 #!/usr/bin/perl -w
80
81 use strict;
82 use Spreadsheet::WriteExcel;
83
84 my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' );
85 my $worksheet = $workbook->add_worksheet();
86
87 my $chart = $workbook->add_chart( type => 'line' );
88
89 # Configure the chart.
90 $chart->add_series(
91 categories => '=Sheet1!$A$2:$A$7',
92 values => '=Sheet1!$B$2:$B$7',
93 );
94
95 # Add the worksheet data the chart refers to.
96 my $data = [
97 [ 'Category', 2, 3, 4, 5, 6, 7 ],
98 [ 'Value', 1, 4, 5, 2, 1, 5 ],
99 ];
100
101 $worksheet->write( 'A1', $data );
102
103 __END__
104
105=head1 DESCRIPTION
106
107This module implements Line charts for L<Spreadsheet::WriteExcel>. The chart object is created via the Workbook C<add_chart()> method:
108
109 my $chart = $workbook->add_chart( type => 'line' );
110
111Once the object is created it can be configured via the following methods that are common to all chart classes:
112
113 $chart->add_series();
114 $chart->set_x_axis();
115 $chart->set_y_axis();
116 $chart->set_title();
117
118These methods are explained in detail in L<Spreadsheet::WriteExcel::Chart>. Class specific methods or settings, if any, are explained below.
119
120=head1 Line Chart Methods
121
122There aren't currently any line chart specific methods. See the TODO section of L<Spreadsheet::WriteExcel::Chart>.
123
124=head1 EXAMPLE
125
126Here is a complete example that demonstrates most of the available features when creating a chart.
127
128 #!/usr/bin/perl -w
129
130 use strict;
131 use Spreadsheet::WriteExcel;
132
133 my $workbook = Spreadsheet::WriteExcel->new( 'chart_line.xls' );
134 my $worksheet = $workbook->add_worksheet();
135 my $bold = $workbook->add_format( bold => 1 );
136
137 # Add the worksheet data that the charts will refer to.
138 my $headings = [ 'Number', 'Sample 1', 'Sample 2' ];
139 my $data = [
140 [ 2, 3, 4, 5, 6, 7 ],
141 [ 1, 4, 5, 2, 1, 5 ],
142 [ 3, 6, 7, 5, 4, 3 ],
143 ];
144
145 $worksheet->write( 'A1', $headings, $bold );
146 $worksheet->write( 'A2', $data );
147
148 # Create a new chart object. In this case an embedded chart.
149 my $chart = $workbook->add_chart( type => 'line', embedded => 1 );
150
151 # Configure the first series. (Sample 1)
152 $chart->add_series(
153 name => 'Sample 1',
154 categories => '=Sheet1!$A$2:$A$7',
155 values => '=Sheet1!$B$2:$B$7',
156 );
157
158 # Configure the second series. (Sample 2)
159 $chart->add_series(
160 name => 'Sample 2',
161 categories => '=Sheet1!$A$2:$A$7',
162 values => '=Sheet1!$C$2:$C$7',
163 );
164
165 # Add a chart title and some axis labels.
166 $chart->set_title ( name => 'Results of sample analysis' );
167 $chart->set_x_axis( name => 'Test number' );
168 $chart->set_y_axis( name => 'Sample length (cm)' );
169
170 # Insert the chart into the worksheet (with an offset).
171 $worksheet->insert_chart( 'D2', $chart, 25, 10 );
172
173 __END__
174
175
176=begin html
177
178<p>This will produce a chart that looks like this:</p>
179
180<p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/line1.jpg" width="527" height="320" alt="Chart example." /></center></p>
181
182=end html
183
184
185=head1 AUTHOR
186
187John McNamara jmcnamara@cpan.org
188
189=head1 COPYRIGHT
190
191Copyright MM-MMX, John McNamara.
192
193All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
194