Skip to content

Commit 201e7f5

Browse files
committed
Initial commit
1 parent 4e4e38d commit 201e7f5

18 files changed

+774
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
.idea
3+
node_modules
4+
vendor
5+
composer.lock
6+
package-lock.json

README.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Laravel Echarts
2+
3+
Server-side rendering of [Apache ECharts](https://echarts.apache.org/) with Laravel and Node.js. Render chart images as
4+
png, jpg, pdf or svg to storage.
5+
6+
## Requirements
7+
8+
- Node.js and npm
9+
10+
## Installation
11+
12+
### Install package
13+
14+
```shell
15+
composer require codebarista/laravel-echarts
16+
```
17+
18+
### Install node modules
19+
20+
```shell
21+
php artisan codebarista:node-echarts install
22+
```
23+
24+
### Publish config (optional)
25+
26+
```shell
27+
php artisan vendor:publish --tag="config" --provider="Codebarista\LaravelEcharts\EchartsServiceProvider"
28+
```
29+
30+
### Install pngcrush (optional)
31+
32+
- https://pmt.sourceforge.io/pngcrush
33+
34+
```shell
35+
apt install pngcrush
36+
```
37+
38+
## Usage
39+
40+
### Store Chart Image
41+
42+
```php
43+
use Codebarista\LaravelEcharts\Actions\StoreChartImage;
44+
45+
// ...
46+
47+
StoreChartImage::make()->handle([
48+
'title' => [
49+
'text' => 'Basic Bar Chart',
50+
],
51+
'xAxis' => [
52+
'type' => 'category',
53+
'data' => ['Shirts', 'Cardigans', 'Chiffons', 'Pants', 'Heels', 'Socks'],
54+
],
55+
'yAxis' => [
56+
'type' => 'value',
57+
],
58+
'series' => [
59+
[
60+
'type' => 'bar',
61+
'data' => [5, 20, 36, 10, 10, 20],
62+
],
63+
],
64+
]);
65+
```
66+
67+
### Config
68+
69+
Overwrite the default attributes from config file:
70+
71+
```php
72+
$action = StoreChartImage::make()
73+
->mimeType(MimeTypes::PNG) // PNG, JPG, PDF, SVG
74+
->optimize(true) // optimize with pngcrush
75+
->filePath('app/public') // storage path
76+
->fileName('simple-bar-chart') // w/o extension
77+
->width(600) // image width
78+
->height(600); // image height
79+
80+
81+
$action->handle(options: [...]);
82+
```
83+
84+
## License
85+
86+
The MIT License (MIT). Please see [License File](LICENSE) for more information.

bootstrap/helpers.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\App;
4+
5+
if (! function_exists('codebarista_path')) {
6+
function codebarista_path(string $path = ''): string
7+
{
8+
return App::joinPaths(dirname(__DIR__), $path);
9+
}
10+
}
11+
12+
if (! function_exists('pngcrush')) {
13+
function pngcrush(string $image): bool
14+
{
15+
$format = 'pngcrush -d %s -q -rem alla %s > /dev/null 2>&1';
16+
$dir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR);
17+
$name = basename($image);
18+
19+
$command = sprintf($format, $dir, escapeshellarg($image));
20+
21+
if (exec($command) !== false) {
22+
return rename($dir.DIRECTORY_SEPARATOR.$name, $image);
23+
}
24+
25+
return false;
26+
}
27+
}

composer.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "codebarista/laravel-echarts",
3+
"description": "Create an image from Apache ECharts.",
4+
"type": "library",
5+
"keywords": [
6+
"laravel",
7+
"echarts"
8+
],
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Ralf Langebrake",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"require": {
17+
"laravel/framework": "^10.0"
18+
},
19+
"require-dev": {
20+
"orchestra/testbench": "^8.21",
21+
"pestphp/pest-plugin-laravel": "^2.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Codebarista\\LaravelEcharts\\": "src/"
26+
},
27+
"files": [
28+
"bootstrap/helpers.php"
29+
]
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Codebarista\\LaravelEcharts\\Tests\\": "tests/"
34+
}
35+
},
36+
"extra": {
37+
"laravel": {
38+
"providers": [
39+
"Codebarista\\LaravelEcharts\\EchartsServiceProvider"
40+
]
41+
}
42+
},
43+
"config": {
44+
"optimize-autoloader": true,
45+
"preferred-install": "dist",
46+
"sort-packages": true,
47+
"allow-plugins": {
48+
"pestphp/pest-plugin": true
49+
}
50+
},
51+
"minimum-stability": "stable",
52+
"prefer-stable": true
53+
}

