Skip to content

Commit e5f584f

Browse files
committed
Load base option from file and add it as param
1 parent 201e7f5 commit e5f584f

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Overwrite the default attributes from config file:
7070

7171
```php
7272
$action = StoreChartImage::make()
73+
->baseOptionPath(resource_path('echarts/base-option.js')) // base chart to be merged
7374
->mimeType(MimeTypes::PNG) // PNG, JPG, PDF, SVG
7475
->optimize(true) // optimize with pngcrush
7576
->filePath('app/public') // storage path

resources/echarts/base-option.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const option = {
2+
animation: false,
3+
tooltip: {},
4+
xAxis: {
5+
type: 'category',
6+
},
7+
yAxis: {
8+
type: 'value'
9+
},
10+
series: {}
11+
};

src/Actions/StoreChartImage.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class StoreChartImage
1515
{
1616
use Makeable;
1717

18+
private string $baseOptionPath;
19+
1820
private MimeTypes $mimeType;
1921

2022
private string $fileName;
@@ -29,6 +31,13 @@ class StoreChartImage
2931

3032
private int $height;
3133

34+
public function baseOptionPath(string $value): static
35+
{
36+
$this->baseOptionPath = $value;
37+
38+
return $this;
39+
}
40+
3241
public function mimeType(MimeTypes $value): static
3342
{
3443
$this->mimeType = $value;
@@ -73,6 +82,7 @@ public function height(int $value): static
7382

7483
public function __construct()
7584
{
85+
$this->baseOptionPath(codebarista_path('resources/echarts/base-option.js'));
7686
$this->filePath(config('echarts.storage.path', 'app/public/vendor/echarts'));
7787
$this->mimeType(config('echarts.canvas.mime_type', MimeTypes::PNG));
7888
$this->optimize(config('echarts.optimize.png', false));
@@ -84,10 +94,10 @@ public function __construct()
8494
/**
8595
* @throws StoreChartImageException|Throwable
8696
*/
87-
public function handle(array $options): string
97+
public function handle(array $option): string
8898
{
8999
$process = Process::path(codebarista_path('tools/echarts'))
90-
->run($this->getCommand($options));
100+
->run($this->getCommand($option));
91101

92102
throw_if($process->failed(),
93103
new StoreChartImageException($process->errorOutput()));
@@ -99,7 +109,7 @@ public function handle(array $options): string
99109
return $this->fullPath;
100110
}
101111

102-
private function getCommand(array $options): array
112+
private function getCommand(array $option): array
103113
{
104114
$filePath = storage_path($this->filePath);
105115

@@ -116,7 +126,8 @@ private function getCommand(array $options): array
116126

117127
return [
118128
'node', 'render.js',
119-
'--options', Arr::toJson($options),
129+
'--baseOptionPath', $this->baseOptionPath,
130+
'--option', Arr::toJson($option),
120131
'--type', $this->mimeType->value,
121132
'--path', $this->fullPath,
122133
'--height', $this->height,

tools/echarts/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "laravel-echarts",
33
"type": "module",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"description": "Create images from echarts",
66
"main": "render.js",
77
"license": "MIT",
88
"dependencies": {
99
"canvas": "^2.11.2",
1010
"echarts": "^5.4.3",
11+
"lodash": "^4.17.21",
1112
"yargs": "^17.7.2"
1213
}
1314
}

tools/echarts/render.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
11
'use strict';
22

33
import {createCanvas} from 'canvas';
4+
import * as echarts from 'echarts';
45
import {writeFile} from 'node:fs';
5-
// import echarts from 'echarts';
6-
import echarts from 'echarts/dist/echarts.js';
76
import yargs from 'yargs';
8-
9-
echarts.setPlatformAPI(createCanvas);
7+
import _ from 'lodash';
108

119
const argv = yargs(process.argv.slice(2)).argv;
1210

1311
const type = argv.type.includes('svg') ? 'svg' : argv.type.split('/').pop();
1412
const canvas = createCanvas(argv.width, argv.height, type);
1513
const chart = echarts.init(canvas);
1614

17-
const options = JSON.parse(argv.options);
18-
const defaults = {
19-
animation: false,
20-
tooltip: {},
21-
xAxis: {
22-
type: 'category',
23-
},
24-
yAxis: {
25-
type: 'value'
26-
},
27-
series: {}
28-
};
29-
30-
chart.setOption({
31-
...defaults,
32-
...options
33-
});
15+
const {option} = await import(argv.baseOptionPath)
16+
const params = JSON.parse(argv.option);
17+
18+
chart.setOption(_.merge(option, params));
3419

3520
const buffer = canvas.toBuffer(argv.type);
3621

22+
echarts.setPlatformAPI(canvas);
23+
3724
writeFile(argv.path, buffer, (err) => {
3825
if (err) throw err;
3926
});

0 commit comments

Comments
 (0)