package ND::Web::Pages::Graph;
use strict;
-use warnings FATAL => 'all';
+use warnings;
use CGI qw/:standard/;
use ND::Include;
-use GD::Graph::lines;
+use ND::Web::Graph;
-$ND::PAGES{graph} = {parse => \&parse, process => \&process, render=> \&render};
+use base qw/ND::Web::Image/;
-sub parse {
- my ($uri) = @_;
- $ND::USETEMPLATE = 0;
-}
+$ND::Web::Page::PAGES{graph} = 'ND::Web::Pages::Graph';
-sub process {
-
-}
-
-sub render {
- my ($DBH,$uri) = @_;
+sub render_body {
+ my $self = shift;
+ my $DBH = $self->{DBH};
my $type;
my ($x,$y,$z);
- if ($uri =~ m{^/\w+/(stats|ranks)/(.*)}){
+ if ($self->{URI} =~ m{^/\w+/(stats|ranks)/(.*)}){
$type = $1;
- if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))(?: |:(\d+))?$}){
+ if ($2 =~ m{(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?$}){
$x = $1;
$y = $2;
$z = $3;
die "Not a proper type" unless defined $type;
my %graph_settings = (
- line_width => 2,
y_number_format => sub { prettyValue abs $_[0]},
- legend_placement => 'BL',
- #x_label => 'tick',
- y_label => '',
- x_label_skip => 50,
- x_tick_offset => 13,
- zero_axis => 1,
- box_axis => 0,
- boxclr => 'black',
- axislabelclr => 'black',
title => $type,
y1_label => 'size',
y2_label => 'rest',
-
-
);
my $findGraph;
$findGraph = $DBH->prepare(q{SELECT graph FROM planet_graphs WHERE planet = planetid($1,$2,$3,$4) AND tick = $4 AND type = $5});
$findGraph->execute($x,$y,$z,$ND::TICK,$type) or die $DBH->errstr;
}
-
my $img;
if (defined $findGraph and my $graph = $findGraph->fetchrow_hashref){
$img = $graph->{graph};
- }elsif(defined $z){
- 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});
- $planets->execute($x,$y,$z,$ND::TICK) or die $DBH->errstr;
-
- my @score;
- my @size;
- my @value;
- my @xp;
- my @ticks;
-
- while (my $tick = $planets->fetchrow_hashref){
- push @ticks,$tick->{tick};
+ }elsif(defined $x){
+ my $query;
+ if (defined $z){
if ($type eq 'stats'){
- push @score,$tick->{score};
- push @size,$tick->{size};
- push @value,$tick->{value};
- push @xp,$tick->{xp}*60;
+ $query = $DBH->prepare(q{SELECT tick,score,size,value,xp*60 AS "xp*60" FROM planets natural join planet_stats WHERE id = planetid($1,$2,$3,$4) ORDER BY tick ASC});
}elsif($type eq 'ranks'){
- push @score,-$tick->{scorerank};
- push @size,-$tick->{sizerank};
- push @value,-$tick->{valuerank};
- push @xp,-$tick->{xprank};
+ $query = $DBH->prepare(q{SELECT tick,-scorerank AS score,-sizerank AS size,-valuerank AS value,-xprank AS xp FROM planets natural join planet_stats WHERE id = planetid($1,$2,$3,$4) ORDER BY tick ASC});
}
-
- }
- my $graph = GD::Graph::lines->new(500,300);
- if ($type eq 'stats'){
- $graph->set_legend(qw{score size value xp*60}) or die $graph->error;
- $graph_settings{two_axes} = 1;
- $graph_settings{use_axis} = [2,1,2,2];
- }elsif($type eq 'ranks'){
- $graph->set_legend(qw{score size value xp}) or die $graph->error;
- $graph_settings{y_max_value} = 0;
- $graph_settings{two_axes} = 1;
- $graph_settings{use_axis} = [2,1,2,2];
+ $query->execute($x,$y,$z,$ND::TICK) or die $DBH->errstr;
+ }else{
+ if ($type eq 'stats'){
+ $query = $DBH->prepare(q{SELECT tick,score,size,value,xp*60 AS "xp*60" FROM galaxies WHERE x = $1 AND y = $2 ORDER BY tick ASC});
+ }elsif($type eq 'ranks'){
+ $query = $DBH->prepare(q{SELECT tick,-scorerank AS score,-sizerank AS size,-valuerank AS value,-xprank AS xp FROM galaxies WHERE x = $1 AND y = $2 ORDER BY tick ASC});
+ }
+ $query->execute($x,$y) or die $DBH->errstr;
}
- $graph->set(%graph_settings);
- my $gd = $graph->plot([\@ticks,\@score,\@size,\@value,\@xp]) or die $graph->error;
- $img = $gd->png;
+
+ $graph_settings{two_axes} = 1;
+ $graph_settings{use_axis} = [2,1,2,2];
+ $graph_settings{y_max_value} = 0 if $type eq 'ranks';
+ $img = graphFromQuery 500,300,\%graph_settings,$query;
}
die 'no image' unless defined $img;
- print header(-type=> 'image/png', -Content_Length => length $img);
- binmode STDOUT;
- print $img;
-}
+ return $img;
+};
1;