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

I have a question about making an Excel graph in perl. I looked at the module SpreadSheet::WriteExcel and it does nearly everything I want (very good!). However, it would be nice if I could automate the making of some graphs/charts on some data. The doc for the above mentioned module says that graphs won't be coming soon (the reason is because it would be too hard/too many user interface problems). However, I only have a *specific* need, so I'd like to know:

  1. Is there a general purpose module out there that I missed on CPAN?
  2. Is it feasible for me to try to make my own solution

Where would I go to find out about making my own solution, what would I need to know?

My specific purpose from before is just to make several charts/graphs based on some data I have in a file. If I need to be more specific I can (I refrain here because it would take up more space than it may be worth)

Replies are listed 'Best First'.
Re: Excel graphing in perl
by jsprat (Curate) on Jul 11, 2002 at 23:12 UTC
    For information, you can try perldoc Win32::OLE and Using Win32::OLE and Excel - Tips and Tricks (an excellent node, BTW).

    Here's a small example that creates a new workbook, inserts some data and creates a chart on sheet1. Once you get the hang of using OLE, record a macro then generalize, like so (the recorded VBA macro is after __END__):

    #!perl use strict; use warnings; use Win32::OLE; use Win32::OLE::Const "Microsoft Excel"; my $xls = Win32::OLE->new('Excel.Application'); $xls->Workbooks->Add(); $xls->{Visible} = -1; $xls->Sheets("Sheet1")->Range('A1')->{Value} = 'Left'; $xls->Sheets("Sheet1")->Range('B1')->{Value} = 'Right'; for (2 .. 5) { $xls->Sheets("Sheet1")->Range("A$_")->{Value} = $_; $xls->Sheets("Sheet1")->Range("B$_")->{Value} = $_ * 2; } $xls->Charts->Add(); $xls->ActiveChart->SetSourceData({ Source =>$xls->Sheets('Sheet1')->Range('A1:B5'), PlotBy =>xlColumns, }); $xls->ActiveChart->Location({ Where => xlLocationAsObject, Name =>'She +et1'}); __END__ Here is the macro I used Sub Macro1() Charts.Add ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:B5"), + PlotBy:= _ xlRows ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1" With ActiveChart .HasTitle = False .Axes(xlCategory, xlPrimary).HasTitle = False .Axes(xlValue, xlPrimary).HasTitle = False End With End Sub

    Good luck...

Re: Excel graphing in perl
by thraxil (Prior) on Jul 11, 2002 at 20:30 UTC

    if it just has to be a graph and doesn't necessarily have to be an Excel graph, or be embedded in an Excel spreadsheet, GD::Graph could probably do what you need.

    otherwise, you could probably hack something together with one or another of the various Win32 and OLE modules but i don't do windows stuff so i really wouldn't know quite where to start.

    anders pearson

      Thanks. I forgot to mention that actually.

      First, It needs to be Excel for various reaasons, second, I don't know too much windows stuff (in relation to Win32::OLE), but saw in the WriteExcel docs that this may be the path to go. So if anyone knows a solution via this method, I'd be interested (an explanation would be appreciated that keeps in mind I haven't messed with Win32::OLE, and I've only done a little windows programming -- API, not MFC, though I'm familiar with the latter ... now)

Re: Excel graphing in perl
by Gerard (Pilgrim) on Jul 11, 2002 at 22:57 UTC
    I may well be barking up the wrong tree, but would it be possible to make the graph in excel manually and then replace the data as needed using SpreadSheet::WriteExcel??
    I have done something similar to this in the past to get round the limitations of the module, although, it is a fantastic module.

    Regards,

    Gerard O'Brien
      would it be possible to make the graph in excel manually

      Quite possible. I've already done it :) I'm trying to make a perl script to do it now so that it can be done in the future with new data sets, and with much LESS TIME. There are quite a few graphs needed, and making them is very simple ... however time consuming.

      and then replace the data as needed using SpreadSheet::WriteExcel

      Once the graphs are done, the program can stop. I don't need any more advance/further handling (by Excel or perl, anyway) of the data at that point.