in reply to Perl - Adobe Illustrator

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

Replies are listed 'Best First'.
Re: Re: Perl - Adobe Illustrator
by boo_radley (Parson) on Feb 16, 2001 at 20:07 UTC
    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