1Excel::Writer::XLSX::ChUasretr::CBoanrt(r3i)buted Perl DEoxccuemle:n:tWartiitoenr::XLSX::Chart::Bar(3)
2
3
4
6 Bar - A class for writing Excel Bar charts.
7
9 To create a simple Excel file with a Bar 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 => 'bar' );
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 Bar 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 => 'bar' );
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_x_axis();
50 $chart->set_y_axis();
51 $chart->set_title();
52
53 These methods are explained in detail in Excel::Writer::XLSX::Chart.
54 Class specific methods or settings, if any, are explained below.
55
57 The "Bar" chart module also supports the following sub-types:
58
59 stacked
60 percent_stacked
61
62 These can be specified at creation time via the add_chart() Worksheet
63 method:
64
65 my $chart = $workbook->add_chart( type => 'bar', subtype => 'stacked' );
66
68 Here is a complete example that demonstrates most of the available
69 features when creating a chart.
70
71 #!/usr/bin/perl
72
73 use strict;
74 use warnings;
75 use Excel::Writer::XLSX;
76
77 my $workbook = Excel::Writer::XLSX->new( 'chart_bar.xlsx' );
78 my $worksheet = $workbook->add_worksheet();
79 my $bold = $workbook->add_format( bold => 1 );
80
81 # Add the worksheet data that the charts will refer to.
82 my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
83 my $data = [
84 [ 2, 3, 4, 5, 6, 7 ],
85 [ 10, 40, 50, 20, 10, 50 ],
86 [ 30, 60, 70, 50, 40, 30 ],
87
88 ];
89
90 $worksheet->write( 'A1', $headings, $bold );
91 $worksheet->write( 'A2', $data );
92
93 # Create a new chart object. In this case an embedded chart.
94 my $chart = $workbook->add_chart( type => 'bar', embedded => 1 );
95
96 # Configure the first series.
97 $chart->add_series(
98 name => '=Sheet1!$B$1',
99 categories => '=Sheet1!$A$2:$A$7',
100 values => '=Sheet1!$B$2:$B$7',
101 );
102
103 # Configure second series. Note alternative use of array ref to define
104 # ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
105 $chart->add_series(
106 name => '=Sheet1!$C$1',
107 categories => [ 'Sheet1', 1, 6, 0, 0 ],
108 values => [ 'Sheet1', 1, 6, 2, 2 ],
109 );
110
111 # Add a chart title and some axis labels.
112 $chart->set_title ( name => 'Results of sample analysis' );
113 $chart->set_x_axis( name => 'Test number' );
114 $chart->set_y_axis( name => 'Sample length (mm)' );
115
116 # Set an Excel chart style. Blue colors with white outline and shadow.
117 $chart->set_style( 11 );
118
119 # Insert the chart into the worksheet (with an offset).
120 $worksheet->insert_chart( 'D2', $chart, 25, 10 );
121
122 __END__
123
125 John McNamara jmcnamara@cpan.org
126
128 Copyright MM-MMXXI, John McNamara.
129
130 All Rights Reserved. This module is free software. It may be used,
131 redistributed and/or modified under the same terms as Perl itself.
132
133
134
135perl v5.36.0 2023-01-20Excel::Writer::XLSX::Chart::Bar(3)