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


In reply to Re: Perl - Adobe Illustrator by clemburg
in thread Perl - Adobe Illustrator by orthanc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.