Fix barchart division by 0 error

This commit is contained in:
Michael Weimann 2022-07-18 19:22:45 +02:00 committed by Igor Scheller
parent c99531decb
commit 29aec8d72f
2 changed files with 66 additions and 42 deletions

View File

@ -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', [
'groups' => self::calculateChartGroups(
@ -74,7 +76,7 @@ class BarChart
$group['bars'][] = [
'value' => $value,
'title' => $group['label'] . "\n" . $rowName . ': ' . $value,
'height' => ($value / $max * 100) . '%',
'height' => $max === 0 ? '0%' : ($value / $max * 100) . '%',
'bg' => $colors[$rowKey],
];
}
@ -97,7 +99,7 @@ class BarChart
for ($y = 0; $y <= $max; $y += $step) {
$yLabels[] = [
'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
{
$step = floor(10000 / $days + 1);
$step = $days === 0 ? 0 : floor(10000 / $days + 1);
$now = CarbonImmutable::now();
$twoWeeksAgo = $now->subDays($days);
$current = $twoWeeksAgo;

View File

@ -8,6 +8,7 @@ use Carbon\CarbonImmutable;
use Engelsystem\Helpers\BarChart;
use Engelsystem\Renderer\Renderer;
use Engelsystem\Test\Unit\TestCase;
use Generator;
use PHPUnit\Framework\MockObject\MockObject;
class BarChartTest extends TestCase
@ -40,40 +41,9 @@ class BarChartTest extends TestCase
$this->mockTranslator();
}
/**
* @covers \Engelsystem\Helpers\BarChart::render
* @covers \Engelsystem\Helpers\BarChart::calculateChartGroups
* @covers \Engelsystem\Helpers\BarChart::calculateYLabels
* @covers \Engelsystem\Helpers\BarChart::generateChartDemoData
*/
public function testRender(): void
public function provideRenderTestData(): Generator
{
$this->rendererMock->expects(self::once())
->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' => [
$yLabels = [
[
'label' => '0',
'bottom' => '0%',
@ -98,12 +68,64 @@ class BarChartTest extends TestCase
'label' => '5',
'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');
self::assertSame(
'test bar chart',
BarChart::render(self::ROW_LABELS, self::COLORS, self::DATA)
BarChart::render(self::ROW_LABELS, self::COLORS, $testData)
);
}