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