#!/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;
my $k=0;
for ($c=0;$c<3;$c++){

 for($j=0;$j<24;$j+=3){
    for ($i=0;$i<20;$i++){
	if ($i==0){$data[0][$k]=$j.":00";}
	if ($i>0){$data[0][$k]=$j.".".$i;}
	$k++;
	}
    }
}

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


$maxt_p=-100;
$mint_p=1000;

my $sth= $dbh->prepare("SELECT * FROM `sensor2` WHERE date < NOW( ) AND date > ADDDATE( CURDATE( ) , INTERVAL -2 DAY ) ORDER BY `sensor2`.`id` ASC ");
$sth->execute();
$j=0;$sr=0;
$zag=0;
$day_s=0;$prev_id=0;
while (@ar=$sth->fetchrow_array){
    if ($prev_id>@ar[3]){$day_s+=479;}# print "\n$j @ar[1] @ar[0]";}
    $z=$j;$z++;$z=$z*3-$day_s;
   if ($z==3){while (@ar[3]>=$z){$j++;$z=$j;$z++;$z=$z*3-$day_s;}}
    if (@ar[3]>=$z){$data[1][$j]=$t1/$getpok;$getpok=0;
	if ($data[1][$j]>$maxt_p){$maxt_p=$data[1][$j];}
	if ($data[1][$j]<$mint_p){$mint_p=$data[1][$j];}
	$t1=0;
	while (@ar[3]>=$z){$j++;$z=$j;$z++;$z=$z*3-$day_s;}
    }
    if (@ar[3]<$z){$t1+=@ar[2];$getpok++;$prev_id=@ar[3];}
}

$maxt=int($maxt_p)+2;
$mint=int($mint_p)-2;
$y_tick_number=(abs($maxt-$mint));
$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     => "Атмосферное давление  (mm Hg)",
    title       => $time_zam."           Атмосферное давление (mm Hg) среднее за 3 дня (9 минут)\nПозавчера                          Вчера                          Сегодня",
    bar_width   => 1,
    # Sepearte the bars with 4 pixels
    bar_spacing => 1,
    # Показывать сетку
    long_ticks  => 1,
    y_max_value => $maxt,
    y_min_value => $mint,
   y_tick_number => $y_tick_number,
    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       => '#0000FF',   # 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('Атмосферное давление  (mm Hg)              '."Максимальнаое: ".$maxt_p." (mm Hg)   Минимальное: ".$mint_p." (mm Hg)");
$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/pressure_3d.png");
print GRAPH $myimage->png;
close GRAPH;

