X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=ND%2FWeb%2FPages%2FGraph.pm;h=599051a53cf40b42f1e5b9dc5f559918ea062829;hb=256609e2df07f5dd40a33ace6163d2b72a269246;hp=fc0ba8e90227452b65d21568df81b974af3d7304;hpb=9298f8363ced9fbe9cba01386629281150b96945;p=ndwebbie.git diff --git a/ND/Web/Pages/Graph.pm b/ND/Web/Pages/Graph.pm index fc0ba8e..599051a 100644 --- a/ND/Web/Pages/Graph.pm +++ b/ND/Web/Pages/Graph.pm @@ -19,30 +19,24 @@ 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}; +our @ISA = 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; @@ -52,22 +46,10 @@ sub render { die "Not a proper type" unless defined $type; my %graph_settings = ( - line_width => 1, 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; @@ -75,56 +57,36 @@ sub render { $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;