Skip to content

Commit

Permalink
Graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
malamalca committed Feb 4, 2024
1 parent 8e02ae0 commit c4884dc
Show file tree
Hide file tree
Showing 15 changed files with 719 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/Calc/Hrup/Elementi/Konstrukcija.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class Konstrukcija

public string $id;
public string $naziv;
public ?string $tip;
public string $tip = 'zahtevna';
public string $racunskiPasovi = 'tercni';

public float $povrsinskaMasa = 0;
public float $gostota = 0;
Expand Down Expand Up @@ -107,6 +108,8 @@ protected function parseConfig($config)
}
}
}

$this->debelina = $this->gostota / $this->povrsinskaMasa;
}

/**
Expand Down Expand Up @@ -269,14 +272,14 @@ public function analiza()
$sumTau += pow(10, (-self::SPQ_C[$ix] - $this->R[$fq]) / 10);
}

$this->C = $this->Rw - round((-10 * log10($sumTau)), 0);
$this->C = -($this->Rw - round((-10 * log10($sumTau)), 0));

$sumTau = 0;
foreach (self::FQS as $ix => $fq) {
$sumTau += pow(10, (-self::SPQ_CTR[$ix] - $this->R[$fq]) / 10);
}

$this->Ctr = $this->Rw - round((-10 * log10($sumTau)), 0);
$this->Ctr = -($this->Rw - round((-10 * log10($sumTau)), 0));
}

/**
Expand All @@ -287,6 +290,7 @@ public function analiza()
public function export()
{
$konstrukcija = new \stdClass();
$konstrukcija->tip = 'zahtevna';

$reflect = new \ReflectionClass(self::class);
$props = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC);
Expand Down
32 changes: 32 additions & 0 deletions src/Calc/Hrup/KonstrukcijaFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace App\Calc\Hrup;

use App\Calc\Hrup\Elementi\EnostavnaKonstrukcija;
use App\Calc\Hrup\Elementi\Konstrukcija;

class KonstrukcijaFactory
{
/**
* Ustvari konstrukcijo glede na podani tip
*
* @param \stdClass|null $options Dodatne nastavitve
* @return \App\Calc\Hrup\Elementi\EnostavnaKonstrukcija|\App\Calc\Hrup\Elementi\Konstrukcija|null
*/
public static function create($options)
{
if (empty($options->tip)) {
return new EnostavnaKonstrukcija($options);
}

switch ($options->tip) {
case 'zahtevna':
return new Konstrukcija($options);
case 'enostavna':
return new EnostavnaKonstrukcija($options);
default:
throw new \Exception('Vrsta stavbe ne obstaja.');
}
}
}
4 changes: 2 additions & 2 deletions src/Command/Hrup/IzracunElementov.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace App\Command\Hrup;

use App\Calc\Hrup\Elementi\EnostavnaKonstrukcija;
use App\Calc\Hrup\Elementi\OknaVrata;
use App\Calc\Hrup\KonstrukcijaFactory;
use App\Core\App;
use App\Core\Command;

Expand All @@ -28,7 +28,7 @@ public function run($projectId = null)

$konstrukcijeOut = [];
foreach ($konstrukcijeIn as $konstrukcijaConfig) {
$konstrukcija = new EnostavnaKonstrukcija($konstrukcijaConfig);
$konstrukcija = KonstrukcijaFactory::create($konstrukcijaConfig);
$konstrukcija->analiza();
$konstrukcijeOut[] = $konstrukcija->export();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Controller/Pures/TSSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function prezracevanje($projectId, $sistemId)
App::set('projectId', $projectId);

$sistemi = App::loadProjectCalculation('Pures', $projectId, 'TSS' . DS . 'prezracevanje');
App::set('sistemi', $sistemi);
App::set('sistem', array_first($sistemi, fn($sistem) => strtolower($sistem->id) == strtolower($sistemId)));
}

Expand All @@ -34,6 +35,7 @@ public function razsvetljava($projectId, $sistemId)
App::set('projectId', $projectId);

$sistemi = App::loadProjectCalculation('Pures', $projectId, 'TSS' . DS . 'razsvetljava');
App::set('sistemi', $sistemi);
App::set('sistem', array_first($sistemi, fn($sistem) => strtolower($sistem->id) == strtolower($sistemId)));
}

Expand All @@ -49,6 +51,7 @@ public function ogrevanje($projectId, $sistemId)
App::set('projectId', $projectId);

$sistemi = App::loadProjectCalculation('Pures', $projectId, 'TSS' . DS . 'ogrevanje');
App::set('sistemi', $sistemi);
App::set('sistem', array_first($sistemi, fn($sistem) => strtolower($sistem->id) == strtolower($sistemId)));
}

