Come faccio a riorganizzare i miei dati siano (x, y) coordinate per GD :: Graph?
Domanda
Sto scrivendo un programma che prende in un file di input da parte dell'utente. Il file ha po 'di numeri in esso e mi leggerà i numeri nel file e creare una trama basata su quei numeri che utilizzano GD :: Graph .
La prima riga del file è all'asse X, seconda riga del file è valori Y corrispondenti all'asse X e la terza, la quarta, ..., ecc Ad esempio:
1 2 3 4 5
2 4 5 10 14
5 6 8 12 13
Così in quanto sopra, la prima linea è xaxis, secondo è valori y corrispondenti a XAxis quindi questo scaricherà 10 punti. (1, 2) (1, 5) (2, 4) (2, 6) .... (4,10) (4,12) (5,14) (5, 13)
Ho intenzione di lettura di ogni riga della matrice e quindi dividere la linea in spazi o schede e memorizzare i valori di un array. Così, 1 campo avrà asse x, array2 avrà asse y, ma come devo conservare 3 °, 4 °, 5 °, ..., ecc linee in un array in modo che diventino (x, y)?
Inoltre, come posso trovare il valore più grande per la prima e la seconda linea (2 campi) in modo posso creare un limite per la mia assi X e Y?
Soluzione
Ops, frainteso la questione, si vuole sia un AoAoH o un AOH, a seconda che ogni riga dopo il primo rappresenta una linea o siete tutti solo punti da tracciare, rispettivamente. Ecco come vorrei scrivere che se ogni riga nel file era quello di diventare una linea nel grafico:
#!/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";
}
E qui è come vorrei gestirlo se sono solo punti da 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";
Altri suggerimenti
Non proprio una risposta alla tua domanda, ma non perdetevi GD: :. Grafico :: Data
A meno che non si è che che la prima e l'ultima su ogni linea è più piccolo / grande avrete bisogno di usare qualcosa come List :: Util min()
s ' e max()
.
Io non capisco cosa si intende per "La prima riga del file è l'asse X, seconda riga del file è asse Y e terza, quarta, ..., ecc sono corrispondenti punti all'asse X".
È possibile far crescere la tua X e Y array, come si va.
#!/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;
}