1 Star 0 Fork 3

谢二郎/ssr-echarts

forked from han.yuan/ssr-echarts 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server.js 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
lizheng.lz1998 提交于 2020-10-21 19:12 . first commit
const {createCanvas} = require('canvas')
const echarts = require('echarts')
// 引入主题
require('echarts-themes-js/src/chalk')
require('echarts-themes-js/src/essos')
require('echarts-themes-js/src/halloween')
require('echarts-themes-js/src/infographic')
require('echarts-themes-js/src/macarons')
require('echarts-themes-js/src/purple-passion')
require('echarts-themes-js/src/roma')
require('echarts-themes-js/src/romantic')
require('echarts-themes-js/src/shine')
require('echarts-themes-js/src/vintage')
require('echarts-themes-js/src/walden')
require('echarts-themes-js/src/westeros')
require('echarts-themes-js/src/wonderland')
// 生成图片
function generateImage(options, width, height, theme) {
console.log("GENERATE IMAGE")
console.log(`WIDTH: ${width}`)
console.log(`HEIGHT: ${height}`)
console.log(`THEME: ${theme}`)
console.log(`OPTIONS: ${JSON.stringify(options)}`)
const canvas = createCanvas(width, height)
const ctx = canvas.getContext('2d')
ctx.font = '12px'
echarts.setCanvasCreator(() => canvas)
const chart = echarts.init(canvas, theme)
options.animation = false
chart.setOption(options)
return chart.getDom().toBuffer()
}
const express = require('express')
const app = express()
app.use(express.json())
const imgController = (req, res) => {
console.log("RECV REQ")
let options = req.query.options || req.body
if (typeof options === 'string') {
options = JSON.parse(options)
}
let width = parseInt(req.query.width || options.width || 1024)
let height = parseInt(req.query.height || options.height || 768)
let theme = req.query.theme || options.theme || 'dark'
res.header("Content-Type", "image/png")
res.send(generateImage(options, width, height, theme))
}
app.post('/', imgController)
app.get('/', imgController)
const PORT = process.env.PORT || 3000
app.listen(PORT)
console.log("echarts started port:" + PORT)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xieerlang/ssr-echarts.git
git@gitee.com:xieerlang/ssr-echarts.git
xieerlang
ssr-echarts
ssr-echarts
master

搜索帮助