一个 NodeJS 服务,提供后台接口(SSR渲染)生成 ECharts 图片。
361
一个 NodeJS 服务,提供后台接口(SSR渲染)生成 ECharts 图片。可 Docker 启动。
docker 镜像版本的前面部分和使用的 echarts 版本保持一致,镜像版本的最后一个部分为额外 echarts-server 小版本。
比如镜像版本 6.0.0.1 版本,表示使用 echarts 版本为 6.0.0,最后面的 1 为 echarts-server 的第一个版本。
docker pull mengweijin/echarts-server:6.0.0.1
docker run \
--name echarts-server \
-p 3000:3000 \
--restart=on-failure:3 \
-d mengweijin/echarts-server:6.0.0.1
前提:需要 NodeJS 环境。
# 安装依赖
npm install
# 启动服务
npm start
访问 3000 端口即可。
POST 携带 RequestBody 参数请求 http://localhost:3000
# 或者(指定图片格式和大小)
POST 携带 RequestBody 参数请求 http://localhost:3000?type=svg&width=800&height=600
| 参数名称 | 是否必选 | 默认值 | 描述 |
|---|---|---|---|
| type | 否 | png | 图片类型。可选以下值:["svg", "jpeg", "png", "webp", "gif", "jp2", "tiff", "avif", "heif", "jxl"] |
| width | 否 | 1200 | |
| height | 否 | 900 |
请求体同 echarts 中的 options 参数,与在 javascript 中不同的是,在 JSON 中,key 值需要被双引号包裹起来。
{
"xAxis": {
"type": "category",
"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
},
"yAxis": {
"type": "value"
},
"series": [
{
"data": [120, 200, 150, 80, 70, 110, 130],
"type": "bar"
}
]
}
curl -X POST http://localhost:3000 -o echarts.png \
-d '{
"xAxis": {
"type": "category",
"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
},
"yAxis": {
"type": "value"
},
"series": [
{
"data": [120, 200, 150, 80, 70, 110, 130],
"type": "bar"
}
]
}'
curl -X POST http://localhost:3000?type=svg&width=800&height=600 -o echarts.png \
-d '{
"xAxis": {
"type": "category",
"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
},
"yAxis": {
"type": "value"
},
"series": [
{
"data": [120, 200, 150, 80, 70, 110, 130],
"type": "bar"
}
]
}'
请求体要符合 echarts 的格式的 JSON 对象,其中东西比较多,那么如何更方便的构建呢?提供以下几个思路。
echarts SSR 参考文档:https://echarts.apache.org/handbook/zh/how-to/cross-platform/server/
私有镜像:
docker pull registry.cn-hangzhou.aliyuncs.com/mengweijin/echarts-server:6.0.0.1
Content type
Image
Digest
sha256:a46ff4d49…
Size
155.7 MB
Last updated
3 months ago
docker pull mengweijin/echarts-server:6.1.0.1