rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame^] | 1 | package Spreadsheet::WriteExcel::Chart::Pie; |
| 2 | |
| 3 | ############################################################################### |
| 4 | # |
| 5 | # Pie - A writer class for Excel Pie 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 | $self->{_vary_data_color} = 1; |
| 38 | |
| 39 | bless $self, $class; |
| 40 | return $self; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | ############################################################################### |
| 45 | # |
| 46 | # _store_chart_type() |
| 47 | # |
| 48 | # Implementation of the abstract method from the specific chart class. |
| 49 | # |
| 50 | # Write the Pie chart BIFF record. |
| 51 | # |
| 52 | sub _store_chart_type { |
| 53 | |
| 54 | my $self = shift; |
| 55 | |
| 56 | my $record = 0x1019; # Record identifier. |
| 57 | my $length = 0x0006; # Number of bytes to follow. |
| 58 | my $angle = 0x0000; # Angle. |
| 59 | my $donut = 0x0000; # Donut hole size. |
| 60 | my $grbit = 0x0002; # Option flags. |
| 61 | |
| 62 | my $header = pack 'vv', $record, $length; |
| 63 | my $data = ''; |
| 64 | $data .= pack 'v', $angle; |
| 65 | $data .= pack 'v', $donut; |
| 66 | $data .= pack 'v', $grbit; |
| 67 | |
| 68 | $self->_append( $header, $data ); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | ############################################################################### |
| 73 | # |
| 74 | # _store_axisparent_stream(). Overridden. |
| 75 | # |
| 76 | # Write the AXISPARENT chart substream. |
| 77 | # |
| 78 | # A Pie chart has no X or Y axis so we override this method to remove them. |
| 79 | # |
| 80 | sub _store_axisparent_stream { |
| 81 | |
| 82 | my $self = shift; |
| 83 | |
| 84 | $self->_store_axisparent( @{ $self->{_config}->{_axisparent} } ); |
| 85 | |
| 86 | $self->_store_begin(); |
| 87 | $self->_store_pos( @{ $self->{_config}->{_axisparent_pos} } ); |
| 88 | |
| 89 | $self->_store_chartformat_stream(); |
| 90 | $self->_store_end(); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | 1; |
| 95 | |
| 96 | |
| 97 | __END__ |
| 98 | |
| 99 | |
| 100 | =head1 NAME |
| 101 | |
| 102 | Pie - A writer class for Excel Pie charts. |
| 103 | |
| 104 | =head1 SYNOPSIS |
| 105 | |
| 106 | To create a simple Excel file with a Pie chart using Spreadsheet::WriteExcel: |
| 107 | |
| 108 | #!/usr/bin/perl -w |
| 109 | |
| 110 | use strict; |
| 111 | use Spreadsheet::WriteExcel; |
| 112 | |
| 113 | my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' ); |
| 114 | my $worksheet = $workbook->add_worksheet(); |
| 115 | |
| 116 | my $chart = $workbook->add_chart( type => 'pie' ); |
| 117 | |
| 118 | # Configure the chart. |
| 119 | $chart->add_series( |
| 120 | categories => '=Sheet1!$A$2:$A$7', |
| 121 | values => '=Sheet1!$B$2:$B$7', |
| 122 | ); |
| 123 | |
| 124 | # Add the worksheet data the chart refers to. |
| 125 | my $data = [ |
| 126 | [ 'Category', 2, 3, 4, 5, 6, 7 ], |
| 127 | [ 'Value', 1, 4, 5, 2, 1, 5 ], |
| 128 | ]; |
| 129 | |
| 130 | $worksheet->write( 'A1', $data ); |
| 131 | |
| 132 | __END__ |
| 133 | |
| 134 | =head1 DESCRIPTION |
| 135 | |
| 136 | This module implements Pie charts for L<Spreadsheet::WriteExcel>. The chart object is created via the Workbook C<add_chart()> method: |
| 137 | |
| 138 | my $chart = $workbook->add_chart( type => 'pie' ); |
| 139 | |
| 140 | Once the object is created it can be configured via the following methods that are common to all chart classes: |
| 141 | |
| 142 | $chart->add_series(); |
| 143 | $chart->set_title(); |
| 144 | |
| 145 | These methods are explained in detail in L<Spreadsheet::WriteExcel::Chart>. Class specific methods or settings, if any, are explained below. |
| 146 | |
| 147 | =head1 Pie Chart Methods |
| 148 | |
| 149 | There aren't currently any pie chart specific methods. See the TODO section of L<Spreadsheet::WriteExcel::Chart>. |
| 150 | |
| 151 | A Pie chart doesn't have an X or Y axis so the following common chart methods are ignored. |
| 152 | |
| 153 | $chart->set_x_axis(); |
| 154 | $chart->set_y_axis(); |
| 155 | |
| 156 | =head1 EXAMPLE |
| 157 | |
| 158 | Here is a complete example that demonstrates most of the available features when creating a chart. |
| 159 | |
| 160 | #!/usr/bin/perl -w |
| 161 | |
| 162 | use strict; |
| 163 | use Spreadsheet::WriteExcel; |
| 164 | |
| 165 | my $workbook = Spreadsheet::WriteExcel->new( 'chart_pie.xls' ); |
| 166 | my $worksheet = $workbook->add_worksheet(); |
| 167 | my $bold = $workbook->add_format( bold => 1 ); |
| 168 | |
| 169 | # Add the worksheet data that the charts will refer to. |
| 170 | my $headings = [ 'Category', 'Values' ]; |
| 171 | my $data = [ |
| 172 | [ 'Apple', 'Cherry', 'Pecan' ], |
| 173 | [ 60, 30, 10 ], |
| 174 | ]; |
| 175 | |
| 176 | $worksheet->write( 'A1', $headings, $bold ); |
| 177 | $worksheet->write( 'A2', $data ); |
| 178 | |
| 179 | # Create a new chart object. In this case an embedded chart. |
| 180 | my $chart = $workbook->add_chart( type => 'pie', embedded => 1 ); |
| 181 | |
| 182 | # Configure the series. |
| 183 | $chart->add_series( |
| 184 | name => 'Pie sales data', |
| 185 | categories => '=Sheet1!$A$2:$A$4', |
| 186 | values => '=Sheet1!$B$2:$B$4', |
| 187 | ); |
| 188 | |
| 189 | # Add a title. |
| 190 | $chart->set_title( name => 'Popular Pie Types' ); |
| 191 | |
| 192 | |
| 193 | # Insert the chart into the worksheet (with an offset). |
| 194 | $worksheet->insert_chart( 'C2', $chart, 25, 10 ); |
| 195 | |
| 196 | __END__ |
| 197 | |
| 198 | |
| 199 | =begin html |
| 200 | |
| 201 | <p>This will produce a chart that looks like this:</p> |
| 202 | |
| 203 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/pie1.jpg" width="527" height="320" alt="Chart example." /></center></p> |
| 204 | |
| 205 | =end html |
| 206 | |
| 207 | |
| 208 | =head1 AUTHOR |
| 209 | |
| 210 | John McNamara jmcnamara@cpan.org |
| 211 | |
| 212 | =head1 COPYRIGHT |
| 213 | |
| 214 | Copyright MM-MMX, John McNamara. |
| 215 | |
| 216 | All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. |
| 217 | |