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