From 0f2fb204701afa571356d241c78077c2e7356fc7 Mon Sep 17 00:00:00 2001 From: Jamie Schouten Date: Wed, 22 Jan 2025 22:20:34 +0100 Subject: [PATCH] Allow adding a SVG viewBox --- examples/output/advanced-bar-chart.svg | 2 +- examples/output/advanced-line-chart.svg | 2 +- examples/output/bar-chart.svg | 2 +- examples/output/curved-line-chart.svg | 2 +- examples/output/grouped-bar-chart.svg | 2 +- examples/output/line-chart.svg | 2 +- examples/output/mixed-chart.svg | 2 +- examples/output/stacked-bar-chart.svg | 2 +- examples/output/step-line-chart.svg | 2 +- src/Chart.php | 7 ++++++- tests/Unit/AnnotationTest.php | 2 +- tests/Unit/BarChartTest.php | 4 ++-- tests/Unit/ChartTest.php | 2 +- tests/Unit/CurvedLineChartTest.php | 5 ++--- tests/Unit/GroupedBarChartTest.php | 4 ++-- tests/Unit/LineChartTest.php | 4 ++-- tests/Unit/StackedBartChartTest.php | 4 ++-- tests/Unit/StepLineChartTest.php | 4 ++-- 18 files changed, 29 insertions(+), 25 deletions(-) diff --git a/examples/output/advanced-bar-chart.svg b/examples/output/advanced-bar-chart.svg index 998ec4f..9f674b6 100644 --- a/examples/output/advanced-bar-chart.svg +++ b/examples/output/advanced-bar-chart.svg @@ -1,4 +1,4 @@ - + € 120.000,00€ 142.000,00€ 164.000,00€ 186.000,00€ 208.000,00€ 230.000,00 diff --git a/examples/output/advanced-line-chart.svg b/examples/output/advanced-line-chart.svg index 458d857..861bc98 100644 --- a/examples/output/advanced-line-chart.svg +++ b/examples/output/advanced-line-chart.svg @@ -1,4 +1,4 @@ - + 303438424650Temperature0 m600 m1200 m1800 m2400 m3000 mElevation 02:42 diff --git a/examples/output/bar-chart.svg b/examples/output/bar-chart.svg index 2879e94..8c3975d 100644 --- a/examples/output/bar-chart.svg +++ b/examples/output/bar-chart.svg @@ -1,4 +1,4 @@ - + 044,46088,920133,381177,841222,301 diff --git a/examples/output/curved-line-chart.svg b/examples/output/curved-line-chart.svg index e18d739..717debc 100644 --- a/examples/output/curved-line-chart.svg +++ b/examples/output/curved-line-chart.svg @@ -1,4 +1,4 @@ - + 0265177102128 0 diff --git a/examples/output/grouped-bar-chart.svg b/examples/output/grouped-bar-chart.svg index 1a39ec8..21b9a8f 100644 --- a/examples/output/grouped-bar-chart.svg +++ b/examples/output/grouped-bar-chart.svg @@ -1,4 +1,4 @@ - + 02004006008001,000 diff --git a/examples/output/line-chart.svg b/examples/output/line-chart.svg index 075a8ef..5be7afb 100644 --- a/examples/output/line-chart.svg +++ b/examples/output/line-chart.svg @@ -1,4 +1,4 @@ - + 0510141924 0 diff --git a/examples/output/mixed-chart.svg b/examples/output/mixed-chart.svg index 763d338..4486e2b 100644 --- a/examples/output/mixed-chart.svg +++ b/examples/output/mixed-chart.svg @@ -1,4 +1,4 @@ - + 02.8005.6008.40011.20014.000 0 diff --git a/examples/output/stacked-bar-chart.svg b/examples/output/stacked-bar-chart.svg index c6e5502..3e5fe32 100644 --- a/examples/output/stacked-bar-chart.svg +++ b/examples/output/stacked-bar-chart.svg @@ -1,4 +1,4 @@ - + 01428425670 diff --git a/examples/output/step-line-chart.svg b/examples/output/step-line-chart.svg index f9363aa..988f4b6 100644 --- a/examples/output/step-line-chart.svg +++ b/examples/output/step-line-chart.svg @@ -1,4 +1,4 @@ - + 022446688110 0 diff --git a/src/Chart.php b/src/Chart.php index d1eb141..9fb2c7e 100644 --- a/src/Chart.php +++ b/src/Chart.php @@ -43,6 +43,7 @@ public function __construct( protected float $rightMargin = 30, protected float $bottomMargin = 50, protected float $topMargin = 25, + protected ?string $viewBox = null, ) { $this->yAxis = is_array($yAxis) ? $yAxis : [$yAxis]; $this->yAxis = array_reduce($this->yAxis, function (array $carry, YAxis $yAxis) { @@ -58,12 +59,16 @@ public function __construct( if (count($this->xAxis->data) === 0) { $this->guessXAxisData(); } + + if (is_null($this->viewBox)) { + $this->viewBox = "0 0 $this->width $this->height"; + } } public function render(): string { return << + {$this->background()} {$this->renderYAxis()} {$this->xAxis->render($this)} diff --git a/tests/Unit/AnnotationTest.php b/tests/Unit/AnnotationTest.php index 2058810..5319e05 100644 --- a/tests/Unit/AnnotationTest.php +++ b/tests/Unit/AnnotationTest.php @@ -50,7 +50,7 @@ expect(pretty($chart->render())) ->toBe(<<<'SVG' - + diff --git a/tests/Unit/BarChartTest.php b/tests/Unit/BarChartTest.php index 751177c..8d8c315 100644 --- a/tests/Unit/BarChartTest.php +++ b/tests/Unit/BarChartTest.php @@ -27,7 +27,7 @@ ); expect(pretty($chart->render()))->toBe(<<<'SVG' - + @@ -73,7 +73,7 @@ ); expect(pretty($chart->render()))->toBe(<<<'SVG' - + diff --git a/tests/Unit/ChartTest.php b/tests/Unit/ChartTest.php index b7a1a7b..7f552fb 100644 --- a/tests/Unit/ChartTest.php +++ b/tests/Unit/ChartTest.php @@ -6,7 +6,7 @@ $chart = new Chart; expect(pretty($chart->render()))->toBe(<<<'SVG' - + diff --git a/tests/Unit/CurvedLineChartTest.php b/tests/Unit/CurvedLineChartTest.php index 5b08298..177842b 100644 --- a/tests/Unit/CurvedLineChartTest.php +++ b/tests/Unit/CurvedLineChartTest.php @@ -36,7 +36,7 @@ ); expect(pretty($chart->render()))->toBe(<<<'SVG' - + @@ -102,9 +102,8 @@ ), ], ); - $svg = <<<'SVG' - + diff --git a/tests/Unit/GroupedBarChartTest.php b/tests/Unit/GroupedBarChartTest.php index baf8071..9301a48 100644 --- a/tests/Unit/GroupedBarChartTest.php +++ b/tests/Unit/GroupedBarChartTest.php @@ -109,7 +109,7 @@ echo $chart->render(); expect(pretty($chart->render()))->toBe(<<<'SVG' - + @@ -206,7 +206,7 @@ ); expect(pretty($chart->render()))->toBe(<<<'SVG' - + diff --git a/tests/Unit/LineChartTest.php b/tests/Unit/LineChartTest.php index 38a6ae9..c419e2f 100644 --- a/tests/Unit/LineChartTest.php +++ b/tests/Unit/LineChartTest.php @@ -33,7 +33,7 @@ ); expect(pretty($chart->render()))->toBe(<<<'SVG' - + @@ -101,7 +101,7 @@ ); $svg = <<<'SVG' - + diff --git a/tests/Unit/StackedBartChartTest.php b/tests/Unit/StackedBartChartTest.php index 541adb9..b4b2360 100644 --- a/tests/Unit/StackedBartChartTest.php +++ b/tests/Unit/StackedBartChartTest.php @@ -32,7 +32,7 @@ expect(pretty($chart->render())) ->toBe(<<<'SVG' - + @@ -92,7 +92,7 @@ expect(pretty($svg)) ->toBe(<<<'SVG' - + diff --git a/tests/Unit/StepLineChartTest.php b/tests/Unit/StepLineChartTest.php index 1e53001..10d7c37 100644 --- a/tests/Unit/StepLineChartTest.php +++ b/tests/Unit/StepLineChartTest.php @@ -36,7 +36,7 @@ ); expect(pretty($chart->render()))->toBe(<<<'SVG' - + @@ -104,7 +104,7 @@ ); $svg = <<<'SVG' - +