Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
PerlMonks - Line Graphs from a CSV file

by clemburg (Curate)
on Feb 16, 2001 at 23:14 UTC ( #58943=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info clemburg
Description: This is a small script that takes CSV data as input and generates a PNG file displaying a line graph as output. Basically, each column in the CSV file is a data series that will be displayed as a line graph. The first series labels the X axis.


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";

# --------------------------------------------------
# action
# --------------------------------------------------

my @data =  read_data_from_csv($input_file);

my $my_graph = new GD::Graph::linespoints($graph{'width'}, $graph{'hei


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>) {
        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
    c: configuration file name (default "csv2png.conf")
    d: delimiter for CSV file (Perl regular expression, default ";")
    k: keep quotes around data (default off)


sub save_chart
    my ($chart, $name) = @_; 
    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;


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',

Sample data:

Replies are listed 'Best First'.
Re: - Line Graphs from a CSV file
by a (Friar) on Feb 17, 2001 at 10:21 UTC
    You know, I have this data file of pages scanned per day and I was thinking it'd be nice to show the bosses a little line graph ... thanks.

    Update: Yup, just what the bosses wanted, works like a charm. GD::Graph is so easy! However getting GD::etc installed was a little messy as my libgd was older (and shared) and I didn't remove it first on upgrading. That then broke mrtg which was using the old shared lib ... <sigh> something about 'every solution brings w/ it a new set of problems' ... least the bosses are happy about the stats. And recompling rateup was relatively painless.


Re: - Line Graphs from a CSV file
by slojuggler (Beadle) on Mar 21, 2002 at 21:42 UTC
    I figured out the goof!

    I initially cut and pasted the code from the page. So, I ended up deleting the two red +s that dot the code as a result of long lines going past the page limit.

    I'm posting this to help out other monks in the event they obtain the weird error that I got the first time.

    Neat program..thanks!

      By following the stated steps, I was able to create a gif file but unable to see any graph lines. The Windows Picture Viewer claimed "No preview available". Why is that so? What did I do wrong?

        Not sure. Works for me:

        Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\strawberry\perl>perl -v This is perl, v5.10.0 built for MSWin32-x86-multi-thread Copyright 1987-2007, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at, the Perl Home Pa +ge.

        viz. csv2png.gif

        Note that the output is actually a .gif (not a .png); maybe try a different graphics viewer?


      Hi, I'm new Perl user, i need help to generate graph for 3 axes. input(x-axis) output1(y-axis) output2(z-axis) The input file is .csv Kindly provide me the input. Thanks, sgupta
Re: - Line Graphs from a CSV file
by wazoox (Prior) on Feb 23, 2009 at 18:01 UTC
    nice, but it creates gif files for me :)
Re: - Line Graphs from a CSV file
by slojuggler (Beadle) on Mar 08, 2002 at 17:06 UTC
    Hi, when I try running this on Activestate Perl 5.6.1, build 631 using the input file as csvin.txt, I get the following:

    C:\scripts> csvin.txt gd-png: fatal libpng error: No IDATs written into file gd-png error: setjmp returns error condition

    Any ideas?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://58943]
[stevieb]: w00t! my i2c module actually works, and it works well!

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-06-22 22:53 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (531 votes). Check out past polls.