Skip to content

Commit eca6966

Browse files
committedAug 12, 2018
Nullable height
1 parent 3007f46 commit eca6966

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed
 

‎examples/realData.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
use noximo\PHPColoredAsciiLinechart\Colorizers\AsciiColorizer;
5+
use noximo\PHPColoredAsciiLinechart\Linechart;
6+
use noximo\PHPColoredAsciiLinechart\Settings;
7+
8+
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
9+
$settings = new Settings();
10+
$settings->setFPS(40);
11+
$settings->setHeight(null);
12+
$lineGraph = new Linechart();
13+
$lineGraph->setSettings($settings);
14+
15+
try {
16+
$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,];
17+
18+
$lineGraph->addMarkers($line, [AsciiColorizer::GREEN], [AsciiColorizer::RED]);
19+
20+
$lineGraph->chart()->clearScreen()->print()->wait();
21+
$lineGraph->clearAllMarkers();
22+
} catch (Exception $e) {
23+
echo $e->getMessage();
24+
}

‎examples/stretched.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
99
$settings = new Settings();
1010
$settings->setFPS(40);
11-
$settings->setHeight(9);
11+
$settings->setHeight(null);
1212

1313
$lineGraph = new Linechart();
1414
$lineGraph->setSettings($settings);

‎src/Linechart.php

+73-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class Linechart
7575
* @var IColorizer
7676
*/
7777
private $colorizer;
78+
/**
79+
* @var float|null
80+
*/
81+
private $adjuster;
7882

7983
/**
8084
* @param int $x alias x coordinate
@@ -181,6 +185,7 @@ public function chart(): Chart
181185
$this->prepareData();
182186

183187
foreach ($this->allmarkers as $markersData) {
188+
$markersData['markers'] = $this->adjustMarkerValues($markersData['markers']);
184189
$this->currentColors = $this->currentColors ?? $markersData['colors'];
185190
$result = $this->prepareResult();
186191

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

244+
$this->adjuster = $this->findAdjuster($min, $max);
245+
$max = $this->adjust($max);
246+
$min = $this->adjust($min);
247+
239248
$this->range = max(1, abs($max - $min));
240249

241-
$height = $this->getSettings()->getHeight() ?? $this->range;
250+
$height = (int) ($this->getSettings()->getHeight() ?? $this->range);
242251
$this->ratio = $height / $this->range;
243252

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

289+
/**
290+
* @param float $min
291+
* @param float $max
292+
*
293+
* @return float|null
294+
*/
295+
private function findAdjuster(float $min, float $max): ?float
296+
{
297+
$adjuster = null;
298+
$realMin = $max - $min;
299+
300+
if ($realMin < 1 && $realMin > 0) {
301+
$adjuster = 1 / $realMin;
302+
}
303+
304+
return $adjuster;
305+
}
306+
307+
/**
308+
* @param float $number
309+
*
310+
* @return float
311+
*/
312+
private function adjust(float $number): float
313+
{
314+
if ($this->adjuster !== null) {
315+
$number *= $this->adjuster;
316+
}
317+
318+
return $number;
319+
}
320+
321+
/**
322+
* @param $markers
323+
*
324+
* @return array
325+
*/
326+
private function adjustMarkerValues($markers): array
327+
{
328+
if ($this->adjuster === null) {
329+
return $markers;
330+
}
331+
332+
return array_map(function ($value) {
333+
return $value * $this->adjuster;
334+
}, $markers);
335+
}
336+
280337
/**
281338
* @return array
282339
*/
@@ -307,6 +364,7 @@ private function processBorder(array $result, array $markersData): array
307364

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

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

383+
/**
384+
* @param float $number
385+
*
386+
* @return float
387+
*/
388+
private function deadjust(float $number): float
389+
{
390+
if ($this->adjuster !== null) {
391+
$number /= $this->adjuster;
392+
}
393+
394+
return $number;
395+
}
396+
325397
/**
326398
* @param array $markers
327399
* @param int $x

‎src/Settings.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function getHeight(): ?int
9292
*
9393
* @return Settings
9494
*/
95-
public function setHeight(int $height): Settings
95+
public function setHeight(?int $height): Settings
9696
{
9797
$this->height = $height;
9898

0 commit comments

Comments
 (0)
Please sign in to comment.