forked from hellosean1025/node-echarts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (113 loc) · 2.92 KB
/
index.js
File metadata and controls
129 lines (113 loc) · 2.92 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var echarts = require("echarts");
var { createCanvas } = require("canvas");
var fs = require('fs');
var path = require('path');
/**
* default echart option in case the client doesn't define
*/
function getDefaultOption() {
return {
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],
}]
};
}
/**
* default config for the option, in case the client doesn't
* define it.
*
* @param {*} option echart option
*/
function getDefaultConfig(option) {
return {
width: 500,
height: 500,
option,
enableAutoDispose: true,
}
}
/**
* saves the chart passed in the provided path
* @param {*} chart chart in which thedom will be retrieved and saved
* as an image
* @param {*} path directory to the image that will be saved
*/
function saveChart(chart, path) {
try {
fs.writeFileSync(path, chart.getDom().toBuffer());
console.log("Created image:" + path)
} catch (err) {
console.error("Error: write file failed: " + err.message)
}
}
/**
* retrieves the buffer of the chart
* @param {*} chart chart in which the buffer will be retrieved
*/
function getBuffer(chart) {
return chart.getDom().toBuffer();
}
/**
* @param config = {
width: 500 // Image width, type is number.
height: 500 // Image height, type is number.
option: {}, // Echarts configuration, type is Object.
//If the path is not set, return the Buffer of image.
path: '', // Path is filepath of the image which will be created.
}
*
*/
module.exports = function (config) {
const createdCanvas = createCanvas(128, 128);
const ctx = createdCanvas.getContext('2d');
if (config.font) {
ctx.font = config.font;
}
echarts.setCanvasCreator(function () {
return createdCanvas;
});
const option = getDefaultOption();
const defaultConfig = getDefaultConfig(option);
config = Object.assign({}, defaultConfig, config);
config.option.animation = false;
const chart = echarts.init(
createCanvas(
parseInt(config.width, 10),
parseInt(config.height, 10),
),
);
chart.setOption(config.option);
try {
/**
* if specified the path, the chart will be saved into it
*/
if (config.path) {
saveChart(chart, config.path);
return;
}
/**
* if not, the buffer will be returned
*/
return getBuffer(chart);
} finally {
/**
* finally, dispose the chart if client defined so
*/
if (config.enableAutoDispose) {
chart.dispose();
}
}
}