Skip to content

Commit

Permalink
Add chart option to display N/A as empty cells.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Sep 30, 2023
1 parent 466268e commit 05a9705
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 25 deletions.
72 changes: 65 additions & 7 deletions lib/Excel/Writer/XLSX/Chart.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ sub new {
$self->{_x2_axis} = {};
$self->{_chart_name} = '';
$self->{_show_blanks} = 'gap';
$self->{_show_na_as_empty} = 0;
$self->{_show_hidden_data} = 0;
$self->{_show_crosses} = 1;
$self->{_width} = 480;
Expand Down Expand Up @@ -511,6 +512,21 @@ sub show_blanks_as {
}



###############################################################################
#
# show_na_as_empty_cell()
#
# Set the option for displaying #N/A as an empty cell in a chart.
#
sub show_na_as_empty_cell {

my $self = shift;

$self->{_show_na_as_empty} = 1;
}


###############################################################################
#
# show_hidden_data()
Expand Down Expand Up @@ -2521,6 +2537,12 @@ sub _write_chart {
# Write the c:dispBlanksAs element.
$self->_write_disp_blanks_as();


if ( $self->{_show_na_as_empty} ) {
# Write the c:extLst element.
$self->_write_ext_lst_display_na();
}

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

Expand Down Expand Up @@ -2803,7 +2825,7 @@ sub _write_ser {

if ( $series->{_inverted_color} ) {
# Write the c:extLst element.
$self->_write_ext_lst( $series->{_inverted_color} );
$self->_write_ext_lst_inverted_fill( $series->{_inverted_color} );
}


Expand All @@ -2813,26 +2835,25 @@ sub _write_ser {

##############################################################################
#
# _write_ext_lst()
# _write_ext_lst_inverted_fill()
#
# Write the <c:extLst> element for the inverted fill color.
#
sub _write_ext_lst {
sub _write_ext_lst_inverted_fill {

my $self = shift;
my $color = shift;

my $uri = '{6F2FDCE9-48DA-4B69-8628-5D25D57E5C99}';
my $xmlns_c_14 =
'http://schemas.microsoft.com/office/drawing/2007/8/2/chart';
my $uri = '{6F2FDCE9-48DA-4B69-8628-5D25D57E5C99}';
my $xmlns_c_14 = 'http://schemas.microsoft.com/office/drawing/2007/8/2/chart';


my @attributes1 = (
'uri' => $uri,
'xmlns:c14' => $xmlns_c_14,
);

my @attributes2 = ( 'xmlns:c14' => $xmlns_c_14, );
my @attributes2 = ( 'xmlns:c14' => $xmlns_c_14 );


$self->xml_start_tag( 'c:extLst' );
Expand All @@ -2848,6 +2869,38 @@ sub _write_ext_lst {
$self->xml_end_tag( 'c:extLst' );
}


##############################################################################
#
# _write_ext_lst_display_na()
#
# Write the <c:extLst> element for the display N/A as empty cell option.
#
sub _write_ext_lst_display_na {

my $self = shift;
my $color = shift;

my $uri = '{56B9EC1D-385E-4148-901F-78D8002777C0}';
my $xmlns_c_16 = 'http://schemas.microsoft.com/office/drawing/2017/03/chart';

my @attributes1 = (
'uri' => $uri,
'xmlns:c16r3' => $xmlns_c_16,
);

my @attributes2 = ( 'val' => 1 );

$self->xml_start_tag( 'c:extLst' );
$self->xml_start_tag( 'c:ext', @attributes1 );
$self->xml_start_tag( 'c16r3:dataDisplayOptions16' );
$self->xml_empty_tag( 'c16r3:dispNaAsBlank', @attributes2 );
$self->xml_end_tag( 'c16r3:dataDisplayOptions16' );
$self->xml_end_tag( 'c:ext' );
$self->xml_end_tag( 'c:extLst' );
}


##############################################################################
#
# _write_idx()
Expand Down Expand Up @@ -7839,6 +7892,11 @@ The available options are:
span # Blank data is connected with a line.
=head2 show_na_as_empty_cell()
The C<show_na_as_empty_cell()> method enables the option to display C<#N/A> as a blank cell in a chart.
=head2 show_hidden_data()
Display data in hidden rows or columns on the chart.
Expand Down
37 changes: 19 additions & 18 deletions lib/Excel/Writer/XLSX/Chartsheet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,25 @@ sub protect {
#
###############################################################################

sub add_series { return shift->{_chart}->add_series( @_ ) }
sub combine { return shift->{_chart}->combine( @_ ) }
sub set_x_axis { return shift->{_chart}->set_x_axis( @_ ) }
sub set_y_axis { return shift->{_chart}->set_y_axis( @_ ) }
sub set_x2_axis { return shift->{_chart}->set_x2_axis( @_ ) }
sub set_y2_axis { return shift->{_chart}->set_y2_axis( @_ ) }
sub set_title { return shift->{_chart}->set_title( @_ ) }
sub set_legend { return shift->{_chart}->set_legend( @_ ) }
sub set_plotarea { return shift->{_chart}->set_plotarea( @_ ) }
sub set_chartarea { return shift->{_chart}->set_chartarea( @_ ) }
sub set_style { return shift->{_chart}->set_style( @_ ) }
sub show_blanks_as { return shift->{_chart}->show_blanks_as( @_ ) }
sub show_hidden_data { return shift->{_chart}->show_hidden_data( @_ ) }
sub set_size { return shift->{_chart}->set_size( @_ ) }
sub set_table { return shift->{_chart}->set_table( @_ ) }
sub set_up_down_bars { return shift->{_chart}->set_up_down_bars( @_ ) }
sub set_drop_lines { return shift->{_chart}->set_drop_lines( @_ ) }
sub set_high_low_lines { return shift->{_chart}->high_low_lines( @_ ) }
sub add_series { return shift->{_chart}->add_series( @_ ) }
sub combine { return shift->{_chart}->combine( @_ ) }
sub set_x_axis { return shift->{_chart}->set_x_axis( @_ ) }
sub set_y_axis { return shift->{_chart}->set_y_axis( @_ ) }
sub set_x2_axis { return shift->{_chart}->set_x2_axis( @_ ) }
sub set_y2_axis { return shift->{_chart}->set_y2_axis( @_ ) }
sub set_title { return shift->{_chart}->set_title( @_ ) }
sub set_legend { return shift->{_chart}->set_legend( @_ ) }
sub set_plotarea { return shift->{_chart}->set_plotarea( @_ ) }
sub set_chartarea { return shift->{_chart}->set_chartarea( @_ ) }
sub set_style { return shift->{_chart}->set_style( @_ ) }
sub show_blanks_as { return shift->{_chart}->show_blanks_as( @_ ) }
sub show_na_as_empty_cell { return shift->{_chart}->show_na_as_empty_cell( @_ ) }
sub show_hidden_data { return shift->{_chart}->show_hidden_data( @_ ) }
sub set_size { return shift->{_chart}->set_size( @_ ) }
sub set_table { return shift->{_chart}->set_table( @_ ) }
sub set_up_down_bars { return shift->{_chart}->set_up_down_bars( @_ ) }
sub set_drop_lines { return shift->{_chart}->set_drop_lines( @_ ) }
sub set_high_low_lines { return shift->{_chart}->high_low_lines( @_ ) }



Expand Down
89 changes: 89 additions & 0 deletions t/regression/chart_blank07.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
###############################################################################
#
# Tests the output of Excel::Writer::XLSX against Excel generated files.
#
# Copyright 2000-2023, John McNamara, [email protected]
#
# 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_blank07.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 a simple Excel::Writer::XLSX file.
#
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} = [ 45705856, 45843584 ];

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

];

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

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

$chart->show_na_as_empty_cell();

$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__
Binary file added t/regression/xlsx_files/chart_blank07.xlsx
Binary file not shown.

0 comments on commit 05a9705

Please sign in to comment.