1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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 #**************************************************************************/
20 package ND::Web::Pages::Graph;
22 use warnings FATAL => 'all';
23 use CGI qw/:standard/;
27 $ND::PAGES{graph} = {parse => \&parse, process => \&process, render=> \&render};
43 if ($uri =~ m{^/\w+/(stats|ranks)/(.*)}){
45 if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))(?: |:(\d+))?$}){
52 die "Not a proper type" unless defined $type;
54 my %graph_settings = (
56 y_number_format => sub { prettyValue abs $_[0]},
57 legend_placement => 'BL',
65 axislabelclr => 'black',
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;
80 if (defined $findGraph and my $graph = $findGraph->fetchrow_hashref){
81 $img = $graph->{graph};
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;
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};
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];
118 $graph->set(%graph_settings);
119 my $gd = $graph->plot([\@ticks,\@score,\@size,\@value,\@xp]) or die $graph->error;
123 die 'no image' unless defined $img;
125 print header(-type=> 'image/png', -Content_Length => length $img);