Skip to content

codebarista/laravel-echarts

Repository files navigation

Laravel Echarts

Server-side rendering of Apache ECharts with Laravel and Node.js. Render chart images as png, jpg, pdf or svg to storage.

Requirements

  • Node.js and npm

Installation

Install package

composer require codebarista/laravel-echarts

Install node modules

php artisan codebarista:node-echarts install

For updates it would be a good idea to add the command to the root composer.json.

{
    "scripts": {
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force",
            "@php artisan codebarista:node-echarts install"
        ]
    },
}

Publish config (optional)

php artisan vendor:publish --tag="config" --provider="Codebarista\LaravelEcharts\EchartsServiceProvider"

Install pngcrush (optional)

apt install pngcrush

Usage

Store Chart Image

use Codebarista\LaravelEcharts\Actions\StoreChartImage;

// ...

StoreChartImage::make()->handle([
    'title' => [
        'text' => 'Basic Bar Chart',
    ],
    'xAxis' => [
        'type' => 'category',
        'data' => ['Shirts', 'Cardigans', 'Chiffons', 'Pants', 'Heels', 'Socks'],
    ],
    'yAxis' => [
        'type' => 'value',
    ],
    'series' => [
        [
            'type' => 'bar',
            'data' => [5, 20, 36, 10, 10, 20],
        ],
    ],
]);

Config

Overwrite the default attributes from config file:

$action = StoreChartImage::make()
    ->baseOptionPath(resource_path('echarts/base-option.mjs')) // base chart to be merged
    ->mimeType(MimeTypes::PNG) // PNG, JPG, PDF, SVG
    ->optimize(true) // optimize with pngcrush
    ->filePath('app/public') // storage path
    ->fileName('simple-bar-chart') // w/o extension
    ->width(600) // image width
    ->height(600); // image height


$action->handle(options: [...]);

License

The MIT License (MIT). Please see License File for more information.