#!/usr/bin/perl 

use CGI ':standard';
use DBI;
use GD;

use GD::Graph::lines;
use Image::Magick;

my @data;

my $i;
my $j=0;
my $c;

for ($c=0;$c<=23;$c++){

    for ($i=0;$i<100;$i+=5){
	if ($i==0){$data[0][$j]=$c.":00";}
	if ($i>0){$data[0][$j]=$c.".".$i;}
    $j++
    }
    if ($c==23){$data[0][$j]=24;}
}

($sec,$min,$hour,$mday,$monn,$yearn,$wday,$yday,$isdst) = localtime(time());
my $dbh= DBI->connect("DBI:mysql:termo","sersql","sersql");


$maxt_p=0;
$mint_p=900;

my $sth= $dbh->prepare("select * from  sensor3 order by id DESC limit 480");
$sth->execute();
$j=$hour*20+int($min/3);
$zag=0;$old_id=480;

while (@ar=$sth->fetchrow_array){
    if ($old_id>=@ar[3]){$old_id=@ar[3];}
    else {last;}
    $data[1][@ar[3]]=@ar[2];
    if ($zag==0) {$time_zam=@ar[1];$zag=1;$term_zag=@ar[2];$term_zag=~s/(.?\d+\.\d\d)(.*)/$1/i;}
    if (@ar[2]>$maxt_p){$maxt_p=@ar[2];}
    if (@ar[2]<$mint_p){$mint_p=@ar[2];}
}


$maxt=int($maxt_p)+2;
$mint=int($mint_p)-2;

$y_tick_number=int((abs(($maxt-$mint)/4)));
$mint_p=~ s/(.\d+\.\d\d)(.*)/$1/i;
$maxt_p=~ s/(.\d+\.\d\d)(.*)/$1/i;


my $mygraph = GD::Graph::lines->new(900, 300);
$mygraph->set(
    x_label     => "Время                        &#169; Ser kandalaksha.info",
    y_label     => "Относительная влажность %",
    title       => $time_zam."  Относительная влажность: ".$term_zag ." %",
    bar_width   => 1,
    # Sepearte the bars with 4 pixels
    bar_spacing => 1,
    # Показывать сетку
    long_ticks  => 1,
    y_max_value => 100,
    y_min_value => 0,
   y_tick_number => 10,
    box_axis => 0,
    x_label_skip=>20,
#    zero_axis_only => 1,
    # Показывать значения над столбиками
    show_values => 0,
    # Делаем линии сплошной (solid), пунктирной (dashed), и штрих-точкой (dotted-dashed)
    line_types  => [1],
    # Задаем толщину линий
    line_width  => 4,
    # Задаем цвета
    dclrs       => ['red'],
    transparent => 1,
    legendclr     => 'red',   # Text for the legend


    # bgclr         => 'white',   # background colour
    #fgclr         => 'dblue',   # Axes and grid
#    boxclr        => 'red',     # Fill colour for box axes, default: not used
#    accentclr     => 'red',    # bar, area and pie outlines.

#    labelclr      => 'red',   # labels on axes
#    axislabelclr  => 'red',   # values on axes
#    legendclr     => 'red',   # Text for the legend
    textclr       => '#3300cc',   # All text, apart from the following 2
#
#    valuesclr     => 'red',   # values printed above the points
# data set colours
#    dclrs => [qw(lred lgreen lblue lyellow lpurple cyan lorange)],




#     x_label_position => 1,
) or warn $mygraph->error;


#$mygraph->set_legend_font(GD::gdMediumBoldFont,25);
$mygraph->set_legend_font('/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 9);
$mygraph->set_title_font('/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 18);
$mygraph->set_legend('Относительная влажность              '."Максимальная: ".$maxt_p." %   Минимальная: ".$mint_p." %");
$mygraph->set_y_label_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 9);
$mygraph->set_x_label_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf',9 );
$mygraph->set_y_axis_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 9);
#$mygraph->set_y_label_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 9);
$mygraph->set_x_axis_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 8);
$mygraph->set_values_font('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 10);

my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
open(GRAPH,">/var/www/termo/Humidity.png");
print GRAPH $myimage->png;
close GRAPH;

$im = GD::Image->new("/usr/lib/cgi-bin/88x31.png");
$red = $im->colorAllocate(255,0,0);
$white = $im->colorClosest(0,255,0);
$im->transparent($white);
$im->stringFT($red, '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf', 14, 0,10, 20,"$term_zag %");
open(GRAPH,">/usr/lib/cgi-bin/88x31_v.png");
print GRAPH $im->png;
close GRAPH;

