File: chart_pareto.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 (87 lines) | stat: -rw-r--r-- 2,347 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
#!/usr/bin/perl

#######################################################################
#
# A demo of a Pareto 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_pareto.xlsx' );
my $worksheet = $workbook->add_worksheet();

# Formats used in the workbook.
my $bold           = $workbook->add_format( bold       => 1 );
my $percent_format = $workbook->add_format( num_format => '0.0%' );


# Widen the columns for visibility.
$worksheet->set_column( 'A:A', 15 );
$worksheet->set_column( 'B:C', 10 );

# Add the worksheet data that the charts will refer to.
my $headings = [ 'Reason', 'Number', 'Percentage' ];

my $reasons = [
    'Traffic',   'Child care', 'Public Transport', 'Weather',
    'Overslept', 'Emergency',
];

my $numbers  = [ 60,   40,    20,  15,  10,    5 ];
my $percents = [ 0.44, 0.667, 0.8, 0.9, 0.967, 1 ];

$worksheet->write_row( 'A1', $headings, $bold );
$worksheet->write_col( 'A2', $reasons );
$worksheet->write_col( 'B2', $numbers );
$worksheet->write_col( 'C2', $percents, $percent_format );


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

# Add a series.
$column_chart->add_series(
    categories => '=Sheet1!$A$2:$A$7',
    values     => '=Sheet1!$B$2:$B$7',
);

# Add a chart title.
$column_chart->set_title( name => 'Reasons for lateness' );

# Turn off the chart legend.
$column_chart->set_legend( position => 'none' );

# Set the title and scale of the Y axes. Note, the secondary axis is set from
# the primary chart.
$column_chart->set_y_axis(
    name => 'Respondents (number)',
    min  => 0,
    max  => 120
);
$column_chart->set_y2_axis( max => 1 );

# Create a new line chart. This will be the secondary chart.
my $line_chart = $workbook->add_chart( type => 'line', embedded => 1 );

# Add a series, on the secondary axis.
$line_chart->add_series(
    categories => '=Sheet1!$A$2:$A$7',
    values     => '=Sheet1!$C$2:$C$7',
    marker     => { type => 'automatic' },
    y2_axis    => 1,
);


# Combine the charts.
$column_chart->combine( $line_chart );

# Insert the chart into the worksheet.
$worksheet->insert_chart( 'F2', $column_chart );

$workbook->close();

__END__