codebarista/laravel-echarts

Create an image from Apache ECharts.

1.1.0 2024-03-12 19:42 UTC

This package is auto-updated.

Last update: 2024-04-30 13:38:52 UTC


README

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.