wizsc has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I'm trying to plot a chart in perl from the excel sheet that I also create in the program. Below is the actual code. If I understood it correctly, the $A and $B etc under "values" in add_series function are the excel column number. I'm facing a problem where it would plot data only if I put $A in it. No other column(tried $B, $CJ). I'll appreciate help with these questions: 1. Why is nothing being plotted if I try any column other than $A? 2. I need to automate it to pick the last column of any sheet. I couldn't figure a syntax to do that. 3. How do I automate the worksheet name “sim_*” to as many sheets I have?

#!/usr/intel/pkgs/perl/5.14.1/bin/perl use Spreadsheet::WriteExcel; $num_files = @ARGV[0]; print "Help - Syntax: ./process_xyz.pl <number_of_csv_files> <all_csv_files>\n"; print " The output file is: xyz_stat.xls \n"; my $workbook = Spreadsheet::WriteExcel->new('xyz_stat.x +ls') or die $!; my $worksheet_chart = $workbook->add_worksheet('chart'); my $chart = $workbook->add_chart(type => 'line', emb +edded => 1); for ($k = 1; $k <= $num_files; $k++) { $stat_file[$k] = @ARGV[$k]; open (_file, "$stat_file[$k]") or die "cannot open $stat_file[$k]"; $frm_num = 0; $frm_start = 2; $frm_end = 3; $tmp_var = "sim_$k"; $worksheet[$k] = $workbook->add_worksheet($tmp_var); while ($line = <_file>) { @frame_stats = split(/,/,$line); my $col = 0; foreach $el(@frame_stats) { $worksheet[$k]->write($frm_num, $col, $el); $col ++; } $frm_num++; } } $chart->add_series( # categories => '=test1!$A$2:$A$10', values => '=sim_1!$B$2:$B$361', # values => '=sim_1!$A$2:$A$361', name => 'sim1', ); $chart->add_series( # categories => '=test1!$A$2:$A$10', values => '=sim_2!$CU$2:$CU$361', name => 'sim2', ); $worksheet_chart->insert_chart('E2', $chart, 15, 10);

Replies are listed 'Best First'.
Re: Plot a chart
by 1s44c (Scribe) on Jul 22, 2014 at 23:06 UTC

    You might want to format your post with some HTML markup as it's really hard to read.

      Sorry about that. Hope it's better now.
Re: Plot a chart
by poj (Abbot) on Jul 23, 2014 at 18:59 UTC
    Adapt this test script and see if it works for you
    #!perl use strict; use Spreadsheet::WriteExcel; my @data1 = map { int rand(10) }(0..34); my @data2 = map { int rand(10) }(0..34); my $workbook = Spreadsheet::WriteExcel->new('c:/temp/xyz_stat.xls') or + die $!; my $sheet = $workbook->add_worksheet('sim_1'); $sheet->write_col('B2',\@data1); $sheet = $workbook->add_worksheet('sim_2'); $sheet->write_col('CU2',\@data2); my $worksheet_chart = $workbook->add_worksheet('chart'); my $chart = $workbook->add_chart(type => 'line', embedded => 1); $chart->add_series( name=>'sim1', values=>'=sim_1!$B$2:$B$36'); $chart->add_series( name=>'sim2', values=>'=sim_2!$CU$2:$CU$36'); $worksheet_chart->insert_chart('E2', $chart, 15, 10); $workbook->close;
    poj
      Thanks, poj. Your script works just fine. I wonder if there's a scope issue in my script. Something inside the for loop is not right IMO. Does anyone see it?
        Try reading your csv files with Text:CSV_XS, adding use strict; use warnings and some print diagnostics.
        #!perl use strict; use warnings; use Spreadsheet::WriteExcel; use Text::CSV_XS; my $csv = Text::CSV_XS->new({ binary => 1, sep_char => ',', eol => "\n"}); my $xlsfile = 'c:/temp/xyz_stat1.xls'; my $workbook = Spreadsheet::WriteExcel->new($xlsfile) or die $!; my $num_files = $ARGV[0] || 0; for my $k (1..$num_files){ my $csvfile = $ARGV[$k]; print "Opening $csvfile .. "; open my $fh, '<', $csvfile or die "Cannot open $csvfile : $!"; my $sheet = $workbook->add_worksheet('sim_'.$k); my $ar = $csv->getline_all($fh); $sheet->write_col(0,0,$ar); close $fh; print scalar (@$ar). " lines read into sheet sim_$k\n"; } $workbook->close; print "$xlsfile created\n";
        poj