-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
557 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
require '../vendor/autoload.php'; | ||
|
||
use Maantje\Charts\Bar\Bar; | ||
use Maantje\Charts\Bar\BarGroup; | ||
use Maantje\Charts\Bar\Bars; | ||
use Maantje\Charts\Chart; | ||
use Maantje\Charts\YAxis; | ||
|
||
$chart = new Chart( | ||
yAxis: new YAxis( | ||
minValue: 0, | ||
maxValue: 1000, | ||
), | ||
series: [ | ||
new Bars( | ||
bars: [ | ||
new BarGroup( | ||
name: 'January', | ||
bars: [ | ||
new Bar( | ||
value: 101, | ||
color: '#1E90FF', | ||
), | ||
new Bar( | ||
value: 251, | ||
color: '#32CD32', | ||
), | ||
new Bar( | ||
value: 389, | ||
color: '#FFA500', | ||
), | ||
new Bar( | ||
value: 457, | ||
color: '#FFD700', | ||
), | ||
new Bar( | ||
value: 601, | ||
color: '#FF4500', | ||
), | ||
], | ||
radius: 10, | ||
), | ||
new BarGroup( | ||
name: 'February', | ||
bars: [ | ||
new Bar( | ||
value: 73, | ||
color: '#1E90FF', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 223, | ||
color: '#32CD32', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 347, | ||
color: '#FFA500', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 509, | ||
color: '#FFD700', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 653, | ||
color: '#FF4500', | ||
radius: 10, | ||
), | ||
], | ||
), | ||
new BarGroup( | ||
name: 'March', | ||
bars: [ | ||
new Bar( | ||
value: 113, | ||
color: '#1E90FF', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 281, | ||
color: '#32CD32', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 401, | ||
color: '#FFA500', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 541, | ||
color: '#FFD700', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 733, | ||
color: '#FF4500', | ||
radius: 10, | ||
), | ||
], | ||
), | ||
new BarGroup( | ||
name: 'April', | ||
bars: [ | ||
new Bar( | ||
value: 193, | ||
color: '#1E90FF', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 311, | ||
color: '#32CD32', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 457, | ||
color: '#FFA500', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 613, | ||
color: '#FFD700', | ||
radius: 10, | ||
), | ||
new Bar( | ||
value: 809, | ||
color: '#FF4500', | ||
radius: 10, | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
); | ||
|
||
echo $chart->render(); |
File renamed without changes.
File renamed without changes
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Maantje\Charts\Bar; | ||
|
||
use Maantje\Charts\Chart; | ||
use Maantje\Charts\SVG\Fragment; | ||
use Maantje\Charts\SVG\Line; | ||
use Maantje\Charts\SVG\Text; | ||
|
||
class BarGroup implements BarContract | ||
{ | ||
/** | ||
* @param Bar[] $bars | ||
*/ | ||
public function __construct( | ||
private readonly string $name, | ||
private readonly int $margin = 5, | ||
private readonly int $width = 100, | ||
private readonly array $bars = [], | ||
public string $labelColor = '#333', | ||
public int $labelMarginY = 30, | ||
public ?int $fontSize = null, | ||
public ?int $radius = null, | ||
) { | ||
if (is_null($this->radius)) { | ||
return; | ||
} | ||
|
||
foreach ($this->bars as $bar) { | ||
if (is_null($bar->radius)) { | ||
$bar->radius = $this->radius; | ||
} | ||
} | ||
} | ||
|
||
public function maxValue(): float | ||
{ | ||
if (count($this->bars) === 0) { | ||
return 0; | ||
} | ||
|
||
return max(array_map(fn (BarContract $bar) => $bar->value(), $this->bars)); | ||
} | ||
|
||
public function minValue(): float | ||
{ | ||
if (count($this->bars) === 0) { | ||
return 0; | ||
} | ||
|
||
return min(array_map(fn (BarContract $bar) => $bar->value(), $this->bars)); | ||
} | ||
|
||
public function render(Chart $chart, float $x, float $maxGroupWidth): string | ||
{ | ||
$numBars = count($this->bars); | ||
$barWidth = 0; | ||
|
||
if ($numBars > 0) { | ||
$barWidth = min($maxGroupWidth, $this->width) / $numBars; | ||
} | ||
|
||
$labelX = $x + ($maxGroupWidth / 2); | ||
$startX = $x; | ||
$x += ($maxGroupWidth / 2) - (($barWidth + $this->margin) * $numBars / 2); | ||
|
||
return new Fragment([ | ||
new Line( | ||
x1: $startX, | ||
y1: $chart->bottom(), | ||
x2: $startX, | ||
y2: $chart->bottom() + 10, | ||
), | ||
...array_map(function (BarContract $bar) use ($barWidth, &$x, $chart) { | ||
$svg = $bar->render($chart, $x + $this->margin, $barWidth); | ||
$x += $this->margin + $barWidth; | ||
|
||
return $svg; | ||
}, $this->bars), | ||
new Line( | ||
x1: $startX + $maxGroupWidth, | ||
y1: $chart->bottom(), | ||
x2: $startX + $maxGroupWidth, | ||
y2: $chart->bottom() + 10, | ||
), | ||
new Text( | ||
content: $this->name, | ||
x: $labelX, | ||
y: $chart->bottom() + $this->labelMarginY, | ||
fontFamily: $chart->fontFamily, | ||
fontSize: $this->fontSize ?? $chart->fontSize, | ||
fill: $this->labelColor, | ||
textAnchor: 'middle' | ||
), | ||
]); | ||
} | ||
|
||
public function value(): float | ||
{ | ||
return $this->maxValue(); | ||
} | ||
} |
Oops, something went wrong.