How can I rearrange my data to be (x,y) coordinates for GD::Graph?
Question
I am writing a program that takes in an input file from the user. The file has bunch of numbers in it and I will read the numbers in the file and create a plot based on those numbers using GD::Graph.
The first line of the file is X axis, second line of the file is Y values corresponding to X axis and third, fourth, ..., etc For example:
1 2 3 4 5
2 4 5 10 14
5 6 8 12 13
So in the above, first line is x-axis, second is y values corresponding to xaxis so this will fetch 10 points. (1, 2) (1, 5) (2, 4) (2, 6)....(4,10) (4,12) (5,14) (5, 13)
I plan on reading each line of the array and then splitting the line on spaces or tabs and storing the values in an array. So, array 1 will have x-axis, array2 will have y-axis, but how should I store 3rd, 4th, 5th, ..., etc lines in an array so they become (x,y)?
Furthermore, how can I find the largest value for the first and second lines (2 arrays) so I can create a limit for my X and Y axes?
Solution
Whoops, misread the question, you want either an AoAoH or an AoH, depending on whether each line after the first represents a line or the are all just points to be plotted respectively. Here is how I would write it if each line in the file was to become a line in the graph:
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw/min max/;
my @x_points = split " ", scalar <>; #read in the x axis labels
my ($x_min, $x_max) = (sort { $a <=> $b } @x_points)[0,-1];
my ($y_min, $y_max) = (0, 0);
#lines is an AoAoH, first layer are the lines to be drawn
#second layer is a list of coords
#third layer are the x and y coords
my @lines;
while (<>) {
my @y_points = split;
#if the two arrays are not the same size, we have a problem
die "invalid file\n" unless @y_points == @x_points;
$y_min = max($y_min, @y_points);
$y_max = min($y_max, @y_points);
push @lines, [
map { { x => $x_points[$_], y => $y_points[$_] } }
0 .. $#x_points
];
}
use Data::Dumper;
print "x min and max $x_min $x_max\n",
"y min and max $y_min $y_max\n",
"data:\n",
Dumper(\@lines);
my $i;
for my $line (@lines) {
$i++;
print "line $i is made up of points: ",
(map { "($_->{x}, $_->{y}) " } @$line), "\n";
}
And here is how I would handle it if they are just points to be ploted:
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw/min max/;
my @x_points = split " ", scalar <>; #read in the x axis labels
my ($x_min, $x_max) = (sort { $a <=> $b } @x_points)[0,-1];
my ($y_min, $y_max) = (0, 0);
#lines is an AoAoH, first layer are the lines to be drawn
#second layer is a list of coords
#third layer are the x and y coords
my @points;
while (<>) {
my @y_points = split;
#if the two arrays are not the same size, we have a problem
die "invalid file\n" unless @y_points == @x_points;
$y_min = max($y_min, @y_points);
$y_max = min($y_max, @y_points);
push @points,
map { { x => $x_points[$_], y => $y_points[$_] } }
0 .. $#x_points;
}
use Data::Dumper;
print "x min and max $x_min $x_max\n",
"y min and max $y_min $y_max\n",
"data:\n",
Dumper(\@points);
print "Here are the points: ",
(map { "($_->{x}, $_->{y}) " } @points), "\n";
OTHER TIPS
Not really an answer to your question, but don't miss GD::Graph::Data.
Unless you are sure that the first and last on each line is smallest/biggest you'll need to use something like List::Util's min()
and max()
.
I don't really understand what you mean by "The first line of the file is X axis, second line of the file is Y axis and third, fourth, ..., etc are corresponding points to X axis."
You can grow your x and y arrays as you go.
#!/usr/bin/perl
use Data::Dumper;
use warnings;
use strict;
my @xs = ();
my @ys = ();
my $expecting_xs = 1;
my $last_xs_count;
while(<>) {
chomp;
my @values = split(/\s+/);
if($expecting_xs) {
push(@xs, @values);
$last_xs_count = @values;
$expecting_xs = 0;
} else {
if(@values != $last_xs_count) {
die "Count mismatch";
}
push(@ys, @values);
$expecting_xs = 1;
}
}
if(!$expecting_xs) {
die("Odd number of lines");
}
my($xmin, $xmax) = extremes(@xs);
my($ymin, $ymax) = extremes(@ys);
print "xmin: $xmin xmax: $xmax ymin: $ymin ymax: $ymax\n";
print Dumper(\@xs), Dumper(\@ys);
sub extremes {
my(@values) = @_;
return undef unless @values;
my $min = shift(@values);
my $max = $min;
for my $value (@values) {
$max = $value if $value > $max;
$min = $value if $value < $min;
}
return $min, $max;
}