Pie.pm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # DISCLAIMER OF WARRANTY
  2. # Because this software is licensed free of charge, there is no warranty for the software,
  3. # to the extent permitted by applicable law. Except when otherwise stated in writing
  4. # the copyright holders and/or other parties provide the software "as is" without
  5. # warranty of any kind, either expressed or implied, including, but not limited to,
  6. # the implied warranties of merchantability and fitness for a particular purpose.
  7. # The entire risk as to the quality and performance of the software is with you.
  8. # Should the software prove defective, you assume the cost of all necessary
  9. # servicing, repair, or correction.
  10. # In no event unless required by applicable law or agreed to in writing will any
  11. # copyright holder, or any other party who may modify and/or redistribute the software
  12. # as permitted by the above licence, be liable to you for damages, including any general,
  13. # special, incidental, or consequential damages arising out of the use or inability
  14. # to use the software (including but not limited to loss of data or data being rendered
  15. # inaccurate or losses sustained by you or third parties or a failure of the software
  16. # to operate with any other software), even if such holder or other party
  17. # has been advised of the possibility of such damages.
  18. # AUTHOR
  19. # John McNamara jmcnamara@cpan.org
  20. # COPYRIGHT
  21. # Copyright MM-MMX, John McNamara.
  22. # All Rights Reserved. This module is free software. It may be used,
  23. # redistributed and/or modified under the terms of
  24. # the Artistic License(full text of the Artistic License http://dev.perl.org/licenses/artistic.html).
  25. package Spreadsheet::WriteExcel::Chart::Pie;
  26. ###############################################################################
  27. #
  28. # Pie - A writer class for Excel Pie charts.
  29. #
  30. # Used in conjunction with Spreadsheet::WriteExcel::Chart.
  31. #
  32. # See formatting note in Spreadsheet::WriteExcel::Chart.
  33. #
  34. # Copyright 2000-2010, John McNamara, jmcnamara@cpan.org
  35. #
  36. # Documentation after __END__
  37. #
  38. require Exporter;
  39. use strict;
  40. use Spreadsheet::WriteExcel::Chart;
  41. use vars qw($VERSION @ISA);
  42. @ISA = qw(Spreadsheet::WriteExcel::Chart Exporter);
  43. $VERSION = '2.37';
  44. ###############################################################################
  45. #
  46. # new()
  47. #
  48. #
  49. sub new {
  50. my $class = shift;
  51. my $self = Spreadsheet::WriteExcel::Chart->new( @_ );
  52. $self->{_vary_data_color} = 1;
  53. bless $self, $class;
  54. return $self;
  55. }
  56. ###############################################################################
  57. #
  58. # _store_chart_type()
  59. #
  60. # Implementation of the abstract method from the specific chart class.
  61. #
  62. # Write the Pie chart BIFF record.
  63. #
  64. sub _store_chart_type {
  65. my $self = shift;
  66. my $record = 0x1019; # Record identifier.
  67. my $length = 0x0006; # Number of bytes to follow.
  68. my $angle = 0x0000; # Angle.
  69. my $donut = 0x0000; # Donut hole size.
  70. my $grbit = 0x0002; # Option flags.
  71. my $header = pack 'vv', $record, $length;
  72. my $data = '';
  73. $data .= pack 'v', $angle;
  74. $data .= pack 'v', $donut;
  75. $data .= pack 'v', $grbit;
  76. $self->_append( $header, $data );
  77. }
  78. ###############################################################################
  79. #
  80. # _store_axisparent_stream(). Overridden.
  81. #
  82. # Write the AXISPARENT chart substream.
  83. #
  84. # A Pie chart has no X or Y axis so we override this method to remove them.
  85. #
  86. sub _store_axisparent_stream {
  87. my $self = shift;
  88. $self->_store_axisparent( @{ $self->{_config}->{_axisparent} } );
  89. $self->_store_begin();
  90. $self->_store_pos( @{ $self->{_config}->{_axisparent_pos} } );
  91. $self->_store_chartformat_stream();
  92. $self->_store_end();
  93. }
  94. 1;
  95. __END__
  96. =head1 NAME
  97. Pie - A writer class for Excel Pie charts.
  98. =head1 SYNOPSIS
  99. To create a simple Excel file with a Pie chart using Spreadsheet::WriteExcel:
  100. #!/usr/bin/perl -w
  101. use strict;
  102. use Spreadsheet::WriteExcel;
  103. my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' );
  104. my $worksheet = $workbook->add_worksheet();
  105. my $chart = $workbook->add_chart( type => 'pie' );
  106. # Configure the chart.
  107. $chart->add_series(
  108. categories => '=Sheet1!$A$2:$A$7',
  109. values => '=Sheet1!$B$2:$B$7',
  110. );
  111. # Add the worksheet data the chart refers to.
  112. my $data = [
  113. [ 'Category', 2, 3, 4, 5, 6, 7 ],
  114. [ 'Value', 1, 4, 5, 2, 1, 5 ],
  115. ];
  116. $worksheet->write( 'A1', $data );
  117. __END__
  118. =head1 DESCRIPTION
  119. This module implements Pie charts for L<Spreadsheet::WriteExcel>. The chart object is created via the Workbook C<add_chart()> method:
  120. my $chart = $workbook->add_chart( type => 'pie' );
  121. Once the object is created it can be configured via the following methods that are common to all chart classes:
  122. $chart->add_series();
  123. $chart->set_title();
  124. These methods are explained in detail in L<Spreadsheet::WriteExcel::Chart>. Class specific methods or settings, if any, are explained below.
  125. =head1 Pie Chart Methods
  126. There aren't currently any pie chart specific methods. See the TODO section of L<Spreadsheet::WriteExcel::Chart>.
  127. A Pie chart doesn't have an X or Y axis so the following common chart methods are ignored.
  128. $chart->set_x_axis();
  129. $chart->set_y_axis();
  130. =head1 EXAMPLE
  131. Here is a complete example that demonstrates most of the available features when creating a chart.
  132. #!/usr/bin/perl -w
  133. use strict;
  134. use Spreadsheet::WriteExcel;
  135. my $workbook = Spreadsheet::WriteExcel->new( 'chart_pie.xls' );
  136. my $worksheet = $workbook->add_worksheet();
  137. my $bold = $workbook->add_format( bold => 1 );
  138. # Add the worksheet data that the charts will refer to.
  139. my $headings = [ 'Category', 'Values' ];
  140. my $data = [
  141. [ 'Apple', 'Cherry', 'Pecan' ],
  142. [ 60, 30, 10 ],
  143. ];
  144. $worksheet->write( 'A1', $headings, $bold );
  145. $worksheet->write( 'A2', $data );
  146. # Create a new chart object. In this case an embedded chart.
  147. my $chart = $workbook->add_chart( type => 'pie', embedded => 1 );
  148. # Configure the series.
  149. $chart->add_series(
  150. name => 'Pie sales data',
  151. categories => '=Sheet1!$A$2:$A$4',
  152. values => '=Sheet1!$B$2:$B$4',
  153. );
  154. # Add a title.
  155. $chart->set_title( name => 'Popular Pie Types' );
  156. # Insert the chart into the worksheet (with an offset).
  157. $worksheet->insert_chart( 'C2', $chart, 25, 10 );
  158. __END__
  159. =begin html
  160. <p>This will produce a chart that looks like this:</p>
  161. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/pie1.jpg" width="527" height="320" alt="Chart example." /></center></p>
  162. =end html
  163. =head1 AUTHOR
  164. John McNamara jmcnamara@cpan.org
  165. =head1 COPYRIGHT
  166. Copyright MM-MMX, John McNamara.
  167. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.