riku
2025-09-02 adc9abd145c24f2d3e7033bb738e1e8641eaf4cf
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
import * as echarts from 'echarts';
import { smallLineOption, baseVisualMap } from '@/utils/chart/chart-option';
 
/**
 * 使用echarts生成图表并转换为base64图片
 * @returns {string} 图表的base64编码图片
 */
function generateEchartsImage(params, exceptionIndexArr, yMinInterval) {
  // 1. 创建临时DOM元素
  const div = document.createElement('div');
  // div.style.width = '330px';
  // div.style.height = '160px';
  div.style.width = '800px';
  div.style.height = '400px';
  document.body.appendChild(div);
 
  // 2. 初始化echarts实例
  const myChart = echarts.init(div);
 
  // 3. 准备测试数据
  const { xAxis, series } = params;
  const option = smallLineOption(
    xAxis,
    series,
    yMinInterval,
    'light',
    true,
    false,
    { left: '7%', right: '7%', top: '10%', bottom: '10%' },
    series.name
  );
  if (exceptionIndexArr) {
    const visualMap = baseVisualMap(exceptionIndexArr);
    option.visualMap = visualMap;
  }
 
  // 4. 设置图表配置项
  myChart.setOption(option);
 
  // 5. 将图表转换为base64图片
  const imageBase64 = myChart.getDataURL({
    type: 'png',
    pixelRatio: 2, // 提高图片清晰度
    backgroundColor: '#fff'
  });
 
  // 6. 销毁实例并移除临时DOM
  myChart.dispose();
  document.body.removeChild(div);
 
  return imageBase64;
}
 
export default { generateEchartsImage };