]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Graph.pm
updated lib dir
[ndwebbie.git] / NDWeb / Graph.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 package NDWeb::Graph;
21 use strict;
22 use warnings;
23 use ND::Include;
24 use GD::Graph::lines;
25 require Exporter;
26
27 our @ISA = qw/Exporter/;
28
29 our @EXPORT = qw/graphFromQuery/;
30
31 sub graphFromQuery {
32         my ($x,$y,$settings,$query,) = @_;
33
34         my %graph_settings = (
35                 line_width => 1,
36                 y_number_format => sub { prettyValue abs $_[0]},
37                 legend_placement => 'BL',
38                 #zero_axis => 1,
39                 box_axis => 0,
40                 boxclr => 'black',
41                 axislabelclr => 'black',
42         );
43
44         my $fields = $query->{NUM_OF_FIELDS};
45         my @fields;
46         for (my $i = 0; $i < $fields; $i++){
47                 push @fields,[];
48         }
49         while (my @result = $query->fetchrow){
50                 for (my $i = 0; $i < $fields; $i++){
51                         push @{$fields[$i]},$result[$i];
52                 }
53
54         }
55         $graph_settings{x_label_skip} = int(1+(scalar @{$fields[0]}) / 6);
56
57         my $graph = GD::Graph::lines->new($x,$y);
58         $graph->set_legend(@{$query->{NAME}}[1..$fields]) or die $graph->error;
59
60         for my $key (keys %{$settings}){
61                 $graph_settings{$key} = $settings->{$key};
62         }
63
64         $graph->set(%graph_settings);
65         my $gd = $graph->plot(\@fields) or die $graph->error;
66         return $gd->png;
67 }
68
69 1;