config/echarts.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use Codebarista\LaravelEcharts\Enums\MimeTypes;
4+
5+
return [
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Node Canvas
10+
|--------------------------------------------------------------------------
11+
|
12+
| node-canvas is a Cairo-backed Canvas implementation for Node.js. Make
13+
| sure your server meets the requirements. For more information, visit:
14+
|
15+
| https://www.npmjs.com/package/canvas
16+
|
17+
*/
18+
19+
'storage' => [
20+
'path' => env('ECHARTS_STORAGE_PATH', 'app/public/vendor/echarts'),
21+
],
22+
23+
'canvas' => [
24+
'height' => env('ECHARTS_CANVAS_HEIGHT', 600),
25+
'width' => env('ECHARTS_CANVAS_WIDTH', 600),
26+
'mime_type' => MimeTypes::PNG,
27+
],
28+
29+
/*
30+
|--------------------------------------------------------------------------
31+
| Image Optimizer
32+
|--------------------------------------------------------------------------
33+
|
34+
| PNG (pngcrush)
35+
| pngcrush is a free and open-source command-line utility for optimizing PNG
36+
| image files. It reduces the size of the file lossless – that is, the resulting
37+
| "crushed" image will have the same quality as the source image.
38+
|
39+
| To support PNG optimization, pngcrush must be installed on the server:
40+
|
41+
| https://pmt.sourceforge.io/pngcrush
42+
|
43+
*/
44+
45+
'optimize' => [
46+
'png' => env('ECHARTS_OPTIMIZE_PNG', false),
47+
],
48+
];

phpunit.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
stopOnFailure="true"
6+
cacheResult="false"
7+
colors="true"
8+
>
9+
<testsuites>
10+
<testsuite name="Laravel Echarts Test Suite">
11+
<directory suffix="Test.php">./tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
<source>
15+
<include>
16+
<directory suffix=".php">./src</directory>
17+
</include>
18+
</source>
19+
</phpunit>

src/Actions/StoreChartImage.php

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace Codebarista\LaravelEcharts\Actions;
4+
5+
use Codebarista\LaravelEcharts\Enums\MimeTypes;
6+
use Codebarista\LaravelEcharts\Exceptions\StoreChartImageException;
7+
use Codebarista\LaravelEcharts\Makeable;
8+
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Facades\File;
10+
use Illuminate\Support\Facades\Process;
11+
use Illuminate\Support\Str;
12+
use Throwable;
13+
14+
class StoreChartImage
15+
{
16+
use Makeable;
17+
18+
private MimeTypes $mimeType;
19+
20+
private string $fileName;
21+
22+
private string $filePath;
23+
24+
private string $fullPath;
25+
26+
private bool $optimize;
27+
28+
private int $width;
29+
30+
private int $height;
31+
32+
public function mimeType(MimeTypes $value): static
33+
{
34+
$this->mimeType = $value;
35+
36+
return $this;
37+
}
38+
39+
public function fileName(string $value): static
40+
{
41+
$this->fileName = $value;
42+
43+
return $this;
44+
}
45+
46+
public function filePath(string $value): static
47+
{
48+
$this->filePath = $value;
49+
50+
return $this;
51+
}
52+
53+
public function optimize(bool $value): static
54+
{
55+
$this->optimize = $value;
56+
57+
return $this;
58+
}
59+
60+
public function width(int $value): static
61+
{
62+
$this->width = $value;
63+
64+
return $this;
65+
}
66+
67+
public function height(int $value): static
68+
{
69+
$this->height = $value;
70+
71+
return $this;
72+
}
73+
74+
public function __construct()
75+
{
76+
$this->filePath(config('echarts.storage.path', 'app/public/vendor/echarts'));
77+
$this->mimeType(config('echarts.canvas.mime_type', MimeTypes::PNG));
78+
$this->optimize(config('echarts.optimize.png', false));
79+
$this->height(config('echarts.canvas.height', 600));
80+
$this->width(config('echarts.canvas.width', 600));
81+
$this->fileName(Str::random());
82+
}
83+
84+
/**
85+
* @throws StoreChartImageException|Throwable
86+
*/
87+
public function handle(array $options): string
88+
{
89+
$process = Process::path(codebarista_path('tools/echarts'))
90+
->run($this->getCommand($options));
91+
92+
throw_if($process->failed(),
93+
new StoreChartImageException($process->errorOutput()));
94+
95+
if ($this->optimize && $this->mimeType === MimeTypes::PNG) {
96+
pngcrush($this->fullPath);
97+
}
98+
99+
return $this->fullPath;
100+
}
101+
102+
private function getCommand(array $options): array
103+
{
104+
$filePath = storage_path($this->filePath);
105+
106+
File::ensureDirectoryExists($filePath);
107+
108+
$fileName = $this->fileName.match ($this->mimeType) {
109+
MimeTypes::PDF => '.pdf',
110+
MimeTypes::JPG => '.jpg',
111+
MimeTypes::PNG => '.png',
112+
MimeTypes::SVG => '.svg'
113+
};
114+
115+
$this->fullPath = $filePath.DIRECTORY_SEPARATOR.$fileName;
116+
117+
return [
118+
'node', 'render.js',
119+
'--options', Arr::toJson($options),
120+
'--type', $this->mimeType->value,
121+
'--path', $this->fullPath,
122+
'--height', $this->height,
123+
'--width', $this->width,
124+
];
125+
}
126+
}

0 commit comments

Comments
 (0)