Skip to content

Commit

Permalink
[fix]add try catch Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomaiyun committed Sep 28, 2022
1 parent 77f8987 commit f75c6b3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
46 changes: 37 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Apache ECharts Node.js Export Server
# Apache ECharts HTTP Export Server

Convert Apache ECharts charts to static image files.
This is a Node.js-based service, and uses [node canvas](https://github.com/Automattic/node-canvas) to render [Apache ECharts](https://echarts.apache.org/) charts to images (PNG, JPG, SVG , PDF and Base64) to be sent back to the user.

## What

This is a node.js application/service that converts [Apache ECharts](https://echarts.apache.org/) charts to static image files. It supports PNG, JPEG, SVG, and PDF output; and the input can be either SVG, or JSON-formatted chart options.
The export server is a Node.js-based service, which is easy to install and integrate on any system. It accepts either JSON-formatted chart options or SVGs, together with additional resources, and uses [node canvas](https://github.com/Automattic/node-canvas) to render [Apache ECharts](https://echarts.apache.org/) charts to images (PNG, JPG, SVG , PDF and Base64) to be sent back to the user.

The application can be used either as a CLI (Command Line Interface), as an HTTP server, or as a node.js module.

Expand Down Expand Up @@ -136,6 +136,7 @@ cp msyh.ttf /usr/share/fonts/truetype/
"download": false,
"option": {
"backgroundColor": "#fff",
"animation": false,
"xAxis": {
"type": "category",
"data": [
Expand All @@ -162,7 +163,10 @@ cp msyh.ttf /usr/share/fonts/truetype/
1330,
1720
],
"type": "bar"
"type": "line",
"label": {
"show": true
}
}
]
}
Expand Down Expand Up @@ -190,6 +194,7 @@ curl -H "Content-Type: application/json" \
"download": false,
"option": {
"backgroundColor": "#fff",
"animation": false,
"xAxis": {
"type": "category",
"data": [
Expand All @@ -216,25 +221,48 @@ curl -H "Content-Type: application/json" \
1330,
1720
],
"type": "bar"
"type": "line",
"label": {
"show": true
}
}
]
}
}'
```

### ECharts Java Client

It's recommended to create an `Option` object and its Json representation in chainable Java codes using [ECharts Java](https://github.com/ECharts-Java/ECharts-Java) . Please refer to the ECharts Java documentation for details on how to set this up.

```java
public static void main(String[] args) {
// All methods in ECharts Java supports method chaining
Bar bar = new Bar()
.setLegend()
.setTooltip("item")
.addXAxis(new String[] { "Matcha Latte", "Milk Tea", "Cheese Cocoa", "Walnut Brownie" })
.addYAxis()
.addSeries("2015", new Number[] { 43.3, 83.1, 86.4, 72.4 })
.addSeries("2016", new Number[] { 85.8, 73.4, 65.2, 53.9 })
.addSeries("2017", new Number[] { 93.7, 55.1, 82.5, 39.1 });
Engine engine = new Engine();
// The renderJsonOption method will return a string, which represents an Option object in JSON format.
String optionJsonString = engine.renderJsonOption(line);
System.out.println(optionJsonString);
}
```

### Base64 Response Format

```json
{
"code": 200,
"msg": "success",
"data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA"
"data": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..."
}
```



## Reference

[Apache ECharts Server Side Rendering](https://echarts.apache.org/handbook/en/how-to/cross-platform/server/)
Expand All @@ -245,7 +273,7 @@ curl -H "Content-Type: application/json" \

[PM2](https://www.npmjs.com/package/pm2)


[ECharts Java](https://github.com/ECharts-Java/ECharts-Java)

## License

Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "echarts-export-server",
"version": "1.0.0",
"description": "Apache ECharts Node.js Export Server",
"version": "1.0.1",
"description": "Apache ECharts HTTP Export Server",
"main": "src/index.js",
"scripts": {
"start": "pm2 start ./src/index.js --name echarts-export-server",
Expand All @@ -27,9 +27,12 @@
"keywords": [
"Apache",
"ECharts",
"HTTP",
"Export",
"Server"
"Server",
"Java",
"Visualization"
],
"author": "xiaomaiyun",
"license": "Apache License 2.0"
}
}
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,19 @@ http.createServer(function (req, res) {
res.setHeader('Content-Disposition', 'attachment; filename="chart.' + config.type + '"');
}

res.write(renderChart(config));
let result;
try {
result = renderChart(config);
} catch (e) {
console.error("Error: Canvas rendering failed!" + e.message);
res.setHeader('Content-Type', 'application/json;charset=UTF-8');
result = JSON.stringify({
code: 500,
msg: 'Error: Canvas rendering failed! The content of the request parameter "option" may be invalid!',
data: config.option
});
}
res.write(result);
res.end();
})
}).listen(port, function () {
Expand Down
6 changes: 5 additions & 1 deletion test/demo_json.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"download": false,
"option": {
"backgroundColor": "#fff",
"animation": false,
"xAxis": {
"type": "category",
"data": [
Expand All @@ -32,7 +33,10 @@
1330,
1720
],
"type": "bar"
"type": "line",
"label": {
"show": true
}
}
]
}
Expand Down

0 comments on commit f75c6b3

Please sign in to comment.