Fix barchart division by 0 error
This commit is contained in:
parent
c99531decb
commit
29aec8d72f
|
@ -37,7 +37,9 @@ class BarChart
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$roundedMax = (int) ceil($max / 5) * 5;
|
$roundedMax = $max === 0
|
||||||
|
? 5
|
||||||
|
: (int) ceil($max / 5) * 5;
|
||||||
|
|
||||||
return view('components/barchart', [
|
return view('components/barchart', [
|
||||||
'groups' => self::calculateChartGroups(
|
'groups' => self::calculateChartGroups(
|
||||||
|
@ -74,7 +76,7 @@ class BarChart
|
||||||
$group['bars'][] = [
|
$group['bars'][] = [
|
||||||
'value' => $value,
|
'value' => $value,
|
||||||
'title' => $group['label'] . "\n" . $rowName . ': ' . $value,
|
'title' => $group['label'] . "\n" . $rowName . ': ' . $value,
|
||||||
'height' => ($value / $max * 100) . '%',
|
'height' => $max === 0 ? '0%' : ($value / $max * 100) . '%',
|
||||||
'bg' => $colors[$rowKey],
|
'bg' => $colors[$rowKey],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -97,7 +99,7 @@ class BarChart
|
||||||
for ($y = 0; $y <= $max; $y += $step) {
|
for ($y = 0; $y <= $max; $y += $step) {
|
||||||
$yLabels[] = [
|
$yLabels[] = [
|
||||||
'label' => $y,
|
'label' => $y,
|
||||||
'bottom' => ($y / $max * 100) . '%',
|
'bottom' => $max === 0 ? '0%' : ($y / $max * 100) . '%',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +133,7 @@ class BarChart
|
||||||
*/
|
*/
|
||||||
public static function generateChartDemoData(int $days): array
|
public static function generateChartDemoData(int $days): array
|
||||||
{
|
{
|
||||||
$step = floor(10000 / $days + 1);
|
$step = $days === 0 ? 0 : floor(10000 / $days + 1);
|
||||||
$now = CarbonImmutable::now();
|
$now = CarbonImmutable::now();
|
||||||
$twoWeeksAgo = $now->subDays($days);
|
$twoWeeksAgo = $now->subDays($days);
|
||||||
$current = $twoWeeksAgo;
|
$current = $twoWeeksAgo;
|
||||||
|
|
|
@ -8,6 +8,7 @@ use Carbon\CarbonImmutable;
|
||||||
use Engelsystem\Helpers\BarChart;
|
use Engelsystem\Helpers\BarChart;
|
||||||
use Engelsystem\Renderer\Renderer;
|
use Engelsystem\Renderer\Renderer;
|
||||||
use Engelsystem\Test\Unit\TestCase;
|
use Engelsystem\Test\Unit\TestCase;
|
||||||
|
use Generator;
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
|
|
||||||
class BarChartTest extends TestCase
|
class BarChartTest extends TestCase
|
||||||
|
@ -40,40 +41,9 @@ class BarChartTest extends TestCase
|
||||||
$this->mockTranslator();
|
$this->mockTranslator();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function provideRenderTestData(): Generator
|
||||||
* @covers \Engelsystem\Helpers\BarChart::render
|
|
||||||
* @covers \Engelsystem\Helpers\BarChart::calculateChartGroups
|
|
||||||
* @covers \Engelsystem\Helpers\BarChart::calculateYLabels
|
|
||||||
* @covers \Engelsystem\Helpers\BarChart::generateChartDemoData
|
|
||||||
*/
|
|
||||||
public function testRender(): void
|
|
||||||
{
|
{
|
||||||
$this->rendererMock->expects(self::once())
|
$yLabels = [
|
||||||
->method('render')
|
|
||||||
->with('components/barchart', [
|
|
||||||
'groups' => [
|
|
||||||
[
|
|
||||||
'bars' => [
|
|
||||||
[
|
|
||||||
'value' => 1,
|
|
||||||
'title' => "2022-07-11\na label: 1",
|
|
||||||
'height' => '20%',
|
|
||||||
'bg' => '#000',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'value' => 2,
|
|
||||||
'title' => "2022-07-11\nb label: 2",
|
|
||||||
'height' => '40%',
|
|
||||||
'bg' => '#fff',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'label' => '2022-07-11',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'colors' => self::COLORS,
|
|
||||||
'rowLabels' => self::ROW_LABELS,
|
|
||||||
'barChartClass' => '',
|
|
||||||
'yLabels' => [
|
|
||||||
[
|
[
|
||||||
'label' => '0',
|
'label' => '0',
|
||||||
'bottom' => '0%',
|
'bottom' => '0%',
|
||||||
|
@ -98,12 +68,64 @@ class BarChartTest extends TestCase
|
||||||
'label' => '5',
|
'label' => '5',
|
||||||
'bottom' => '100%',
|
'bottom' => '100%',
|
||||||
],
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
yield 'empty data' => [
|
||||||
|
[],
|
||||||
|
[
|
||||||
|
'groups' => [],
|
||||||
|
'colors' => self::COLORS,
|
||||||
|
'rowLabels' => self::ROW_LABELS,
|
||||||
|
'barChartClass' => '',
|
||||||
|
'yLabels' => $yLabels,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
yield 'non-empty data' => [
|
||||||
|
self::DATA,
|
||||||
|
[
|
||||||
|
'groups' => [
|
||||||
|
[
|
||||||
|
'bars' => [
|
||||||
|
[
|
||||||
|
'value' => 1,
|
||||||
|
'title' => "2022-07-11\na label: 1",
|
||||||
|
'height' => '20%',
|
||||||
|
'bg' => '#000',
|
||||||
],
|
],
|
||||||
])
|
[
|
||||||
|
'value' => 2,
|
||||||
|
'title' => "2022-07-11\nb label: 2",
|
||||||
|
'height' => '40%',
|
||||||
|
'bg' => '#fff',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'label' => '2022-07-11',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'colors' => self::COLORS,
|
||||||
|
'rowLabels' => self::ROW_LABELS,
|
||||||
|
'barChartClass' => '',
|
||||||
|
'yLabels' => $yLabels,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideRenderTestData
|
||||||
|
* @covers \Engelsystem\Helpers\BarChart::render
|
||||||
|
* @covers \Engelsystem\Helpers\BarChart::calculateChartGroups
|
||||||
|
* @covers \Engelsystem\Helpers\BarChart::calculateYLabels
|
||||||
|
* @covers \Engelsystem\Helpers\BarChart::generateChartDemoData
|
||||||
|
*/
|
||||||
|
public function testRender(array $testData, array $expected): void
|
||||||
|
{
|
||||||
|
$this->rendererMock->expects(self::once())
|
||||||
|
->method('render')
|
||||||
|
->with('components/barchart', $expected)
|
||||||
->willReturn('test bar chart');
|
->willReturn('test bar chart');
|
||||||
self::assertSame(
|
self::assertSame(
|
||||||
'test bar chart',
|
'test bar chart',
|
||||||
BarChart::render(self::ROW_LABELS, self::COLORS, self::DATA)
|
BarChart::render(self::ROW_LABELS, self::COLORS, $testData)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue