Skip to content

Commit 474667d

Browse files
committed
chart: add axis title formatting
1 parent 27c86cb commit 474667d

File tree

5 files changed

+213
-6
lines changed

5 files changed

+213
-6
lines changed

lib/Excel/Writer/XLSX/Chart.pm

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,16 @@ sub _convert_axis_args {
826826
$axis->{_title}{_font} = $self->_convert_font_args( $arg{name_font} );
827827
$axis->{_title}{_layout} = $self->_get_layout_properties( $arg{name_layout}, 1 );
828828

829+
# Set the format properties.
830+
$axis->{_title}{_line} = $self->_get_line_properties( $arg{name_line} );
831+
if ( $arg{name_border} ) {
832+
$axis->{_title}{_line} = $self->_get_line_properties( $arg{name_border} );
833+
}
834+
835+
$axis->{_title}{_fill} = $self->_get_fill_properties( $arg{name_fill} );
836+
$axis->{_title}{_pattern} = $self->_get_pattern_properties( $arg{name_pattern} );
837+
$axis->{_title}{_gradient} = $self->_get_gradient_properties( $arg{name_gradient} );
838+
829839
return $axis;
830840
}
831841

@@ -7359,8 +7369,12 @@ The properties that can be set are:
73597369
name
73607370
name_font
73617371
name_layout
7362-
num_font
7372+
name_line
7373+
name_fill
7374+
name_pattern
7375+
name_gradient
73637376
num_format
7377+
num_font
73647378
line
73657379
fill
73667380
pattern
@@ -7440,22 +7454,37 @@ The number format is similar to the Worksheet Cell Format C<num_format> apart fr
74407454
74417455
=item * C<line>
74427456
7443-
Set the properties of the axis line type such as colour and width. See the L</CHART FORMATTING> section below.
7457+
Set the properties of the axis line/border type such as colour and width. See the L</CHART FORMATTING> section below.
74447458
74457459
$chart->set_x_axis( line => { none => 1 });
74467460
7447-
74487461
=item * C<fill>
74497462
7450-
Set the fill properties of the axis such as colour. See the L</CHART FORMATTING> section below. Note, in Excel the axis fill is applied to the area of the numbers of the axis and not to the area of the axis bounding box. That background is set from the chartarea fill.
7463+
Set the fill properties of the axis. See the L</CHART FORMATTING> section below. Note, in Excel the axis fill is applied to the area of the numbers of the axis and not to the area of the axis bounding box. That background is set from the chartarea fill.
74517464
74527465
=item * C<pattern>
74537466
7454-
Set the pattern properties of the axis such as colour. See the L</CHART FORMATTING> section below.
7467+
Set the pattern properties of the axis. See the L</CHART FORMATTING> section below.
74557468
74567469
=item * C<gradient>
74577470
7458-
Set the gradient properties of the axis such as colour. See the L</CHART FORMATTING> section below.
7471+
Set the gradient properties of the axis. See the L</CHART FORMATTING> section below.
7472+
7473+
=item * C<name_line>
7474+
7475+
Set the properties of the axis title/name line/border. See the L</CHART FORMATTING> section below.
7476+
7477+
=item * C<name_fill>
7478+
7479+
Set the fill properties of the axis title/name. See the L</CHART FORMATTING> section below.
7480+
7481+
=item * C<name_pattern>
7482+
7483+
Set the pattern properties of the axis title/name. See the L</CHART FORMATTING> section below.
7484+
7485+
=item * C<name_gradient>
7486+
7487+
Set the gradient properties of the axis title/name. See the L</CHART FORMATTING> section below.
74597488
74607489
=item * C<min>
74617490

