Skip to content

Commit

Permalink
Allow adding a SVG viewBox
Browse files Browse the repository at this point in the history
  • Loading branch information
maantje committed Jan 22, 2025
1 parent 88ff247 commit 0f2fb20
Show file tree
Hide file tree
Showing 18 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/output/advanced-bar-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/output/advanced-line-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/output/bar-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/output/curved-line-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/output/grouped-bar-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/output/line-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/output/mixed-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/output/stacked-bar-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/output/step-line-chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion src/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 <<<SVG
<svg xmlns="http://www.w3.org/2000/svg" width="$this->width" height="$this->height">
<svg xmlns="http://www.w3.org/2000/svg" width="$this->width" height="$this->height" viewBox="$this->viewBox">
{$this->background()}
{$this->renderYAxis()}
{$this->xAxis->render($this)}
Expand Down
Loading

0 comments on commit 0f2fb20

Please sign in to comment.