How can I use Perl, how can I create charts using values โ€‹โ€‹in a CSV file?

I am new to this and need a hint on how to do this. I have a csv file with the following example data:

site,type,2009-01-01,2009-01-02,....
X,A,12,10,...
X,B,10,23,...
Y,A,20,33,...
Y,B,3,12,...

and so on....

      

I want to create a perl script to read data from a csv file (as per user given) and generate XY (scatter) charts. Let's say I want to create a chart for date 2009-01-01 and type B. The user must enter something like "2009-01-01 B" and the chart should be created with values โ€‹โ€‹from a CSV file.

Can anyone suggest me some code to start with?

+2


source to share


4 answers


I need to do some scatter plots and have been playing around with the module suggested in other answers. In my opinion, the data obtained with GD :: Graph :: Cartesian is too large and the module does not provide any methods to manipulate this parameter, so I hacked my copy Cartesian.pm

(search iconsize

if you want to do the same).



use strict;
use warnings;
use Text::CSV;
use GD::Graph::Cartesian;

# Parse CSV file and convert the data for the
# requested $type and $date into a list of [X,Y] pairs.
my ($csv_file, $type, $date) = @ARGV;
my @xy_points;
my %i = ( X => -1, Y => -1 );
open(my $csv_fh, '<', $csv_file) or die $!;
my $parser = Text::CSV->new();
$parser->column_names( $parser->getline($csv_fh) );
while ( defined( my $hr = $parser->getline_hr($csv_fh) ) ){
    next unless $hr->{type} eq $type;
    my $xy = $hr->{site};
    $xy_points[++ $i{$xy}][$xy eq 'X' ? 0 : 1] = $hr->{$date};
}

# Make a graph.
my $graph = GD::Graph::Cartesian->new(
    width   => 400, # Image size (in pixels, not X-Y coordinates).
    height  => 400,
    borderx => 20,  # Margins (also pixels).
    bordery => 20,
    strings => [[ 20, 50, 'Graph title' ]],
    lines => [
        [ 0,0, 50,0 ], # Draw an X axis.
        [ 0,0,  0,50], # Draw a Y axis.
    ],
    points => \@xy_points, # The data.
);
open(my $png_file, '>', 'some_data.png') or die $!;
binmode $png_file;
print $png_file $graph->draw;

      

+1


source


Don't start with code. Start with CPAN.



CSV and Scatter

+6


source


Here you go, some code to get started:

#!/usr/bin/perl -w
use strict;

use Text::CSV;
use GD;
use Getopt::Long;

      

Instead of GD, you can of course use whatever module you want.

+3


source


OK, for entertainment purposes only:

#!/usr/bin/perl

use strict;
use warnings;

use DBI;
use List::AllUtils qw( each_array );

my $dbh = DBI->connect("DBI:CSV:f_dir=.", undef, undef, {
        RaiseError => 1, AutoCommit => 1,
    }
);

my $sth = $dbh->prepare(qq{
    SELECT d20090101 FROM test.csv WHERE type = ? and site = ?
});

$sth->execute('B', 'X');
my @x = map { $_->[0] } @{ $sth->fetchall_arrayref };

$sth->execute('B', 'Y');
my @y = map { $_->[0] } @{ $sth->fetchall_arrayref };

my @xy;

my $ea = each_array(@x, @y);
while ( my @vals = $ea->() ) {
    push @xy, \@vals;
}

my @canvas;
push @canvas, [ '|', (' ') x 40 ] for 1 .. 40;
push @canvas, [ '+', ('-') x 40 ];

for my $coord ( @xy ) {
    warn "coords=@$coord\n";
    my ($x, $y) = @$coord;
    $canvas[40 - $y]->[$x + 1] = '*';
}

print join "\n", map { join '', @$_ } @canvas;

      

Adding axes and improving on ScatterPlot - a really frustrating module - is left as an exercise for the reader.

Please note that I always need to cheat when it comes to SQL. I would appreciate a correct JOIN

one that eliminates the need for @x

, @y

and each_array

.

Output:

|
|
|
| *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| *
|
|
+ ----------------------------------------
+2


source







All Articles