How can I rearrange my data to the coordinates (x, y) for GD :: Graph?

I am writing a program that takes an input file from a user. There are a bunch of numbers in the file, and I will read the numbers in the file and create a graph based on these numbers using GD :: Graph .

The first line of the file is the X axis, the second line of the file is the Y values ​​corresponding to the X axis, and the third, fourth, ... etc. For example:

1 2 3 4 5 
2 4 5 10 14
5 6 8 12 13

So, in the above example, the first line is the x axis, the second is the y values ​​corresponding to xaxis, so this will lead to 10 points. (1, 2) (1, 5) (2, 4) (2, 6) .... (4.10) (4.12) (5.14) (5.13)

I plan to read each line of the array and then split the line into spaces or tabs and store the values ​​in the array. That way, array 1 will have the x axis, array2 will have the y axis, but how should I store lines 3, 4, 5, ... etc. in the array so that they become (x, y)?

Also, how can I find the highest value for the first and second rows (2 arrays), so I can create a limit for the X and Y axes?

+3
source share
3 answers

Kills, reads the question incorrectly, do you want either AoAoH or AoH, depending on whether each line after the first represents the line or all the points that should be built accordingly. Here is how I could write this if every line in the file was to become a line on the chart:

#!/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";
}

, , :

#!/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";
0

, GD:: Graph:: Data.

, / , - List::Util min() max().


, " - X, - Y, , ,... .. - X".

+2

You can grow arrays of x and y when 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;
}
+1
source

Source: https://habr.com/ru/post/1705872/


All Articles