Spreadsheet::WriteExcel with Charts

In the past I've avoided trying to implement charts in Spreadsheet::WriteExcel because the task always seemed too big to tackle in the time scales that I get to work with.

However, prompted by a module user, I went back to revisit it. The task was still too big but I did it anyway.

So I've uploaded version 2.32 of Spreadsheet::WriteExcel to CPAN. Here is the documentation for Charts and here is an example.

#!/usr/bin/perl -w

use strict;
use Spreadsheet::WriteExcel;

my $workbook  = Spreadsheet::WriteExcel->new( 'chart.xls' );
my $worksheet = $workbook->add_worksheet();

my $chart     = $workbook->add_chart( name => 'Chart1', type => 'line' );

# Configure the chart.
$chart->add_series(
    categories => '=Sheet1!$A$2:$A$7',
    values     => '=Sheet1!$B$2:$B$7',
);

# Add the data to the worksheet the chart refers to.
my $data = [
    [ 'Category', 2, 3, 4, 5, 6, 7 ],
    [ 'Value',    1, 4, 5, 2, 1, 5 ],
];

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

Here is a slightly longer example from the distro.

There is still plently of work to be done but it is a good start.

John.

4 Comments

um ... I get this error.

Can't locate object method "add_chart" via package "Spreadsheet::WriteExcel"

in both examples. I have Spreadsheet-WriteExcel-2.31

Also I was getting this error with the Makefile.PL for 2.32.

Argument "6.55_02" isn't numeric in numeric ge (>=) at ... line 34.

So I changed your version check for e::mm to this


my @emm_v = split('_', $ExtUtils::MakeMaker::VERSION );

if ( $ExtUtils::MakeMaker::VERSION >= 6.46 ) {
$params{META_MERGE} = { resources => \%resources };
}

Hi all i had tried to execute the following perl script to plot tha data but i am getting
ocate object method add_chart via package Spreadsheet::WriteExcel
error can u help this ...!!!


#!/usr/bin/perl -w

use strict;
use Spreadsheet::WriteExcel;
use Spreadsheet::WriteExcel::Workbook;

my $workbook = Spreadsheet::WriteExcel->new( 'chart.xls' );
my $worksheet = $workbook->add_worksheet();

my $chart = $worksheet->add_chart_ext( type => 'column' );

# Configure the chart.
$chart->add_series(
categories => '=Sheet1!$A$2:$A$7',
values => '=Sheet1!$B$2:$B$7',
);

# Add the worksheet data the chart refers to.
my $data = [
[ 'Category', 2, 3, 4, 5, 6, 7 ],
[ 'Value', 1, 4, 5, 2, 1, 5 ],
];

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

Leave a comment

About John McNamara

user-pic Just another Perl hacker