t/regression/chart_axis53.t

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
###############################################################################
2+
#
3+
# Tests the output of Excel::Writer::XLSX against Excel generated files.
4+
#
5+
# Copyright 2000-2024, John McNamara, jmcnamara@cpan.org
6+
#
7+
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
8+
#
9+
10+
use lib 't/lib';
11+
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
12+
use strict;
13+
use warnings;
14+
15+
use Test::More tests => 1;
16+
17+
###############################################################################
18+
#
19+
# Tests setup.
20+
#
21+
my $filename = 'chart_axis53.xlsx';
22+
my $dir = 't/regression/';
23+
my $got_filename = $dir . "ewx_$filename";
24+
my $exp_filename = $dir . 'xlsx_files/' . $filename;
25+
26+
my $ignore_members = [];
27+
my $ignore_elements = {};
28+
29+
30+
###############################################################################
31+
#
32+
# Test the creation of a simple Excel::Writer::XLSX file.
33+
#
34+
use Excel::Writer::XLSX;
35+
36+
my $workbook = Excel::Writer::XLSX->new( $got_filename );
37+
my $worksheet = $workbook->add_worksheet();
38+
my $chart = $workbook->add_chart( type => 'column', embedded => 1 );
39+
40+
# For testing, copy the randomly generated axis ids in the target xlsx file.
41+
$chart->{_axis_ids} = [ 56294784, 63555072 ];
42+
43+
my $data = [
44+
[ 1, 2, 3, 4, 5 ],
45+
[ 2, 4, 6, 8, 10 ],
46+
[ 3, 6, 9, 12, 15 ],
47+
48+
];
49+
50+
$worksheet->write( 'A1', $data );
51+
52+
$chart->add_series( values => '=Sheet1!$A$1:$A$5' );
53+
$chart->add_series( values => '=Sheet1!$B$1:$B$5' );
54+
$chart->add_series( values => '=Sheet1!$C$1:$C$5' );
55+
56+
$chart->set_x_axis( name => 'XXX', name_fill => { color => 'yellow' } );
57+
$chart->set_y_axis( name => 'YYY' );
58+
59+
$worksheet->insert_chart( 'E9', $chart );
60+
61+
$workbook->close();
62+
63+
64+
###############################################################################
65+
#
66+
# Compare the generated and existing Excel files.
67+
#
68+
69+
my ( $got, $expected, $caption ) = _compare_xlsx_files(
70+
71+
$got_filename,
72+
$exp_filename,
73+
$ignore_members,
74+
$ignore_elements,
75+
);
76+
77+
_is_deep_diff( $got, $expected, $caption );
78+
79+
80+
###############################################################################
81+
#
82+
# Cleanup.
83+
#
84+
unlink $got_filename;
85+
86+
__END__
87+
88+
89+

t/regression/chart_axis54.t

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
###############################################################################
2+
#
3+
# Tests the output of Excel::Writer::XLSX against Excel generated files.
4+
#
5+
# Copyright 2000-2024, John McNamara, jmcnamara@cpan.org
6+
#
7+
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
8+
#
9+
10+
use lib 't/lib';
11+
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
12+
use strict;
13+
use warnings;
14+
15+
use Test::More tests => 1;
16+
17+
###############################################################################
18+
#
19+
# Tests setup.
20+
#
21+
my $filename = 'chart_axis54.xlsx';
22+
my $dir = 't/regression/';
23+
my $got_filename = $dir . "ewx_$filename";
24+
my $exp_filename = $dir . 'xlsx_files/' . $filename;
25+
26+
my $ignore_members = [];
27+
my $ignore_elements = {};
28+
29+
30+
###############################################################################
31+
#
32+
# Test the creation of a simple Excel::Writer::XLSX file.
33+
#
34+
use Excel::Writer::XLSX;
35+
36+
my $workbook = Excel::Writer::XLSX->new( $got_filename );
37+
my $worksheet = $workbook->add_worksheet();
38+
my $chart = $workbook->add_chart( type => 'column', embedded => 1 );
39+
40+
# For testing, copy the randomly generated axis ids in the target xlsx file.
41+
$chart->{_axis_ids} = [ 63560704, 74118272 ];
42+
43+
my $data = [
44+
[ 1, 2, 3, 4, 5 ],
45+
[ 2, 4, 6, 8, 10 ],
46+
[ 3, 6, 9, 12, 15 ],
47+
48+
];
49+
50+
$worksheet->write( 'A1', $data );
51+
52+
$chart->add_series( values => '=Sheet1!$A$1:$A$5' );
53+
$chart->add_series( values => '=Sheet1!$B$1:$B$5' );
54+
$chart->add_series( values => '=Sheet1!$C$1:$C$5' );
55+
56+
$chart->set_x_axis( name => 'XXX', name_fill => { color => 'yellow' }, name_line => { color => 'red' } );
57+
$chart->set_y_axis( name => 'YYY', name_fill => { color => 'red' }, name_line => { color => 'yellow' } );
58+
59+
$worksheet->insert_chart( 'E9', $chart );
60+
61+
$workbook->close();
62+
63+
64+
###############################################################################
65+
#
66+
# Compare the generated and existing Excel files.
67+
#
68+
69+
my ( $got, $expected, $caption ) = _compare_xlsx_files(
70+
71+
$got_filename,
72+
$exp_filename,
73+
$ignore_members,
74+
$ignore_elements,
75+
);
76+
77+
_is_deep_diff( $got, $expected, $caption );
78+
79+
80+
###############################################################################
81+
#
82+
# Cleanup.
83+
#
84+
unlink $got_filename;
85+
86+
__END__
87+
88+
89+
9.2 KB
Binary file not shown.
9.23 KB
Binary file not shown.

0 commit comments

Comments
 (0)