1#!/usr/bin/perl 2 3####################################################################### 4# 5# A demo of an Radar chart in Excel::Writer::XLSX. 6# 7# Copyright 2000-2021, John McNamara, jmcnamara@cpan.org 8# 9 10use strict; 11use warnings; 12use Excel::Writer::XLSX; 13 14my $workbook = Excel::Writer::XLSX->new( 'chart_radar.xlsx' ); 15my $worksheet = $workbook->add_worksheet(); 16my $bold = $workbook->add_format( bold => 1 ); 17 18# Add the worksheet data that the charts will refer to. 19my $headings = [ 'Number', 'Batch 1', 'Batch 2' ]; 20my $data = [ 21 [ 2, 3, 4, 5, 6, 7 ], 22 [ 30, 60, 70, 50, 40, 30 ], 23 [ 25, 40, 50, 30, 50, 40 ], 24 25]; 26 27$worksheet->write( 'A1', $headings, $bold ); 28$worksheet->write( 'A2', $data ); 29 30# Create a new chart object. In this case an embedded chart. 31my $chart1 = $workbook->add_chart( type => 'radar', embedded => 1 ); 32 33# Configure the first series. 34$chart1->add_series( 35 name => '=Sheet1!$B$1', 36 categories => '=Sheet1!$A$2:$A$7', 37 values => '=Sheet1!$B$2:$B$7', 38); 39 40# Configure second series. Note alternative use of array ref to define 41# ranges: [ $sheetname, $row_start, $row_end, $col_start, $col_end ]. 42$chart1->add_series( 43 name => '=Sheet1!$C$1', 44 categories => [ 'Sheet1', 1, 6, 0, 0 ], 45 values => [ 'Sheet1', 1, 6, 2, 2 ], 46); 47 48# Add a chart title and some axis labels. 49$chart1->set_title ( name => 'Results of sample analysis' ); 50$chart1->set_x_axis( name => 'Test number' ); 51$chart1->set_y_axis( name => 'Sample length (mm)' ); 52 53# Set an Excel chart style. Blue colors with white outline and shadow. 54$chart1->set_style( 11 ); 55 56# Insert the chart into the worksheet (with an offset). 57$worksheet->insert_chart( 'D2', $chart1, { x_offset => 25, y_offset => 10 } ); 58 59 60# 61# Create a with_markers chart sub-type 62# 63my $chart2 = $workbook->add_chart( 64 type => 'radar', 65 embedded => 1, 66 subtype => 'with_markers' 67); 68 69# Configure the first series. 70$chart2->add_series( 71 name => '=Sheet1!$B$1', 72 categories => '=Sheet1!$A$2:$A$7', 73 values => '=Sheet1!$B$2:$B$7', 74); 75 76# Configure second series. 77$chart2->add_series( 78 name => '=Sheet1!$C$1', 79 categories => [ 'Sheet1', 1, 6, 0, 0 ], 80 values => [ 'Sheet1', 1, 6, 2, 2 ], 81); 82 83# Add a chart title and some axis labels. 84$chart2->set_title ( name => 'Stacked Chart' ); 85$chart2->set_x_axis( name => 'Test number' ); 86$chart2->set_y_axis( name => 'Sample length (mm)' ); 87 88# Set an Excel chart style. Blue colors with white outline and shadow. 89$chart2->set_style( 12 ); 90 91# Insert the chart into the worksheet (with an offset). 92$worksheet->insert_chart( 'D18', $chart2, { x_offset => 25, y_offset => 10 } ); 93 94 95# 96# Create a filled chart sub-type 97# 98my $chart3 = $workbook->add_chart( 99 type => 'radar', 100 embedded => 1, 101 subtype => 'filled' 102); 103 104# Configure the first series. 105$chart3->add_series( 106 name => '=Sheet1!$B$1', 107 categories => '=Sheet1!$A$2:$A$7', 108 values => '=Sheet1!$B$2:$B$7', 109); 110 111# Configure second series. 112$chart3->add_series( 113 name => '=Sheet1!$C$1', 114 categories => [ 'Sheet1', 1, 6, 0, 0 ], 115 values => [ 'Sheet1', 1, 6, 2, 2 ], 116); 117 118# Add a chart title and some axis labels. 119$chart3->set_title ( name => 'Percent Stacked Chart' ); 120$chart3->set_x_axis( name => 'Test number' ); 121$chart3->set_y_axis( name => 'Sample length (mm)' ); 122 123# Set an Excel chart style. Blue colors with white outline and shadow. 124$chart3->set_style( 13 ); 125 126# Insert the chart into the worksheet (with an offset). 127$worksheet->insert_chart( 'D34', $chart3, { x_offset => 25, y_offset => 10 } ); 128 129$workbook->close(); 130 131__END__ 132