From 15cc9ce813d7dfffb772f111947bf38c51bcfcd2 Mon Sep 17 00:00:00 2001 From: agp8x Date: Fri, 7 Feb 2014 16:47:08 +0100 Subject: [PATCH] added basic support for climaDiagrams --- data/index.html | 7 +- draw.php | 31 ++++-- lib/climaDiagram.php | 254 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 281 insertions(+), 11 deletions(-) create mode 100644 lib/climaDiagram.php diff --git a/data/index.html b/data/index.html index 8a366cc..a4b2bb9 100644 --- a/data/index.html +++ b/data/index.html @@ -1,7 +1,6 @@ get-modifiers:
-force : redraw chart
-totalforce : rewrite database-entry +redraw chart
--lib (+force) : use custom svg-lib
--short : linked (png only)
--abs : absolut values instead of rounded
--mode=last + num=[x] : dispolay last x hours +-lib[=old] (+force) : use custom svg-lib
+-short : linked
+-mode=last + num=[x] : display last x hours diff --git a/draw.php b/draw.php index e01c766..4cdf899 100644 --- a/draw.php +++ b/draw.php @@ -232,8 +232,31 @@ function generateChart($today,$mode,$hours=0,$dateInput){ #if(date("d.m.Y")==$date || !$file_exists || isset($_GET['force']) || $forceRedraw){ #if((date("d.m.Y")==$date &&$force) || !file_exists($chartname) || $force){ #if(true){#always redraw + if(date("I",$selectionStart)==1 ){ + //summertime + //if($mode!=3){ + $selectionStart+=3600; + //} + } if($svg){ - include('ownlib.php'); + if ($_GET['lib']=="old") { + include('ownlib.php'); + } else { + include 'lib/climaDiagram.php'; + $values=prepareData($datas[1],$div,$selectionStart,$selectionEnd,$chartdistance); + unset($values[0]); + // echo ""; + $diagram=new ClimaDiagram(); + $diagram->setLocation('Ort','Land','100','50`, 50`' ,'north'); + // $temp=array(25,30,40,-10,5,6.75,50,20,-5,-15.9,-12,-8); + // $rain=array(1,5,17,48,100,155,425,320,280,76,8,4); + $diagram->setMode('day'); + $diagram->setRainData(array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)); + $diagram->setTemperatureData($values); + file_put_contents("charts/".$chartname.".svg", $diagram->getGraph()); + } }else{ chdir('pchart'); include("class/pData.class.php"); @@ -241,12 +264,6 @@ function generateChart($today,$mode,$hours=0,$dateInput){ include("class/pImage.class.php"); $myData=new pData(); - if(date("I",$selectionStart)==1 ){ - //summertime - //if($mode!=3){ - $selectionStart+=3600; - //} - } $labels=getLabels($datas[0],$selectionStart,$selectionEnd,$chartdistance); if($mode==3){ $selectionStart+=60*60*24; diff --git a/lib/climaDiagram.php b/lib/climaDiagram.php new file mode 100644 index 0000000..5a786ab --- /dev/null +++ b/lib/climaDiagram.php @@ -0,0 +1,254 @@ +array(1, 600),'month'=>array(2.6, 1337),'day'=>array(2, 1050)); + private $mode='year'; + private $languages=array('de'); + private $language='de'; + + private $temperatureData; + private $rainData; + private $locationName; + private $locationCountry; + private $locationHeight; + private $locationCoordinates; + private $hemisphere='north'; + private $result; + + private function drawBase(){ + foreach ($this->modes as $mode => $prop) { + if ($this->mode==$mode) { + $xOffset=$prop[0]; + $width=$prop[1]; + } + } + $base=' + '.$this->locationName.','.$this->locationCountry.' ('.$this->locationHeight.' m) + '.$this->locationCoordinates.' + + T in C + N in mm'."\n"; + $horizontalLines=""; + $rainIndex=400; // Text Höchster Niederschlag + $rainMarker=0; + $temperatureMarker=240; + $temperatureIndex=50; // Text Höchste Temperatur + for ($i=90+30; $i < 740; $i+=30) { + $horizontalLines.=''."\n"; + if ($i>=$temperatureMarker) { + $horizontalLines.=''.$temperatureIndex.''."\n"; + $temperatureMarker=$i+60; + $temperatureIndex-=10; + } + if ($i>=$rainMarker && $rainIndex>=0) { + $horizontalLines.=''.$rainIndex.''."\n"; + if ($i<180) { + $rainMarker=$i+30; + $rainIndex-=100; + } elseif ($i<240) { + $rainMarker=$i+30; + $rainIndex-=50; + }else { + $rainMarker=$i+60; + $rainIndex-=20; + } + } + } + $base.=$horizontalLines; + $horizontalLines=null; + if ($this->mode=='year'){ + $base.=$this->yearDiagram()."\n"; + }elseif ($this->mode=='day') { + $base.=$this->dayDiagram()."\n"; + }else{ + $base.='TODO: modes'."\n"; + } + $this->result=$base; + $base=null; + } + + private function yearDiagram(){ + $verticalLines=""; + foreach ($this->months as $index => $month) { + $verticalLines.=''."\n"; + $verticalLines.=''.$month.''."\n"; + } + $verticalLines.=$this->drawRain(); + $verticalLines.=$this->drawTemperature(); + return $verticalLines; + } + + private function dayDiagram(){ + $verticalLines=""; + for($i=1;$i<=24;$i++) { + $verticalLines.=''."\n"; + $verticalLines.=''.$i.''."\n"; + } + $verticalLines.=$this->drawRain(); + $verticalLines.=$this->drawTemperature(); + return $verticalLines; + } + + private function drawTemperature(){ + $zero=540; + $max=240; + $difference=50; + $resolution=($zero-$max)/$difference; + $startX=40; + $path=''; + $lastX=0; + $lastY=0; + $i=0; + foreach ($this->temperatureData as $index=>$value) { + $x=20+$startX*($i+1); + $y=$zero+$resolution*-$value; + if (empty($path)) { + $path="M ".($x-20).' '.$y.' L '.$x.' '.$y.' '; + }else{ + $path.=$x.' '.$y.' '; + } + $lastX=$x; + $lastY=$y; + $i++; + } + $path.=($lastX+20).' '.$lastY; + return ''; + } + + private function drawRain(){ + $rain=""; + $startX=40; + $startY=540; + foreach ($this->rainData as $index => $value) { + if ($value==0) { + continue; + } + if($value>200){ + $localvalue=$value-200; + $value=200; + $zero=180; + $max=90; + $difference=300; + $resolution=($max-$zero)/$difference; + $height=($localvalue*$resolution); + $rain.=''."\n"; + }if ($value>100) { + $localvalue=$value-100; + $value=100; + $zero=240; + $max=180; + $difference=100; + $resolution=($max-$zero)/$difference; + $height=($localvalue*$resolution); + $rain.=''."\n"; + } + if($value<=100){ + $zero=540; + $max=240; + $difference=100; + $resolution=($max-$zero)/$difference; + $height=($value*$resolution); + $rain.=''."\n"; + } + } + return $rain; + } + + public function getGraph(){ + foreach ($this->modes as $mode => $prop) { + if ($this->mode==$mode) { + $width=$prop[1]; + } + } + $this->drawBase(); + return ''."\n". + ''."\n". + ''."\n". + ''."\n". + $this->result."\n". + ''; + } + + public function setLocation($city,$country,$height,$coordinates,$hemisphere='north'){ + $this->locationName=$city; + $this->locationCountry=$country; + $this->locationHeight=$height; + $this->locationCoordinates=$coordinates; + $this->hemisphere=$hemisphere; + } + public function setTemperatureData($data){ + if (is_array($data) && !empty($data)) { + $this->temperatureData=$data; + } + } + public function setRainData($data){ + if (is_array($data) && !empty($data)) { + $this->rainData=$data; + } + } + public function setMode($mode){ + if (array_key_exists($mode, $this->modes)) { + $this->mode=$mode; + return true; + echo "TRUE"; + } + return false; + } + +} + +################ +## TESTING ## +################ + +// $test=new ClimaDiagram(); +// $test->setLocation('Ort','Land','100','50`, 50`' ,'north'); +// // $temp=array(25,30,40,-10,5,6.75,50,20,-5,-15.9,-12,-8); +// // $rain=array(1,5,17,48,100,155,425,320,280,76,8,4); +// $temp=array(25,30,40,-10,5,6.75,50,20,-5,-15.9,-12,-8,25,30,40,-10,5,6.75,50,20,-5,-15.9,-12,-8); +// $rain=array(1,5,17,48,100,155,425,320,280,76,8,4,1,5,17,48,100,155,425,320,280,76,8,4); +// $test->setMode('day'); +// // $test->setMode('month'); +// $test->setRainData($rain); +// $test->setTemperatureData($temp); +// echo $test->getGraph(); +?> \ No newline at end of file