Bar.pm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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::Bar;
  26. ###############################################################################
  27. #
  28. # Bar - A writer class for Excel Bar 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. bless $self, $class;
  53. # The axis positions are reversed for a bar chart so we change the config.
  54. my $c = $self->{_config};
  55. $c->{_x_axis_text} = [ 0x2D, 0x6D9, 0x5F, 0x1CC, 0x281, 0x0, 90 ];
  56. $c->{_x_axis_text_pos} = [ 2, 2, 0, 0, 0x17, 0x2A ];
  57. $c->{_y_axis_text} = [ 0x078A, 0x0DFC, 0x011D, 0x9C, 0x0081, 0x0000 ];
  58. $c->{_y_axis_text_pos} = [ 2, 2, 0, 0, 0x45, 0x17 ];
  59. return $self;
  60. }
  61. ###############################################################################
  62. #
  63. # _store_chart_type()
  64. #
  65. # Implementation of the abstract method from the specific chart class.
  66. #
  67. # Write the BAR chart BIFF record. Defines a bar or column chart type.
  68. #
  69. sub _store_chart_type {
  70. my $self = shift;
  71. my $record = 0x1017; # Record identifier.
  72. my $length = 0x0006; # Number of bytes to follow.
  73. my $pcOverlap = 0x0000; # Space between bars.
  74. my $pcGap = 0x0096; # Space between cats.
  75. my $grbit = 0x0001; # Option flags.
  76. my $header = pack 'vv', $record, $length;
  77. my $data = '';
  78. $data .= pack 'v', $pcOverlap;
  79. $data .= pack 'v', $pcGap;
  80. $data .= pack 'v', $grbit;
  81. $self->_append( $header, $data );
  82. }
  83. ###############################################################################
  84. #
  85. # _set_embedded_config_data()
  86. #
  87. # Override some of the default configuration data for an embedded chart.
  88. #
  89. sub _set_embedded_config_data {
  90. my $self = shift;
  91. # Set the parent configuration first.
  92. $self->SUPER::_set_embedded_config_data();
  93. # The axis positions are reversed for a bar chart so we change the config.
  94. my $c = $self->{_config};
  95. $c->{_x_axis_text} = [ 0x57, 0x5BC, 0xB5, 0x214, 0x281, 0x0, 90 ];
  96. $c->{_x_axis_text_pos} = [ 2, 2, 0, 0, 0x17, 0x2A ];
  97. $c->{_y_axis_text} = [ 0x074A, 0x0C8F, 0x021F, 0x123, 0x81, 0x0000 ];
  98. $c->{_y_axis_text_pos} = [ 2, 2, 0, 0, 0x45, 0x17 ];
  99. }
  100. 1;
  101. __END__
  102. =head1 NAME
  103. Bar - A writer class for Excel Bar charts.
  104. =head1 SYNOPSIS
  105. To create a simple Excel file with a Bar chart using Spreadsheet::WriteExcel:
  106. #!/usr/bin/perl -w
  107. use strict;
  108. use Spreadsheet::WriteExcel;
  109. my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' );
  110. my $worksheet = $workbook->add_worksheet();
  111. my $chart = $workbook->add_chart( type => 'bar' );
  112. # Configure the chart.
  113. $chart->add_series(
  114. categories => '=Sheet1!$A$2:$A$7',
  115. values => '=Sheet1!$B$2:$B$7',
  116. );
  117. # Add the worksheet data the chart refers to.
  118. my $data = [
  119. [ 'Category', 2, 3, 4, 5, 6, 7 ],
  120. [ 'Value', 1, 4, 5, 2, 1, 5 ],
  121. ];
  122. $worksheet->write( 'A1', $data );
  123. __END__
  124. =head1 DESCRIPTION
  125. This module implements Bar charts for L<Spreadsheet::WriteExcel>. The chart object is created via the Workbook C<add_chart()> method:
  126. my $chart = $workbook->add_chart( type => 'bar' );
  127. Once the object is created it can be configured via the following methods that are common to all chart classes:
  128. $chart->add_series();
  129. $chart->set_x_axis();
  130. $chart->set_y_axis();
  131. $chart->set_title();
  132. These methods are explained in detail in L<Spreadsheet::WriteExcel::Chart>. Class specific methods or settings, if any, are explained below.
  133. =head1 Bar Chart Methods
  134. There aren't currently any bar chart specific methods. See the TODO section of L<Spreadsheet::WriteExcel::Chart>.
  135. =head1 EXAMPLE
  136. Here is a complete example that demonstrates most of the available features when creating a chart.
  137. #!/usr/bin/perl -w
  138. use strict;
  139. use Spreadsheet::WriteExcel;
  140. my $workbook = Spreadsheet::WriteExcel->new( 'chart_bar.xls' );
  141. my $worksheet = $workbook->add_worksheet();
  142. my $bold = $workbook->add_format( bold => 1 );
  143. # Add the worksheet data that the charts will refer to.
  144. my $headings = [ 'Number', 'Sample 1', 'Sample 2' ];
  145. my $data = [
  146. [ 2, 3, 4, 5, 6, 7 ],
  147. [ 1, 4, 5, 2, 1, 5 ],
  148. [ 3, 6, 7, 5, 4, 3 ],
  149. ];
  150. $worksheet->write( 'A1', $headings, $bold );
  151. $worksheet->write( 'A2', $data );
  152. # Create a new chart object. In this case an embedded chart.
  153. my $chart = $workbook->add_chart( type => 'bar', embedded => 1 );
  154. # Configure the first series. (Sample 1)
  155. $chart->add_series(
  156. name => 'Sample 1',
  157. categories => '=Sheet1!$A$2:$A$7',
  158. values => '=Sheet1!$B$2:$B$7',
  159. );
  160. # Configure the second series. (Sample 2)
  161. $chart->add_series(
  162. name => 'Sample 2',
  163. categories => '=Sheet1!$A$2:$A$7',
  164. values => '=Sheet1!$C$2:$C$7',
  165. );
  166. # Add a chart title and some axis labels.
  167. $chart->set_title ( name => 'Results of sample analysis' );
  168. $chart->set_x_axis( name => 'Test number' );
  169. $chart->set_y_axis( name => 'Sample length (cm)' );
  170. # Insert the chart into the worksheet (with an offset).
  171. $worksheet->insert_chart( 'D2', $chart, 25, 10 );
  172. __END__
  173. =begin html
  174. <p>This will produce a chart that looks like this:</p>
  175. <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/bar1.jpg" width="527" height="320" alt="Chart example." /></center></p>
  176. =end html
  177. =head1 AUTHOR
  178. John McNamara jmcnamara@cpan.org
  179. =head1 COPYRIGHT
  180. Copyright MM-MMX, John McNamara.
  181. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.