1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| #!/usr/bin/perl
use strict;
use utf8;
use warnings;
use Tk;
use Tk::LabFrame;
use Tk::Chart::Bars;
use Tk::Chart::Mixed;
use Tk::Pane;
use Cwd;
use File::Basename;
use Tk::BrowseEntry;
my $dir_get_open_file = getcwd;
my $dim_data_ligne = 2; # Nombre de données par ligne dans le fichier
my $main = MainWindow->new( -title => "Graphe_data --> $dir_get_open_file" );
$main->minsize( 700, 500 );
my $menu_bar = $main->Menu( -type => "menubar", );
$main->configure( -menu => $menu_bar, );
# menu Fichier avec sous menu
my $menu_fichier = $menu_bar->cascade( -label => 'Fichier', -tearoff => 0, );
$menu_fichier->command( -label => 'Sélectionner un fichier', -command => \&lecture_fichier, );
my %param_graph
= (); # Hash contenant les paramètres récupérés en début de fichier, pour la construction du graphique
sub lecture_fichier {
my $fichier_SQL = $main->getOpenFile( -initialdir => $dir_get_open_file, );
if ($fichier_SQL) {
if ( -e $fichier_SQL ) {
#$fichier_SQL =~ s/.*\/([^\/]*\.txt)/$1/; # On extrait uniquement le nom du fichier sans le chemin
print "\nTraitement du fichier : " . basename($fichier_SQL) . "\n";
open( FILE, '<', $fichier_SQL );
while (<FILE>) {
if ( /<Param>/ ... /<\/Param>/ ) {
my $L = $_;
if ( $L =~ /(X_Label)=(.*)/ ) { $param_graph{$1} = $2; }
if ( $L =~ /(Y_Label)=(.*)/ ) { $param_graph{$1} = $2; }
if ( $L =~ /(Legend)=\[(.*)\]/ ) { $param_graph{$1} = $2; }
if ( $L =~ /(Title)=(.*)/ ) { $param_graph{$1} = $2; }
}
}
close(FILE);
const_graphique( $fichier_SQL, \%param_graph );
}
}
}
MainLoop;
sub const_graphique {
my ( $fichier_SQL, $ref_params ) = @_;
my %params = %$ref_params; # On déréférence pour récupérer le contenu de la légende
my @legends = split ',', $params{'Legend'};
my $X_label = $params{'X_Label'};
my $Y_label = $params{'Y_Label'};
my $title = $params{'Title'};
my $mwg = MainWindow->new(
-title => $fichier_SQL,
-background => 'white',
);
my $croplist = $mwg->BrowseEntry(
-label => 'Choisir portion : ',
-background => 'white',
)->pack();
my @types = ( 'bars', 'bars' );
my $chart = $mwg->Mixed(
-title => $title,
-titleposition => 'left',
-xlabel => $X_label,
-ylabel => $Y_label,
-overwrite => 1,
-typemixed => \@types,
-background => 'snow',
-longticks => 1, # Affiche un quadrillage gris
)->pack(qw / -expand 1 -fill both /);
$chart->enabled_gradientcolor();
# Add a legend to the graph
$chart->set_legend(
-title => 'Title legend',
-data => \@legends,
-titlecolors => 'blue',
);
# Tableaux pour la récupération des données depuis le fichier texte
my @data_fichier = ();
my @data = ();
$mwg->configure( -title => $fichier_SQL );
# Transformation du fichier en tableau de tableaux
# Chaque ligne est stockée dans un tableau @tab, puis on ajoute la référence de @tab à @data_fichier
my $cpt_lignes = 0; # Compteur de lignes de données
open( DATA, '<', $fichier_SQL );
while ( my $L = <DATA> ) {
if ( $L =~ /^<data[0-9]+>(.*)<\/data[0-9]+>/ )
{ # Les données sont entre 2 balises, séparées par des '|'
my @tab = split( '\|', $1 ); # On transforme la liste en tableau
push @data_fichier, \@tab; # On ajoute la référence du tableau au tableau de tableaux
$cpt_lignes++; # On incrémente le compteur de lignes
}
}
close(DATA);
print $cpt_lignes. " lignes de donnees\n";
# Réorganisation des données dans le tableau @data pour utilisation du module Tk::Chart
for ( my $k = 0; $k < $dim_data_ligne; $k++ ) {
my @tab_ligne = ();
# On extrait la k-ieme donnee de chaque petit tableau
foreach my $ref (@data_fichier) { # On parcourt les ref de tous les tableaux trouvés
my @tab = @$ref; # On déréférence
push @tab_ligne, $tab[$k]; # On stocke la k-ieme valeur de chaque tableau dans le tableau ligne
}
push @data, \@tab_ligne;
}
# Add help identification
$chart->set_balloon();
# Call this method to avoid resizing.
$chart->enabled_automatic_redraw;
my $nombre_valeurs = scalar @{ $data[0] };
my $interval = "0-$nombre_valeurs";
# Portion de 100
my @choices;
my $debut = 0;
my $portion = 50;
for ( my $i = $debut; $i <= $nombre_valeurs; $i += $portion ) {
next if ( $i == 0 );
push @choices, "$debut-$i";
$debut = $i;
}
if ( $debut < $nombre_valeurs ) {
push @choices, "$debut-$nombre_valeurs";
}
$croplist->configure(
-choices => \@choices,
-variable => \$interval,
-browsecmd => sub {
my ( $debut, $fin ) = split /-/, $interval;
my ( @new_data, @new_x, @new_value ) = ();
# Regénération du graph
for ( $debut .. $fin - 1 ) {
push @new_x, $data[0][$_];
push @new_value, $data[1][$_];
}
push @new_data, \@new_x;
push @new_data, \@new_value;
$chart->plot( \@new_data );
$chart->redraw;
}
);
# Create the graph
$chart->plot( \@data );
} |
Partager