]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Graph.pm
graphs
[ndwebbie.git] / ND / Web / Pages / 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 ND::Web::Pages::Graph;
21 use strict;
22 use warnings FATAL => 'all';
23 use CGI qw/:standard/;
24 use ND::Include;
25 use GD::Graph::lines;
26
27 $ND::PAGES{graph} = {parse => \&parse, process => \&process, render=> \&render};
28
29 sub parse {
30         my ($uri) = @_;
31         $ND::USETEMPLATE = 0;
32 }
33
34 sub process {
35
36 }
37
38 sub render {
39         my ($DBH,$uri) = @_;
40
41         my $type;
42         my ($x,$y,$z);
43         if ($uri =~ m{^/\w+/(stats|ranks)/(.*)}){
44                 $type = $1;
45                 if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))(?: |:(\d+))?$}){
46                         $x = $1;
47                         $y = $2;
48                         $z = $3;
49                 }
50         }
51
52         die "Not a proper type" unless defined $type;
53
54         my %graph_settings = (
55                 line_width => 2,
56                 y_number_format => sub { prettyValue abs $_[0]},
57                 legend_placement => 'BL',
58                 #x_label => 'tick',
59                 y_label => '',
60                 x_label_skip => 50,
61                 x_tick_offset => 13,
62                 zero_axis => 1,
63                 box_axis => 0,
64                 boxclr => 'black',
65                 axislabelclr => 'black',
66                 title => $type,
67                 y1_label => 'size',
68                 y2_label => 'rest',
69
70
71         );
72
73         my $findGraph;
74         if (defined $z){
75                 $findGraph = $DBH->prepare(q{SELECT graph FROM planet_graphs WHERE planet = planetid($1,$2,$3,$4) AND tick = $4 AND type = $5});
76                 $findGraph->execute($x,$y,$z,$ND::TICK,$type) or die $DBH->errstr;
77         }
78
79         my $img;
80         if (defined $findGraph and my $graph = $findGraph->fetchrow_hashref){
81                 $img = $graph->{graph};
82         }elsif(defined $z){
83                 my $planets = $DBH->prepare(q{SELECT id, tick,size,coords(x,y,z),score,size,value,xp,scorerank,sizerank,valuerank,xprank from planets natural join planet_stats where id = planetid($1,$2,$3,$4) ORDER BY tick ASC});
84                 $planets->execute($x,$y,$z,$ND::TICK) or die $DBH->errstr;
85
86                 my @score;
87                 my @size;
88                 my @value;
89                 my @xp;
90                 my @ticks;
91                 
92                 while (my $tick = $planets->fetchrow_hashref){
93                         push @ticks,$tick->{tick};
94                         if ($type eq 'stats'){
95                                 push @score,$tick->{score};
96                                 push @size,$tick->{size};
97                                 push @value,$tick->{value};
98                                 push @xp,$tick->{xp}*60;
99                         }elsif($type eq 'ranks'){
100                                 push @score,-$tick->{scorerank};
101                                 push @size,-$tick->{sizerank};
102                                 push @value,-$tick->{valuerank};
103                                 push @xp,-$tick->{xprank};
104                         }
105
106                 }
107                 my $graph = GD::Graph::lines->new(500,300);
108                 if ($type eq 'stats'){
109                         $graph->set_legend(qw{score size value xp*60}) or die $graph->error;
110                         $graph_settings{two_axes} = 1;
111                         $graph_settings{use_axis} = [2,1,2,2];
112                 }elsif($type eq 'ranks'){
113                         $graph->set_legend(qw{score size value xp}) or die $graph->error;
114                         $graph_settings{y_max_value} = 0;
115                         $graph_settings{two_axes} = 1;
116                         $graph_settings{use_axis} = [2,1,2,2];
117                 }
118                 $graph->set(%graph_settings);
119                 my $gd = $graph->plot([\@ticks,\@score,\@size,\@value,\@xp]) or die $graph->error;
120                 $img = $gd->png;
121         }
122
123         die 'no image' unless defined $img;
124
125         print header(-type=> 'image/png', -Content_Length => length $img);
126         binmode STDOUT;
127         print $img;
128 }
129
130 1;