rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame^] | 1 | package Spreadsheet::WriteExcel::Chart::Scatter; |
| 2 | |
| 3 | ############################################################################### |
| 4 | # |
| 5 | # Scatter - A writer class for Excel Scatter 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 SCATTER chart BIFF record. Defines a scatter chart type. |
| 49 | # |
| 50 | sub _store_chart_type { |
| 51 | |
| 52 | my $self = shift; |
| 53 | |
| 54 | my $record = 0x101B; # Record identifier. |
| 55 | my $length = 0x0006; # Number of bytes to follow. |
| 56 | my $bubble_ratio = 0x0064; # Bubble ratio. |
| 57 | my $bubble_type = 0x0001; # Bubble type. |
| 58 | my $grbit = 0x0000; # Option flags. |
| 59 | |
| 60 | my $header = pack 'vv', $record, $length; |
| 61 | my $data = ''; |
| 62 | $data .= pack 'v', $bubble_ratio; |
| 63 | $data .= pack 'v', $bubble_type; |
| 64 | $data .= pack 'v', $grbit; |
| 65 | |
| 66 | $self->_append( $header, $data ); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | ############################################################################### |
| 71 | # |
| 72 | # _store_axis_category_stream(). Overridden. |
| 73 | # |
| 74 | # Write the AXIS chart substream for the chart category. |
| 75 | # |
| 76 | # For a Scatter chart the category stream is replace with a values stream. We |
| 77 | # override this method and turn it into a values stream. |
| 78 | # |
| 79 | sub _store_axis_category_stream { |
| 80 | |
| 81 | my $self = shift; |
| 82 | |
| 83 | $self->_store_axis( 0 ); |
| 84 | |
| 85 | $self->_store_begin(); |
| 86 | $self->_store_valuerange(); |
| 87 | $self->_store_tick(); |
| 88 | $self->_store_end(); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | ############################################################################### |
| 93 | # |
| 94 | # _store_marker_dataformat_stream(). Overridden. |
| 95 | # |
| 96 | # This is an implementation of the parent abstract method to define |
| 97 | # properties of markers, linetypes, pie formats and other. |
| 98 | # |
| 99 | sub _store_marker_dataformat_stream { |
| 100 | |
| 101 | my $self = shift; |
| 102 | |
| 103 | $self->_store_dataformat( 0x0000, 0xFFFD, 0x0000 ); |
| 104 | |
| 105 | $self->_store_begin(); |
| 106 | $self->_store_3dbarshape(); |
| 107 | $self->_store_lineformat( 0x00000000, 0x0005, 0xFFFF, 0x0008, 0x004D ); |
| 108 | $self->_store_areaformat( 0x00FFFFFF, 0x0000, 0x01, 0x01, 0x4E, 0x4D ); |
| 109 | $self->_store_pieformat(); |
| 110 | $self->_store_markerformat( 0x00, 0x00, 0x02, 0x01, 0x4D, 0x4D, 0x3C ); |
| 111 | $self->_store_end(); |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |
| 116 | 1; |
| 117 | |
| 118 | |
| 119 | __END__ |
| 120 | |
| 121 | |
| 122 | =head1 NAME |
| 123 | |
| 124 | Scatter - A writer class for Excel Scatter charts. |
| 125 | |
| 126 | =head1 SYNOPSIS |
| 127 | |
| 128 | To create a simple Excel file with a Scatter chart using Spreadsheet::WriteExcel: |
| 129 | |
| 130 | #!/usr/bin/perl -w |
| 131 | |
| 132 | use strict; |
| 133 | use Spreadsheet::WriteExcel; |
| 134 | |
| 135 | my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' ); |
| 136 | my $worksheet = $workbook->add_worksheet(); |
| 137 | |
| 138 | my $chart = $workbook->add_chart( type => 'scatter' ); |
| 139 | |
| 140 | # Configure the chart. |
| 141 | $chart->add_series( |
| 142 | categories => '=Sheet1!$A$2:$A$7', |
| 143 | values => '=Sheet1!$B$2:$B$7', |
| 144 | ); |
| 145 | |
| 146 | # Add the worksheet data the chart refers to. |
| 147 | my $data = [ |
| 148 | [ 'Category', 2, 3, 4, 5, 6, 7 ], |
| 149 | [ 'Value', 1, 4, 5, 2, 1, 5 ], |
| 150 | ]; |
| 151 | |
| 152 | $worksheet->write( 'A1', $data ); |
| 153 | |
| 154 | __END__ |
| 155 | |
| 156 | =head1 DESCRIPTION |
| 157 | |
| 158 | This module implements Scatter charts for L<Spreadsheet::WriteExcel>. The chart object is created via the Workbook C<add_chart()> method: |
| 159 | |
| 160 | my $chart = $workbook->add_chart( type => 'scatter' ); |
| 161 | |
| 162 | Once the object is created it can be configured via the following methods that are common to all chart classes: |
| 163 | |
| 164 | $chart->add_series(); |
| 165 | $chart->set_x_axis(); |
| 166 | $chart->set_y_axis(); |
| 167 | $chart->set_title(); |
| 168 | |
| 169 | These methods are explained in detail in L<Spreadsheet::WriteExcel::Chart>. Class specific methods or settings, if any, are explained below. |
| 170 | |
| 171 | =head1 Scatter Chart Methods |
| 172 | |
| 173 | There aren't currently any scatter chart specific methods. See the TODO section of L<Spreadsheet::WriteExcel::Chart>. |
| 174 | |
| 175 | =head1 EXAMPLE |
| 176 | |
| 177 | Here is a complete example that demonstrates most of the available features when creating a chart. |
| 178 | |
| 179 | #!/usr/bin/perl -w |
| 180 | |
| 181 | use strict; |
| 182 | use Spreadsheet::WriteExcel; |
| 183 | |
| 184 | my $workbook = Spreadsheet::WriteExcel->new( 'chart_scatter.xls' ); |
| 185 | my $worksheet = $workbook->add_worksheet(); |
| 186 | my $bold = $workbook->add_format( bold => 1 ); |
| 187 | |
| 188 | # Add the worksheet data that the charts will refer to. |
| 189 | my $headings = [ 'Number', 'Sample 1', 'Sample 2' ]; |
| 190 | my $data = [ |
| 191 | [ 2, 3, 4, 5, 6, 7 ], |
| 192 | [ 1, 4, 5, 2, 1, 5 ], |
| 193 | [ 3, 6, 7, 5, 4, 3 ], |
| 194 | ]; |
| 195 | |
| 196 | $worksheet->write( 'A1', $headings, $bold ); |
| 197 | $worksheet->write( 'A2', $data ); |
| 198 | |
| 199 | # Create a new chart object. In this case an embedded chart. |
| 200 | my $chart = $workbook->add_chart( type => 'scatter', embedded => 1 ); |
| 201 | |
| 202 | # Configure the first series. (Sample 1) |
| 203 | $chart->add_series( |
| 204 | name => 'Sample 1', |
| 205 | categories => '=Sheet1!$A$2:$A$7', |
| 206 | values => '=Sheet1!$B$2:$B$7', |
| 207 | ); |
| 208 | |
| 209 | # Configure the second series. (Sample 2) |
| 210 | $chart->add_series( |
| 211 | name => 'Sample 2', |
| 212 | categories => '=Sheet1!$A$2:$A$7', |
| 213 | values => '=Sheet1!$C$2:$C$7', |
| 214 | ); |
| 215 | |
| 216 | # Add a chart title and some axis labels. |
| 217 | $chart->set_title ( name => 'Results of sample analysis' ); |
| 218 | $chart->set_x_axis( name => 'Test number' ); |
| 219 | $chart->set_y_axis( name => 'Sample length (cm)' ); |
| 220 | |
| 221 | # Insert the chart into the worksheet (with an offset). |
| 222 | $worksheet->insert_chart( 'D2', $chart, 25, 10 ); |
| 223 | |
| 224 | __END__ |
| 225 | |
| 226 | |
| 227 | =begin html |
| 228 | |
| 229 | <p>This will produce a chart that looks like this:</p> |
| 230 | |
| 231 | <p><center><img src="http://homepage.eircom.net/~jmcnamara/perl/images/scatter1.jpg" width="527" height="320" alt="Chart example." /></center></p> |
| 232 | |
| 233 | =end html |
| 234 | |
| 235 | |
| 236 | =head1 AUTHOR |
| 237 | |
| 238 | John McNamara jmcnamara@cpan.org |
| 239 | |
| 240 | =head1 COPYRIGHT |
| 241 | |
| 242 | Copyright MM-MMX, John McNamara. |
| 243 | |
| 244 | All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself. |
| 245 | |