1Spreadsheet::WriteExcelU:s:eCrhaCrotn:t:rPiibeu(t3e)d PeSrplreDaodcsuhmeeentt:a:tWirointeExcel::Chart::Pie(3)
2
3
4
6 Pie - A writer class for Excel Pie charts.
7
9 To create a simple Excel file with a Pie chart using
10 Spreadsheet::WriteExcel:
11
12 #!/usr/bin/perl -w
13
14 use strict;
15 use Spreadsheet::WriteExcel;
16
17 my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' );
18 my $worksheet = $workbook->add_worksheet();
19
20 my $chart = $workbook->add_chart( type => 'pie' );
21
22 # Configure the chart.
23 $chart->add_series(
24 categories => '=Sheet1!$A$2:$A$7',
25 values => '=Sheet1!$B$2:$B$7',
26 );
27
28 # Add the worksheet data the chart refers to.
29 my $data = [
30 [ 'Category', 2, 3, 4, 5, 6, 7 ],
31 [ 'Value', 1, 4, 5, 2, 1, 5 ],
32 ];
33
34 $worksheet->write( 'A1', $data );
35
36 __END__
37
39 This module implements Pie charts for Spreadsheet::WriteExcel. The
40 chart object is created via the Workbook "add_chart()" method:
41
42 my $chart = $workbook->add_chart( type => 'pie' );
43
44 Once the object is created it can be configured via the following
45 methods that are common to all chart classes:
46
47 $chart->add_series();
48 $chart->set_title();
49
50 These methods are explained in detail in
51 Spreadsheet::WriteExcel::Chart. Class specific methods or settings, if
52 any, are explained below.
53
55 There aren't currently any pie chart specific methods. See the TODO
56 section of Spreadsheet::WriteExcel::Chart.
57
58 A Pie chart doesn't have an X or Y axis so the following common chart
59 methods are ignored.
60
61 $chart->set_x_axis();
62 $chart->set_y_axis();
63
65 Here is a complete example that demonstrates most of the available
66 features when creating a chart.
67
68 #!/usr/bin/perl -w
69
70 use strict;
71 use Spreadsheet::WriteExcel;
72
73 my $workbook = Spreadsheet::WriteExcel->new( 'chart_pie.xls' );
74 my $worksheet = $workbook->add_worksheet();
75 my $bold = $workbook->add_format( bold => 1 );
76
77 # Add the worksheet data that the charts will refer to.
78 my $headings = [ 'Category', 'Values' ];
79 my $data = [
80 [ 'Apple', 'Cherry', 'Pecan' ],
81 [ 60, 30, 10 ],
82 ];
83
84 $worksheet->write( 'A1', $headings, $bold );
85 $worksheet->write( 'A2', $data );
86
87 # Create a new chart object. In this case an embedded chart.
88 my $chart = $workbook->add_chart( type => 'pie', embedded => 1 );
89
90 # Configure the series.
91 $chart->add_series(
92 name => 'Pie sales data',
93 categories => '=Sheet1!$A$2:$A$4',
94 values => '=Sheet1!$B$2:$B$4',
95 );
96
97 # Add a title.
98 $chart->set_title( name => 'Popular Pie Types' );
99
100
101 # Insert the chart into the worksheet (with an offset).
102 $worksheet->insert_chart( 'C2', $chart, 25, 10 );
103
104 __END__
105
107 John McNamara jmcnamara@cpan.org
108
110 Copyright MM-MMX, John McNamara.
111
112 All Rights Reserved. This module is free software. It may be used,
113 redistributed and/or modified under the same terms as Perl itself.
114
115
116
117perl v5.32.0 2020-07S-p2r8eadsheet::WriteExcel::Chart::Pie(3)