File: chart_combined.pl

package info (click to toggle)
libexcel-writer-xlsx-perl 1.11-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,096 kB
  • sloc: perl: 22,147; makefile: 41
file content (113 lines) | stat: -rw-r--r-- 3,480 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl

#######################################################################
#
# An example of a Combined chart in Excel::Writer::XLSX.
#
# Copyright 2000-2023, John McNamara, jmcnamara@cpan.org
#

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

my $workbook  = Excel::Writer::XLSX->new( 'chart_combined.xlsx' );
my $worksheet = $workbook->add_worksheet();
my $bold      = $workbook->add_format( bold => 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->write( 'A1', $headings, $bold );
$worksheet->write( 'A2', $data );

#
# In the first example we will create a combined column and line chart.
# They will share the same X and Y axes.
#

# Create a new column chart. This will use this as the primary chart.
my $column_chart1 = $workbook->add_chart( type => 'column', embedded => 1 );

# Configure the data series for the primary chart.
$column_chart1->add_series(
    name       => '=Sheet1!$B$1',
    categories => '=Sheet1!$A$2:$A$7',
    values     => '=Sheet1!$B$2:$B$7',
);

# Create a new column chart. This will use this as the secondary chart.
my $line_chart1 = $workbook->add_chart( type => 'line', embedded => 1 );

# Configure the data series for the secondary chart.
$line_chart1->add_series(
    name       => '=Sheet1!$C$1',
    categories => '=Sheet1!$A$2:$A$7',
    values     => '=Sheet1!$C$2:$C$7',
);

# Combine the charts.
$column_chart1->combine( $line_chart1 );

# Add a chart title and some axis labels. Note, this is done via the
# primary chart.
$column_chart1->set_title( name => 'Combined chart - same Y axis' );
$column_chart1->set_x_axis( name => 'Test number' );
$column_chart1->set_y_axis( name => 'Sample length (mm)' );


# Insert the chart into the worksheet
$worksheet->insert_chart( 'E2', $column_chart1 );

#
# In the second example we will create a similar combined column and line
# chart except that the secondary chart will have a secondary Y axis.
#

# Create a new column chart. This will use this as the primary chart.
my $column_chart2 = $workbook->add_chart( type => 'column', embedded => 1 );

# Configure the data series for the primary chart.
$column_chart2->add_series(
    name       => '=Sheet1!$B$1',
    categories => '=Sheet1!$A$2:$A$7',
    values     => '=Sheet1!$B$2:$B$7',
);

# Create a new column chart. This will use this as the secondary chart.
my $line_chart2 = $workbook->add_chart( type => 'line', embedded => 1 );

# Configure the data series for the secondary chart. We also set a
# secondary Y axis via (y2_axis). This is the only difference between
# this and the first example, apart from the axis label below.
$line_chart2->add_series(
    name       => '=Sheet1!$C$1',
    categories => '=Sheet1!$A$2:$A$7',
    values     => '=Sheet1!$C$2:$C$7',
    y2_axis    => 1,
);

# Combine the charts.
$column_chart2->combine( $line_chart2 );

# Add a chart title and some axis labels.
$column_chart2->set_title(  name => 'Combine chart - secondary Y axis' );
$column_chart2->set_x_axis( name => 'Test number' );
$column_chart2->set_y_axis( name => 'Sample length (mm)' );

# Note: the y2 properites are on the secondary chart.
$line_chart2->set_y2_axis( name => 'Target length (mm)' );


# Insert the chart into the worksheet
$worksheet->insert_chart( 'E18', $column_chart2 );

$workbook->close();

__END__