forked from oss-compass/compass-echarts-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (102 loc) · 2.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const echarts = require("echarts");
const { createCanvas, registerFont } = require("canvas");
const fs = require("fs");
registerFont("arial.ttf", { family: "arial" });
registerFont("msyhl.ttc", { family: "msyhl" });
module.exports = function (config, res) {
const width = parseInt(config.width, 10) || 500;
const height = parseInt(config.height, 10) || 500;
const canvas = createCanvas(width, height);
echarts.setPlatformAPI(function () {
return canvas;
});
const option = {
title: { text: "test" },
tooltip: {},
legend: { data: ["test"] },
xAxis: { data: ["a", "b", "c", "d", "f", "g"] },
yAxis: {},
series: [{ name: "test", type: "bar", data: [5, 20, 36, 10, 10, 20] }],
};
const defaultConfig = {
width,
height,
option,
enableAutoDispose: true,
};
config = Object.assign({}, defaultConfig, config);
config.option.animation = false;
const context = canvas.getContext("2d");
context.fillStyle = "#000";
context.fillRect(0, 0, width, height);
let chart;
if (config.type === "svg") {
chart = echarts.init(null, null, {
renderer: "svg",
ssr: true,
width: width,
height: height,
});
} else {
chart = echarts.init(canvas);
}
chart.setOption(config.option);
if (config.path) {
try {
fs.writeFileSync(config.path, chart.getDom().toBuffer());
if (config.enableAutoDispose) {
chart.dispose();
}
console.log("Create Img: " + config.path);
} catch (err) {
console.error("Error: Failed to write file " + config.path + ". " + err.message);
}
} else {
if (config.type === "svg") {
renderSvg(chart, res);
} else {
renderCanvas(chart, res);
}
try {
if (config.enableAutoDispose) {
chart.dispose();
}
} catch (e) {}
}
};
function renderCanvas(chart, res) {
const buffer = chart.getDom().toBuffer();
res.setHeader("Content-Type", "image/png");
res.write(buffer);
res.end();
}
function renderSvg(chart, res) {
const svg_data = chart.renderToSVGString();
const filename = Math.random().toString(36).slice(-6);
fs.writeFileSync(filename, svg_data, {
encoding: "utf8",
mode: 0o644,
flag: "w",
});
var stream = fs.createReadStream(filename);
stream.on("open", function () {
res.setHeader("Content-Type", "image/svg+xml");
stream.pipe(res);
});
stream.on("close", function () {
res.end();
fs.unlink(filename, function (err) {
if (err) {
console.error(
"Failed to unlink file " + result.filename + "! " + err.toString()
);
}
});
});
stream.on("error", function (err) {
res.statusCode = 500; // 500 == Internal Server Error
res.setHeader("Content-Type", "text/plain");
res.end("Failed to serve file due to I/O error.\n");
console.error("Failed to serve file " + filename + ": " + err.toString());
});
}