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 };
|