<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

#######################################################################
#
# A demo of an Column chart with a data table on the X-axis using
# Excel::Writer::XLSX.
#
# Copyright 2000-2020, John McNamara, jmcnamara@cpan.org
#

use strict;
use warnings;
use Excel::Writer::XLSX;

my $workbook  = Excel::Writer::XLSX-&gt;new( 'chart_data_table.xlsx' );
my $worksheet = $workbook-&gt;add_worksheet();
my $bold      = $workbook-&gt;add_format( bold =&gt; 1 );

# Add the worksheet data that the charts will refer to.
my $headings = [ 'Number', 'Batch 1', 'Batch 2' ];
my $data = [
    [ 2,  3,  4,  5,  6,  7 ],
    [ 10, 40, 50, 20, 10, 50 ],
    [ 30, 60, 70, 50, 40, 30 ],

];

$worksheet-&gt;write( 'A1', $headings, $bold );
$worksheet-&gt;write( 'A2', $data );

# Create a column chart with a data table.
my $chart1 = $workbook-&gt;add_chart( type =&gt; 'column', embedded =&gt; 1 );

# Configure the first series.
$chart1-&gt;add_series(
    name       =&gt; '=Sheet1!$B$1',
    categories =&gt; '=Sheet1!$A$2:$A$7',
    values     =&gt; '=Sheet1!$B$2:$B$7',
);

# Configure second series. Note alternative use of array ref to define
# ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ].
$chart1-&gt;add_series(
    name       =&gt; '=Sheet1!$C$1',
    categories =&gt; [ 'Sheet1', 1, 6, 0, 0 ],
    values     =&gt; [ 'Sheet1', 1, 6, 2, 2 ],
);

# Add a chart title and some axis labels.
$chart1-&gt;set_title( name =&gt; 'Chart with Data Table' );
$chart1-&gt;set_x_axis( name =&gt; 'Test number' );
$chart1-&gt;set_y_axis( name =&gt; 'Sample length (mm)' );

# Set a default data table on the X-Axis.
$chart1-&gt;set_table();

# Insert the chart into the worksheet (with an offset).
$worksheet-&gt;insert_chart( 'D2', $chart1, { x_offset =&gt; 25, y_offset =&gt; 10 } );


#
# Create a second chart.
#
my $chart2 = $workbook-&gt;add_chart( type =&gt; 'column', embedded =&gt; 1 );

# Configure the first series.
$chart2-&gt;add_series(
    name       =&gt; '=Sheet1!$B$1',
    categories =&gt; '=Sheet1!$A$2:$A$7',
    values     =&gt; '=Sheet1!$B$2:$B$7',
);

# Configure second series.
$chart2-&gt;add_series(
    name       =&gt; '=Sheet1!$C$1',
    categories =&gt; [ 'Sheet1', 1, 6, 0, 0 ],
    values     =&gt; [ 'Sheet1', 1, 6, 2, 2 ],
);

# Add a chart title and some axis labels.
$chart2-&gt;set_title( name =&gt; 'Data Table with legend keys' );
$chart2-&gt;set_x_axis( name =&gt; 'Test number' );
$chart2-&gt;set_y_axis( name =&gt; 'Sample length (mm)' );

# Set a data table on the X-Axis with the legend keys showm.
$chart2-&gt;set_table( show_keys =&gt; 1 );

# Hide the chart legend since the keys are show on the data table.
$chart2-&gt;set_legend( position =&gt; 'none' );

# Insert the chart into the worksheet (with an offset).
$worksheet-&gt;insert_chart( 'D18', $chart2, { x_offset =&gt; 25, y_offset =&gt; 10 } );

$workbook-&gt;close();

__END__
</pre></body></html>