1Excel::Writer::XLSX::ChUasretr::CPoinet(r3i)buted Perl DEoxccuemle:n:tWartiitoenr::XLSX::Chart::Pie(3)
2
3
4
6 Pie - A class for writing Excel Pie charts.
7
9 To create a simple Excel file with a Pie chart using
10 Excel::Writer::XLSX:
11
12 #!/usr/bin/perl
13
14 use strict;
15 use warnings;
16 use Excel::Writer::XLSX;
17
18 my $workbook = Excel::Writer::XLSX->new( 'chart.xlsx' );
19 my $worksheet = $workbook->add_worksheet();
20
21 my $chart = $workbook->add_chart( type => 'pie' );
22
23 # Configure the chart.
24 $chart->add_series(
25 categories => '=Sheet1!$A$2:$A$7',
26 values => '=Sheet1!$B$2:$B$7',
27 );
28
29 # Add the worksheet data the chart refers to.
30 my $data = [
31 [ 'Category', 2, 3, 4, 5, 6, 7 ],
32 [ 'Value', 1, 4, 5, 2, 1, 5 ],
33 ];
34
35 $worksheet->write( 'A1', $data );
36
37 __END__
38
40 This module implements Pie charts for Excel::Writer::XLSX. The chart
41 object is created via the Workbook "add_chart()" method:
42
43 my $chart = $workbook->add_chart( type => 'pie' );
44
45 Once the object is created it can be configured via the following
46 methods that are common to all chart classes:
47
48 $chart->add_series();
49 $chart->set_title();
50
51 These methods are explained in detail in Excel::Writer::XLSX::Chart.
52 Class specific methods or settings, if any, are explained below.
53
55 set_rotation()
56 The "set_rotation()" method is used to set the rotation of the first
57 segment of a Pie/Doughnut chart. This has the effect of rotating the
58 entire chart:
59
60 $chart->set_rotation( 90 );
61
62 The angle of rotation must be "0 <= rotation <= 360".
63
64 User defined colors
65 It is possible to define chart colors for most types of
66 Excel::Writer::XLSX charts via the add_series() method. However,
67 Pie/Doughnut charts are a special case since each segment is
68 represented as a point so it is necessary to assign formatting to each
69 point in the series:
70
71 $chart->add_series(
72 values => '=Sheet1!$A$1:$A$3',
73 points => [
74 { fill => { color => '#FF0000' } },
75 { fill => { color => '#CC0000' } },
76 { fill => { color => '#990000' } },
77 ],
78 );
79
80 See the main Excel::Writer::XLSX::Chart documentation for more details.
81
82 Pie charts support leader lines:
83
84 $chart->add_series(
85 name => 'Pie sales data',
86 categories => [ 'Sheet1', 1, 3, 0, 0 ],
87 values => [ 'Sheet1', 1, 3, 1, 1 ],
88 data_labels => {
89 series_name => 1,
90 percentage => 1,
91 leader_lines => 1,
92 position => 'outside_end'
93 },
94 );
95
96 Note: Even when leader lines are turned on they aren't automatically
97 visible in Excel or Excel::Writer::XLSX. Due to an Excel limitation (or
98 design) leader lines only appear if the data label is moved manually or
99 if the data labels are very close and need to be adjusted
100 automatically.
101
102 Unsupported Methods
103 A Pie chart doesn't have an X or Y axis so the following common chart
104 methods are ignored.
105
106 $chart->set_x_axis();
107 $chart->set_y_axis();
108
110 Here is a complete example that demonstrates most of the available
111 features when creating a chart.
112
113 #!/usr/bin/perl
114
115 use strict;
116 use warnings;
117 use Excel::Writer::XLSX;
118
119 my $workbook = Excel::Writer::XLSX->new( 'chart_pie.xlsx' );
120 my $worksheet = $workbook->add_worksheet();
121 my $bold = $workbook->add_format( bold => 1 );
122
123 # Add the worksheet data that the charts will refer to.
124 my $headings = [ 'Category', 'Values' ];
125 my $data = [
126 [ 'Apple', 'Cherry', 'Pecan' ],
127 [ 60, 30, 10 ],
128 ];
129
130 $worksheet->write( 'A1', $headings, $bold );
131 $worksheet->write( 'A2', $data );
132
133 # Create a new chart object. In this case an embedded chart.
134 my $chart = $workbook->add_chart( type => 'pie', embedded => 1 );
135
136 # Configure the series. Note the use of the array ref to define ranges:
137 # [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
138 $chart->add_series(
139 name => 'Pie sales data',
140 categories => [ 'Sheet1', 1, 3, 0, 0 ],
141 values => [ 'Sheet1', 1, 3, 1, 1 ],
142 );
143
144 # Add a title.
145 $chart->set_title( name => 'Popular Pie Types' );
146
147 # Set an Excel chart style. Colors with white outline and shadow.
148 $chart->set_style( 10 );
149
150 # Insert the chart into the worksheet (with an offset).
151 $worksheet->insert_chart( 'C2', $chart, 25, 10 );
152
153 __END__
154
156 John McNamara jmcnamara@cpan.org
157
159 Copyright MM-MMXIX, John McNamara.
160
161 All Rights Reserved. This module is free software. It may be used,
162 redistributed and/or modified under the same terms as Perl itself.
163
164
165
166perl v5.30.1 2020-01-30Excel::Writer::XLSX::Chart::Pie(3)