-
Notifications
You must be signed in to change notification settings - Fork 0
/
getChartsBlobImage.js
38 lines (34 loc) · 1.02 KB
/
getChartsBlobImage.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
// 直接生成可插入pdf的图表图片
import * as echarts from 'echarts/core';
import { LineChart } from 'echarts/charts';
import { GridComponent } from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
// 只引入需要用到的图表类型
echarts.use([
CanvasRenderer,
LineChart,
GridComponent
])
// options即echartsInstance.setOptions所用参数
const getChartsBlobImage = (options) => {
const { width, height } = options;
const canvas = window.document.createElement('canvas');
canvas.style.position = 'absolute';
canvas.style.left = '-9999px';
canvas.style.top = '-9999px';
canvas.width = width || 300;
canvas.height = height || 200;
window.document.body.appendChild(canvas);
const chart = echarts.init(canvas, null, {
});
chart.setOption({
...options,
animation: false,
})
const imageBlob = chart.getDataURL({
pixelRatio: 3
});
canvas.remove();
return imageBlob;
};
export default getChartsBlobImage;