Expand All @@ -64,6 +67,7 @@ public function fotovoltaika($projectId, $sistemId)
App::set('projectId', $projectId);

$sistemi = App::loadProjectCalculation('Pures', $projectId, 'TSS' . DS . 'fotovoltaika');
App::set('sistemi', $sistemi);
App::set('sistem', array_first($sistemi, fn($sistem) => strtolower($sistem->id) == strtolower($sistemId)));
}
}
8 changes: 8 additions & 0 deletions src/Lib/Calc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ class Calc
public const MESECI = ['jan', 'feb', 'mar', 'apr', 'maj', 'jun', 'jul', 'avg', 'sep', 'okt', 'nov', 'dec'];
public const NIX = 0.000001;

public const FREKVENCE_TERCE = [
50, 63, 80,
100, 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500,
3150, 4000, 5000,
];

public const RF = [null, null, null, 33, 36, 39, 42, 45, 48, 51, 52, 53, 54, 55, 56, 56, 56, 56, null, null, null];

/**
* Izračun nasičenega tlaka glede na podano temperaturo
*
Expand Down
241 changes: 240 additions & 1 deletion src/Lib/CalcKonstrukcije.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ public static function graf($data)
$lineWidth = 1;
$data1lineWidth = 4;
$data2lineWidth = 2;
$barWidth = 20;

// Font settings
$font = RESOURCES . 'OpenSans-Regular.ttf';
Expand Down Expand Up @@ -804,4 +803,244 @@ public static function graf($data)

return $imageData;
}

/**
* Klasičen line-graf
*
* @param array<string, mixed> $data Podatki za graf
* @return string|false
*/
public static function lineGraph($data)
{
// Image dimensions
$imageWidth = 800;
$imageHeight = 600;

// Grid dimensions and placement within image
$gridTop = 10;
$gridLeft = 50;
$gridBottom = $imageHeight - 40;
$gridRight = $imageWidth - 50;
$gridHeight = $gridBottom - $gridTop;
$gridWidth = $gridRight - $gridLeft;

// Bar and line width
$lineWidth = 1;
$data1lineWidth = 4;
$data2lineWidth = 2;

// Font settings
$font = RESOURCES . 'OpenSans-Regular.ttf';
$fontSize = 12;

// Margin between label and axis
$labelMargin = 8;

// Margin between axis and graph
$offsetMargin = 20;

// Max value on y-axis
$maxSeriesValues = [];
$minSeriesValues = [];

foreach ($data['series'] as $k => $serie) {
$maxSeriesValues[$k] = max($serie);
$minSeriesValues[$k] = min($serie);
}
$yMaxValue = max($maxSeriesValues);
$yMinValue = min($minSeriesValues);

$yMaxAxis = $yMaxValue + abs(0.05 * ($yMaxValue - $yMinValue));
$yMinAxis = $yMinValue - abs(0.05 * ($yMaxValue - $yMinValue));

// Distance between grid lines on y-axis
// $yGridStep = 10;
// Number of lines we want in a grid
$yGridLinesCount = 4;
$gridSteps = [1, 2, 5];
$yGridStepReal = ($yMaxValue - $yMinValue) / $yGridLinesCount;

$yGridFactor = 1;
while ($yGridStepReal < 10) {
$yGridStepReal *= 10;
$yGridFactor *= 0.1;
}
while ($yGridStepReal > 100) {
$yGridStepReal /= 10;
$yGridFactor *= 10;
}

$yGridStepReal = ((int)$yGridStepReal) / 10;
$yGridStep = null;
$yGridMinDifference = 10;
foreach ($gridSteps as $gridStep) {
if (abs($gridStep - $yGridStepReal) < $yGridMinDifference) {
$yGridMinDifference = abs($gridStep - $yGridStepReal);
$yGridStep = $gridStep;
}
}

$yGridStep = $yGridStep * $yGridFactor * 10;

$yGridLines = [];
$yGridStepDiff = ($yMaxValue - $yMinValue) / $yGridLinesCount;

// First horizontal grid line
$firstGridLinePos = floor($yMinAxis / $yGridStep) * $yGridStep;
if ($firstGridLinePos > $gridBottom) {
$yGridLines[] = $firstGridLinePos;
} else {
$yGridLines[] = $firstGridLinePos + $yGridStep;
}

$yGridLinesCount = floor(($yMaxAxis - $firstGridLinePos) / $yGridStep);

for ($i = 0; $i < $yGridLinesCount; $i++) {
$yGridLines[] = $yGridLines[count($yGridLines) - 1] + $yGridStep;
}

// Init image
$chart = imagecreatetruecolor($imageWidth, $imageHeight);
if (!$chart) {
return false;
}

// Setup colors
$backgroundColor = imagecolorallocate($chart, 255, 255, 255);
$axisColor = imagecolorallocate($chart, 85, 85, 85);
$labelColor = $axisColor;
$gridColor = imagecolorallocate($chart, 212, 212, 212);
$barColor = imagecolorallocatealpha($chart, 127, 201, 255, 50);
$separatorLineColor = imagecolorallocate($chart, 80, 80, 80);
$dataLineColor = imagecolorallocate($chart, 255, 0, 0);
$data2LineColor = imagecolorallocate($chart, 64, 64, 255);

if (
$backgroundColor === false ||
$axisColor === false ||
$labelColor === false ||
$gridColor === false ||
$barColor === false ||
$separatorLineColor === false ||
$dataLineColor == false ||
$data2LineColor == false
) {
return false;
}

imagefill($chart, 0, 0, $backgroundColor);
imagesetthickness($chart, $lineWidth);

/*
* Print grid lines bottom up
*/
//for ($i = 0; $i <= $yMaxValue; $i += $yLabelSpan) {
foreach ($yGridLines as $yGridLineValue) {
//$y = $gridBottom - $i * $gridHeight / $yMaxAxis;

$y = $gridBottom - ($yGridLineValue - $yMinAxis) / ($yMaxAxis - $yMinAxis) * $gridHeight;

// draw the line
imageline($chart, $gridLeft, (int)$y, $gridRight, (int)$y, $gridColor);

// draw right aligned label
$labelBox = imagettfbbox($fontSize, 0, $font, strval($yGridLineValue));
if ($labelBox) {
$labelWidth = $labelBox[4] - $labelBox[0];

$labelX = $gridLeft - $labelWidth - $labelMargin;
$labelY = $y + $fontSize / 2;

imagettftext(
$chart,
$fontSize,
0,
(int)$labelX,
(int)$labelY,
$labelColor,
$font,
strval($yGridLineValue)
);
}
}

/*
* Draw x- and y-axis
*/
imageline($chart, $gridLeft, $gridTop, $gridLeft, $gridBottom, $axisColor);
imageline($chart, $gridLeft, $gridBottom, $gridRight, $gridBottom, $axisColor);

/** Draw the data series line */
$dataX = [];

$sirinaGrafa = $gridWidth - 2 * $offsetMargin;
$offsetX = $gridLeft + $offsetMargin;
foreach ($data['X'] as $dataValue) {
$dataX[] = $offsetX + $dataValue / max($data['X']) * $sirinaGrafa;
}

$prevLabelX = $gridLeft;
foreach ($dataX as $k => $value) {
if ($k > 0) {
// draw series line
foreach ($data['series'] as $j => $serie) {
$x1 = $dataX[$k - 1];
$y1 = $gridBottom - ($serie[$k - 1] - $yMinAxis) / ($yMaxAxis - $yMinAxis) * $gridHeight;
$x2 = $value;
$y2 = $gridBottom - ($serie[$k] - $yMinAxis) / ($yMaxAxis - $yMinAxis) * $gridHeight;

imagesetthickness($chart, $data1lineWidth);
imageline($chart, (int)$x1, (int)$y1, (int)$x2, (int)$y2, $dataLineColor);
}

/* Grid Line */
$x1 = $value;
$y1 = $gridBottom - $gridHeight;
$x2 = $value;
$y2 = $gridBottom - 1;

imagefilledrectangle(
$chart,
(int)$x1,
(int)$y1,
(int)$x2,
(int)$y2,
$gridColor
);

// draw right aligned label
$labelBox = imagettfbbox($fontSize, 0, $font, strval(round($data['X'][$k], 0)));
if ($labelBox) {
$labelWidth = $labelBox[4] - $labelBox[0];

$labelX = $value - $labelWidth / 2;
$labelY = $gridBottom + $fontSize + $labelMargin;

// prevent overlap
if ($labelX > $prevLabelX) {
imagettftext(
$chart,
$fontSize,
0,
(int)$labelX,
(int)$labelY,
$labelColor,
$font,
strval(round($data['X'][$k], 0))
);
$prevLabelX = $labelX + $labelWidth;
}
}
}
}

ob_start();
imagepng($chart);
$imageData = ob_get_contents();
ob_end_clean();

imagedestroy($chart);

return $imageData;
}
}
Loading

0 comments on commit c4884dc

Please sign in to comment.