Skip to content

Commit

Permalink
Nullable height
Browse files Browse the repository at this point in the history
  • Loading branch information
noximo committed Aug 12, 2018
1 parent 3007f46 commit eca6966
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
24 changes: 24 additions & 0 deletions examples/realData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types = 1);

use noximo\PHPColoredAsciiLinechart\Colorizers\AsciiColorizer;
use noximo\PHPColoredAsciiLinechart\Linechart;
use noximo\PHPColoredAsciiLinechart\Settings;

require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$settings = new Settings();
$settings->setFPS(40);
$settings->setHeight(null);
$lineGraph = new Linechart();
$lineGraph->setSettings($settings);

try {
$line = [0.0208, 0.020858, 0.021, 0.021, 0.0211, 0.0211, 0.0211, 0.0211, 0.0211, 0.021056, 0.0211, 0.0211, 0.0211, 0.0211, 0.0211, 0.0211, 0.0211, 0.0211, 0.021124, 0.0214, 0.0215, 0.021436, 0.02149, 0.021488, 0.02149, 0.02145, 0.02145, 0.021406, 0.02145, 0.02145, 0.02145, 0.0214, 0.02145, 0.021487, 0.02149, 0.021482, 0.02148, 0.02148, 0.0215, 0.0215, 0.0215, 0.0215, 0.021499, 0.021473, 0.021454, 0.021497, 0.021489, 0.021454, 0.021705, 0.02151, 0.021513,];

$lineGraph->addMarkers($line, [AsciiColorizer::GREEN], [AsciiColorizer::RED]);

$lineGraph->chart()->clearScreen()->print()->wait();
$lineGraph->clearAllMarkers();
} catch (Exception $e) {
echo $e->getMessage();
}
2 changes: 1 addition & 1 deletion examples/stretched.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$settings = new Settings();
$settings->setFPS(40);
$settings->setHeight(9);
$settings->setHeight(null);

$lineGraph = new Linechart();
$lineGraph->setSettings($settings);
Expand Down
74 changes: 73 additions & 1 deletion src/Linechart.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class Linechart
* @var IColorizer
*/
private $colorizer;
/**
* @var float|null
*/
private $adjuster;

/**
* @param int $x alias x coordinate
Expand Down Expand Up @@ -181,6 +185,7 @@ public function chart(): Chart
$this->prepareData();

foreach ($this->allmarkers as $markersData) {
$markersData['markers'] = $this->adjustMarkerValues($markersData['markers']);
$this->currentColors = $this->currentColors ?? $markersData['colors'];
$result = $this->prepareResult();

Expand Down Expand Up @@ -236,9 +241,13 @@ private function prepareData(): void
$this->colorizer = $this->getSettings()->getColorizer();
[$min, $max, $width] = $this->findMinMax($this->allmarkers);

$this->adjuster = $this->findAdjuster($min, $max);
$max = $this->adjust($max);
$min = $this->adjust($min);

$this->range = max(1, abs($max - $min));

$height = $this->getSettings()->getHeight() ?? $this->range;
$height = (int) ($this->getSettings()->getHeight() ?? $this->range);
$this->ratio = $height / $this->range;

$this->min2 = $min * $this->ratio;
Expand Down Expand Up @@ -277,6 +286,54 @@ private function findMinMax(array $allmarkers): array
return [$min, $max, $width];
}

/**
* @param float $min
* @param float $max
*
* @return float|null
*/
private function findAdjuster(float $min, float $max): ?float
{
$adjuster = null;
$realMin = $max - $min;

if ($realMin < 1 && $realMin > 0) {
$adjuster = 1 / $realMin;
}

return $adjuster;
}

/**
* @param float $number
*
* @return float
*/
private function adjust(float $number): float
{
if ($this->adjuster !== null) {
$number *= $this->adjuster;
}

return $number;
}

/**
* @param $markers
*
* @return array
*/
private function adjustMarkerValues($markers): array
{
if ($this->adjuster === null) {
return $markers;
}

return array_map(function ($value) {
return $value * $this->adjuster;
}, $markers);
}

/**
* @return array
*/
Expand Down Expand Up @@ -307,6 +364,7 @@ private function processBorder(array $result, array $markersData): array

for (; $y <= $yMax; ++$y) {
$rawLabel = $this->max2 / $this->ratio - ($y - $this->min2) * $this->range / $this->rows;
$rawLabel = $this->deadjust($rawLabel);
$label = $format($rawLabel, $this->getSettings());

$border = '';
Expand All @@ -322,6 +380,20 @@ private function processBorder(array $result, array $markersData): array
return $result;
}

/**
* @param float $number
*
* @return float
*/
private function deadjust(float $number): float
{
if ($this->adjuster !== null) {
$number /= $this->adjuster;
}

return $number;
}

/**
* @param array $markers
* @param int $x
Expand Down
2 changes: 1 addition & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getHeight(): ?int
*
* @return Settings
*/
public function setHeight(int $height): Settings
public function setHeight(?int $height): Settings
{
$this->height = $height;

Expand Down

0 comments on commit eca6966

Please sign in to comment.