10

How do I use Perl to create graphs?

I'm running scheduled job that creates text reports. I'd like to move this to the next step (for the management) and also create some graphs that go along with this. Is this possible / feasible? It'd be great if I could do this using Office some how.

update: solutions i'm going to investigate in this order

  • Spreadsheet::WriteExcel (this seems to now have changed from the last time i investigated this .... wait, this was suggested by the author of the module. cool.)
  • GD Graph - this is now available for ActivePerl(wasn't last time i looked)
  • SVG
  • Open Charts look interesting.
  • Chartdirector

15 Answers 15

7

GD and GD::Graph are probably your best bets, you can use them to create images that you can then embed into whatever you need.

7

All of the methods mentioned above are really good, but personally I like SVG::TT::Graph. I really like the power that SVG gives you to draw really nice-looking graphs.

6

Also you can take a look at Google Charts CPAN module

use Google::Chart;

  my $chart = Google::Chart->new(
    type => "Bar",
    data => [ 1, 2, 3, 4, 5 ]
  );

  print $chart->as_uri, "\n"; # or simply print $chart, "\n"

  $chart->render_to_file( filename => 'filename.png' );
5

At work we have used the excellent Chartdirector.

It's not free, but is very cheap (maybe 50 bucks or so). The cost is well worth it, as the API and docs are both excellent (way better than GD!), so easily saved more than that amount of my time.

There's also a free version, which includes a small yellow banner advertising the product on each chart - to be honest if this is for personal use, you can go for that as it's really not very intrusive at all.

Chartdirector is available for lots of platforms (Win, Linux, Solaris, BSD, OSX) and has an API for lots of languages, too (Perl, ASP, .NET, Java, PHP, Python, Ruby, C++).

The output is easy on the eye, as you can see at their examples page.

4

Sorry for blowing my own trumpet, but you might be interested to have a look some slides I did for a short presentation about Graphing With Perl.

It mentions some of the suggestions here, but also gives you some code snippets that you might be able to use to help you get the most of what you're doing.

3

Depending on the complexity of your graph, simply generating a command file for Gnuplot—or GraphViz/Dotty, depending on what kind of graph you are referring to—might do the trick?

3

The Perl module Spreadsheet::WriteExcel allows you to create Excel workbooks that include charts.

You first have to create the type of chart that you want in Excel and then extract it out using a utility called chartex which is installed with Spreadsheet::WriteExcel.

The chart template can then be added to a new workbook and made to reference new data.

The documentation is here and there are several examples in the charts directory of the distro.

The mechanism is a little inflexible however and the it is sometimes tricky to get the exact result that you want.

3

Haven't tried it yet but Chart::Clicker looks quite nifty.

I think it uses the Cairo graphic library (alternative to GD) but is actually built on top of Graphics::Primitive which is an "interesting" graphics agnostic package.

The author in question (GPHAT) seems to be putting together some integrated tools for producing reports... http://www.onemogin.com/blog/582-pixels-and-painting-my-recent-cpan-releases

On a side note... have used both ChartDirector and OFC and both are good (especially if web based).

3
Spreadsheet::WriteExcel::Chart

You might need something like strawberry or vanilla Perl to get this to compile. Or PPM might have the module.

Tutorial link: http://search.cpan.org/dist/Spreadsheet-WriteExcel/charts/charts.pod

2

It won't work with Office, but I really like Chart::OFC which will create Open Flash Charts. Very slick looking and easy to use.

2

It depends to a great extent what sort of graphs (the look of them), and the data-source. I've had some good result by using the YUI Charts and feeding them some JSON style versions of the original source data. Rolling over a live chart for exact values is quite easy for example. There are plenty of examples on the developer pages.

1

If you're set on doing this in MS Office you can use the Win32::OLE module to control Excel via OLE. Be warned, that this tends to run slowly and it can be difficult to find documentation for Excel's API. On the plus side, it allows you to do pretty much everything that you can do manually.

1

Metaprograming of course! Output an R script that creates the graph.

1

PGPlot does great graphs. There are some examples here. It works fine with Perl 5.8.8 but is broken in 5.10.0

0

Spreadsheet::WriteExcel will let you just get the data into Excel, then write Excel equations for the graphs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.