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

Wise People,

We have a number of perl engines which calculate statistics. The statistics end up in plain text output files.

Example:
Bob 1 0 3 2 4 5
John 2 3 6 1 0 2
etc

These stats are then built into Adobe Illustrator graphs.
So I was wondering if any of you wise people had any experience of building these graphs directly from perl.

It would be a great enhancement to our operations if we could do this.

Many thanks
Orthanc

Replies are listed 'Best First'.
Re: Perl - Adobe Illustrator
by clemburg (Curate) on Feb 16, 2001 at 17:26 UTC

    This is a small script that takes CSV data as input and displays it as PNG.

    #!/usr/bin/perl use strict; use Getopt::Std; use GD::Graph::linespoints; use Text::ParseWords; # -------------------------------------------------- # setup # -------------------------------------------------- my %opts = (); getopts('d:c:k', \%opts); my $config_file = ($opts{'c'} || "csv2png.conf"); my $delimiter = ($opts{'d'} || ';'); my $keep = ($opts{'k'} || 0); my $input_file = $ARGV[0]; my ($output_file) = ($input_file =~ /(.*)\./); die usage() unless ($input_file && $output_file); # sets globals %graph, %config and @legends use vars qw(%graph %config @legends); # can't use do() here because of PerlApp limitations :-( { open(CF, "<$config_file") or die "Error: no open for $config_file: + $!"; local $/ = undef; my $configuration = <CF>; eval "$configuration"; close(CF); } # -------------------------------------------------- # action # -------------------------------------------------- my @data = read_data_from_csv($input_file); my $my_graph = new GD::Graph::linespoints($graph{'width'}, $graph{'hei +ght'}); $my_graph->set(%config); $my_graph->set_legend(@legends); $my_graph->plot(\@data); save_chart($my_graph, $output_file); # -------------------------------------------------- # subs # -------------------------------------------------- sub read_data_from_csv { my ($fh) = @_; my @d = (); open(FH, "<$fh") || die "Error: no open for $fh: $!"; while (<FH>) { chomp; my @row = parse_line($delimiter, $keep, $_); for (my $i = 0; $i <= $#row; $i++) { undef $row[$i] if ($row[$i] eq 'undef'); push @{$d[$i]}, $row[$i]; } } close (FH); return @d; } sub usage { print << "EOU"; Usage: $0 [-d][-k][-c] input-filename $0: reads a CSV file and creates a PNG line graphic from it Options: c: configuration file name (default "csv2png.conf") d: delimiter for CSV file (Perl regular expression, default ";") k: keep quotes around data (default off) EOU } sub save_chart { my ($chart, $name) = @_; local(*OUT); my $ext = $chart->export_format; open(OUT, ">$name.$ext") or die "Cannot open $name.$ext for write: $!"; binmode OUT; print OUT $chart->gd->$ext(); close OUT; } 1;

    Sample configuration file:

    # must create the following variables: # %config (graph options, see perldoc GD::Graph) # @legends (graph methods, see perldoc GD::Graph) %graph = ( width => 600, height => 400, ); %config = ( width => 600, height => 400, x_label => 'X-Axis', y_label => 'Y-Axis', title => 'Chart', y_min_value => '0', y_max_value => '5000', y_tick_number => '10', y_label_skip => '2', transparent => 0, bgclr => 'white', ); @legends = ( 'Page Views', 'Visits', );

    Sample data:

    27/04;678;443 28/04;1476;503 29/04;373;98 30/04;312;129 01/05;1136;331 02/05;1250;378

    Hope this helps.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

      Wow :) You really should post this in Code or CUFP, with a nice, descriptive title -- it exceeds the bounds of the original question.

        Thanks for the suggestion - done. See csv2png.pl.

        Christian Lemburg
        Brainbench MVP for Perl
        http://www.brainbench.com

Re: Perl - Adobe Illustrator
by Beatnik (Parson) on Feb 16, 2001 at 16:40 UTC
    The GD::Graph modules provide a nice and clean interface to statistics.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Perl - Adobe Illustrator
by OeufMayo (Curate) on Feb 16, 2001 at 16:52 UTC

    Actually, I worked on a project involving heavy use of GD and Image::Magick (and eventually some DBI and XML for good measure) to produce high-quality graphs. /msg me if you want more info.

    You also have to be aware that Gd::Graph, as far as I know, can not create beziers curves, which are very easily done using Adobe Illustrator.

    <kbd>--
    PerlMonger::Paris(http => 'paris.pm.org');</kbd>

      Hi

      I hope there hasn't been too much confusion with regard to my question.

      Everyone seems to be pointing in the direction of producing a graph with GD::Graph but that isn't exactly what I'm after.
      What I really need is a method or an example of producing a graph in the 'Illustrator EPS' format which can then be used by Quark

      Hope this helps but thanks for all previous offers
      Orthanc

        It's relatively "trivial" to convert from, e.g. PNG to EPS. I'll assume, since you're using Quark, that the concern is more about output resolution than vector vs. raster; Image::Magick, among others, can convert a high-resolution PNG into EPS very nicely. We've used this for printing to filmsetters with very good results (at 1.4 Kdpi). Just be certain to create the PNG with the proper resolution. (Also, I seem to recall that when using GD to create PNG's, the image density ("dpi resolution") was not set correctly, so we had to add a step in the Image::Magick portion to "force" the image density to the correct value.)

        If you're looking for vector-only EPS output, there are some PostScript::* modules in CPAN, but the first one that caught my eye (PostScript::Elements) seems woefully undocumented. I'm sure that there is one out there, just search around a bit :-)

Re: Perl - Adobe Illustrator
by davorg (Chancellor) on Feb 16, 2001 at 16:41 UTC

    A quick search for 'Graph' at CPAN reveals a number of graphing modules. GD::Graph is probably the best.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

(Coyote) Re: Perl - Adobe Illustrator
by Coyote (Deacon) on Feb 17, 2001 at 00:15 UTC
    Since GD::Graph has already been mentioned in this thread already and it appears that you would like vector output to use in Illustrator rather than bitmap output, I though I might mention some alternatives. If you have access to gnuplot you can use the Chart::Graph module. I believe you can produce EPS output from gnuplot. What platform are you using? I've use perl and the Mac::Glue module under MacOS 9 to script Adobe Illustrator (adding captions, titles and headers to graphs for publication). Perhaps you could do something similar Windows using OLE?

    ----
    Coyote