Skip to content

Commit

Permalink
chart: add trendline equation formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Oct 16, 2023
1 parent 48ce5d3 commit a9e84bc
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 4 deletions.
82 changes: 78 additions & 4 deletions lib/Excel/Writer/XLSX/Chart.pm
Expand Up @@ -1547,6 +1547,7 @@ sub _get_trendline_properties {
return;
}


# Set the line properties for the trendline..
my $line = $self->_get_line_properties( $trendline->{line} );

Expand All @@ -1558,12 +1559,15 @@ sub _get_trendline_properties {
# Set the fill properties for the trendline.
my $fill = $self->_get_fill_properties( $trendline->{fill} );

# Set the pattern properties for the series.
# Set the pattern properties for the trendline.
my $pattern = $self->_get_pattern_properties( $trendline->{pattern} );

# Set the gradient fill properties for the series.
# Set the gradient fill properties for the trendline.
my $gradient = $self->_get_gradient_properties( $trendline->{gradient} );

# Set the format properties for the trendline label.
my $label = $self->_get_trendline_label_properties( $trendline->{label} );

# Pattern fill overrides solid fill.
if ( $pattern ) {
$fill = undef;
Expand All @@ -1579,11 +1583,72 @@ sub _get_trendline_properties {
$trendline->{_fill} = $fill;
$trendline->{_pattern} = $pattern;
$trendline->{_gradient} = $gradient;
$trendline->{_label} = $label;

return $trendline;
}


###############################################################################
#
# _get_trendline_label_properties()
#
# Convert user defined trendline label properties to the structure required
# internally.
#
sub _get_trendline_label_properties {

my $self = shift;
my $label = shift;

return if !$label && ref $label ne 'HASH';

# Copy the user supplied properties.
$label = {%$label};

# Set the font properties for the label.
if ($label->{font}) {
$label->{font} = $self->_convert_font_args( $label->{font} );
}


# Set the line properties for the label.
my $line = $self->_get_line_properties( $label->{line} );

# Allow 'border' as a synonym for 'line'.
if ( $label->{border} ) {
$line = $self->_get_line_properties( $label->{border} );
}

# Set the fill properties for the label.
my $fill = $self->_get_fill_properties( $label->{fill} );

# Set the pattern properties for the label.
my $pattern = $self->_get_pattern_properties( $label->{pattern} );

# Set the gradient fill properties for the label.
my $gradient = $self->_get_gradient_properties( $label->{gradient} );

# Pattern fill overrides solid fill.
if ( $pattern ) {
$fill = undef;
}

# Gradient fill overrides solid and pattern fills.
if ( $gradient ) {
$pattern = undef;
$fill = undef;
}

$label->{_line} = $line;
$label->{_fill} = $fill;
$label->{_pattern} = $pattern;
$label->{_gradient} = $gradient;

return $label;
}


###############################################################################
#
# _get_error_bars_properties()
Expand Down Expand Up @@ -5292,7 +5357,7 @@ sub _write_trendline {
$self->_write_disp_eq();

# Write the c:trendlineLbl element.
$self->_write_trendline_lbl();
$self->_write_trendline_lbl( $trendline );
}

$self->xml_end_tag( 'c:trendline' );
Expand Down Expand Up @@ -5461,7 +5526,8 @@ sub _write_disp_rsqr {
#
sub _write_trendline_lbl {

my $self = shift;
my $self = shift;
my $trendline = shift;

$self->xml_start_tag( 'c:trendlineLbl' );

Expand All @@ -5471,6 +5537,14 @@ sub _write_trendline_lbl {
# Write the c:numFmt element.
$self->_write_trendline_num_fmt();

# Write the c:spPr element for the label formatting.
$self->_write_sp_pr( $trendline->{_label} );

# Write the data label font elements.
if ($trendline->{_label}->{font} ) {
$self->_write_axis_font( $trendline->{_label}->{font} );
}

$self->xml_end_tag( 'c:trendlineLbl' );
}

Expand Down
98 changes: 98 additions & 0 deletions t/regression/chart_format33.t
@@ -0,0 +1,98 @@
###############################################################################
#
# Tests the output of Excel::Writer::XLSX against Excel generated files.
#
# Copyright 2000-2023, John McNamara, jmcnamara@cpan.org
#
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
#

use lib 't/lib';
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
use strict;
use warnings;

use Test::More tests => 1;

###############################################################################
#
# Tests setup.
#
my $filename = 'chart_format33.xlsx';
my $dir = 't/regression/';
my $got_filename = $dir . "ewx_$filename";
my $exp_filename = $dir . 'xlsx_files/' . $filename;

my $ignore_members = [];

my $ignore_elements = {};


###############################################################################
#
# Test the creation of an Excel::Writer::XLSX file with chart formatting.
#
use Excel::Writer::XLSX;

my $workbook = Excel::Writer::XLSX->new( $got_filename );
my $worksheet = $workbook->add_worksheet();
my $chart = $workbook->add_chart( type => 'line', embedded => 1 );

# For testing, copy the randomly generated axis ids in the target xlsx file.
$chart->{_axis_ids} = [ 52978048, 52980352 ];

my $data = [
[ 1, 2, 3, 4, 5 ],
[ 2, 4, 6, 8, 10 ],
[ 3, 6, 9, 12, 15 ],

];

$worksheet->write( 'A1', $data );

$chart->add_series(
categories => '=Sheet1!$A$1:$A$5',
values => '=Sheet1!$B$1:$B$5',
trendline => {
type => 'linear',
display_equation => 1,
label => { fill => { color => 'red' } },
}
);

$chart->add_series(
categories => '=Sheet1!$A$1:$A$5',
values => '=Sheet1!$C$1:$C$5',
);

$worksheet->insert_chart( 'E9', $chart );

$workbook->close();


###############################################################################
#
# Compare the generated and existing Excel files.
#

my ( $got, $expected, $caption ) = _compare_xlsx_files(

$got_filename,
$exp_filename,
$ignore_members,
$ignore_elements,
);

_is_deep_diff( $got, $expected, $caption );


###############################################################################
#
# Cleanup.
#
unlink $got_filename;

__END__
101 changes: 101 additions & 0 deletions t/regression/chart_format34.t
@@ -0,0 +1,101 @@
###############################################################################
#
# Tests the output of Excel::Writer::XLSX against Excel generated files.
#
# Copyright 2000-2023, John McNamara, jmcnamara@cpan.org
#
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
#

use lib 't/lib';
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
use strict;
use warnings;

use Test::More tests => 1;

###############################################################################
#
# Tests setup.
#
my $filename = 'chart_format34.xlsx';
my $dir = 't/regression/';
my $got_filename = $dir . "ewx_$filename";
my $exp_filename = $dir . 'xlsx_files/' . $filename;

my $ignore_members = [];

my $ignore_elements = {};


###############################################################################
#
# Test the creation of an Excel::Writer::XLSX file with chart formatting.
#
use Excel::Writer::XLSX;

my $workbook = Excel::Writer::XLSX->new( $got_filename );
my $worksheet = $workbook->add_worksheet();
my $chart = $workbook->add_chart( type => 'line', embedded => 1 );

# For testing, copy the randomly generated axis ids in the target xlsx file.
$chart->{_axis_ids} = [ 52978048, 52980352 ];

my $data = [
[ 1, 2, 3, 4, 5 ],
[ 2, 4, 6, 8, 10 ],
[ 3, 6, 9, 12, 15 ],

];

$worksheet->write( 'A1', $data );

$chart->add_series(
categories => '=Sheet1!$A$1:$A$5',
values => '=Sheet1!$B$1:$B$5',
trendline => {
type => 'linear',
display_equation => 1,
label => {
fill => { color => 'red' },
border => { color => 'yellow' }
},
}
);

$chart->add_series(
categories => '=Sheet1!$A$1:$A$5',
values => '=Sheet1!$C$1:$C$5',
);

$worksheet->insert_chart( 'E9', $chart );

$workbook->close();


###############################################################################
#
# Compare the generated and existing Excel files.
#

my ( $got, $expected, $caption ) = _compare_xlsx_files(

$got_filename,
$exp_filename,
$ignore_members,
$ignore_elements,
);

_is_deep_diff( $got, $expected, $caption );


###############################################################################
#
# Cleanup.
#
unlink $got_filename;

__END__

0 comments on commit a9e84bc

Please sign in to comment.