echarts 图表/SVG 图片指定位置截取

echarts 图表/SVG 图片指定位置截取

需求:如下图所示,需要固定头部legend信息

1.前期准备

echarts dom渲染容器

html 复制代码
<div :id="'barchart' + id" class="charts" ref="barchart"></div>
  1. echart 图表渲染
  2. 取图表的echarts图片传给父组件

为什么使用setTimeout :

原因是因为 防止 dom还没渲染完,echarts执行完了,会导致echarts空白的问题

myChart 为全局变量 是防止vue实时监听参数,导致浏览器卡顿或者崩溃

javascript 复制代码
setTimeout(() => {
this.chartToBase64(myChart[this.id]).then((base64) => {
     const direction = yAxis[0]?.type == "value" ? "l" : "p"; //PDF方向
	const width = this.$refs.barchart.offsetWidth; //宽
	const height = this.$refs.barchart.offsetHeight; //高
	this.$emit("getBottomImag", { canvas: base64, width, height,direction })
})
},200)

// 将图表转换为 base64 字符串
chartToBase64(chart) {
	return new Promise((resolve, reject) => {
		chart.on("finished", () => {
			resolve(chart.getDataURL({ backgroundColor: "#fff" }));
		});
	});
},

2.图片截取

  1. 父组件中创建好所需dom
html 复制代码
<!-- 截取top bottom 图表图片 -->
<img :src="workbookTopImg" ref="topimgRef" style="position: absolute; background: #fff" v-show="workbookTopImg" />
<img :src="workbookBottomImg" ref="bottomimgRef" style="position: absolute; background: #fff" v-show="workbookBottomImg" />
<canvas ref="canvastop" style="visibility: hidden"></canvas>
<canvas ref="canvasbottom" style="visibility: hidden"></canvas>
javascript 复制代码
//截取图表指定位置大小
getBottomImag(data) {
	const { width, height, direction, canvas} = data;
	const { offsetWidth } = this.$refs.tempRef;//获取父组件的最外层div宽度
	//使导出的图表从最头开始
	document.documentElement.scrollTop = 0;
	document.body.scrollTop = 0;
	//SVG 裁剪
	// 使用base64data进行后续操作
	let imgSrc = base64data;
	
	let img = new Image();
	const _this = this;
	let top = "";
	img.onload = async () => {
		await _this.$nextTick();	
		const imgTopHeight = 80;
		//css 属性设定
		_this.$refs.topimgRef.style.width = `${width}px`;
		_this.$refs.topimgRef.style.height = `${imgTopHeight}px`;
		_this.$refs.topimgRef.style.top = "70px";
		_this.$refs.topimgRef.style.left = "10px";
	
		top = {
			canvas: img,
			sourceX: 0,
			sourceY: 0,
			desiredWidth: width,
			desiredHeight: imgTopHeight,
			type: "top",
		};
		_this.workbookTopImg = await _this.cutSvgImg(top);
	
		//用完图片后 释放缓存
		img.style.display = "none";
		document.body.removeChild(img);
		img = null;
	};
	img.src = imgSrc; // 设置源路径会开始加载图像
	document.body.appendChild(img);
},
  1. 裁取图片
javascript 复制代码
//裁剪svg图片
/*canvas:裁剪的svg图片
    sourceX :裁剪起点
    sourceY:裁剪终点
    desiredWidth:裁剪宽度
    desiredHeight:裁剪高度
返回结果:裁剪后的图片 base64码
  */
async cutSvgImg({ canvas, sourceX, sourceY, desiredWidth, desiredHeight, type }) {
	// 创建一个新的 canvas 来存储裁剪结果
	let croppedCanvas = this.$refs["canvas" + type];
	croppedCanvas.width = desiredWidth;
	croppedCanvas.height = desiredHeight;
	let croppedCtx = croppedCanvas.getContext("2d");
	// 在裁剪 canvas 上绘制裁剪的图像部分
	croppedCtx.drawImage(canvas, sourceX, sourceY, desiredWidth, desiredHeight, 0, 0, desiredWidth, desiredHeight);
	const base64 = croppedCanvas.toDataURL("image/png");
	// 清除canvas内容
	croppedCtx.clearRect(0, 0, croppedCanvas.width, croppedCanvas.height);
	// 将裁剪结果转换为 data URL
	return base64;
},

3.关于drawImage参数

  1. image: 要绘制的图像,可以是图片或者canvas对象。
  2. sx: 图像剪切的起始X坐标。
  3. sy: 图像剪切的起始Y坐标。
  4. swidth: 图像剪切的宽度。
  5. sheight: 图像剪切的高度。
  6. x: 绘制图像的起始X坐标。
  7. y: 绘制图像的起始Y坐标。
  8. width: 绘制图像的宽度。
  9. height: 绘制图像的高度。
相关推荐
名字还没想好☜36 分钟前
Next.js App Router 数据获取:fetch 缓存、revalidate 与 dynamic 一次讲清
前端
成都渲染101云渲染66665 小时前
如何在3ds Max中实现更快、更高质量的渲染
前端·javascript·人工智能
闲猫7 小时前
Spring AI 对接Deepseek ChatModel 聊天对话
java·前端·spring
paopaokaka_luck8 小时前
英语单词学习系统的设计与实现(基于艾宾浩斯遗忘曲线的智能复习机制、语音朗读、多题型测试与错题自动收录、Echarts图形化分析)
vue.js·spring boot·后端·echarts
自信的未来9 小时前
JSON 工具|Web Worker 工程化打包 + 语法自动修复 + 多语言代码生成实战
java·前端·json
SRET9 小时前
Mineradio 沙盒隔离安装完全指南 | 一键安全运行 Electron 应用!!!
javascript·经验分享·安全·electron·aigc·音视频·ai编程
全栈前端老曹9 小时前
【MongoDB】安全与权限管理 —— 用户认证、角色权限、SSL 加密
前端·javascript·数据库·安全·mongodb·nosql·ssl
zhangxingchao10 小时前
AI大模型核心八:从 Agent Skill、长文档 RAG 到知识库更新与训练策略
前端·人工智能·后端
zhangxingchao10 小时前
AI大模型核心七:从 Workflow、RAG、记忆治理到幂等性
前端·人工智能·后端
gyx_这个杀手不太冷静11 小时前
Agent开发进阶指南(第 1 章):AI Agent 到底是什么?
前端·agent·ai编程