封装了一个echarts图全屏放大的功能

news/2025/3/16 5:40:40/

封装的js文件

import * as echarts from 'echarts'
/*** el:目标渲染元素* option:echarts配置项* resize:是否需要全屏功能*/export const initEcharts = (el, option, resize = false) => {const chartDom = el;const myChart = echarts.init(chartDom);let num = 0//全屏放大功能,增加全屏按钮,点击全屏按钮(自己添加)传递resize参数=true就行if (resize) {myChart.resize()const resizeObserver = new ResizeObserver(entries => {num++if (num >= 2) {myChart.resize()if (!document.webkitIsFullScreen) {num = 0setTimeout(() => {resizeObserver.disconnect()}, 50)}}});resizeObserver.observe(el)}myChart.clear()myChart.setOption(option);
}export const screenFull = e => {if (e.requestFullscreen) {e.requestFullscreen();} else if (e.mozRequestFullScreen) {e.mozRequestFullScreen();} else if (e.webkitRequestFullscreen) {e.webkitRequestFullscreen();} else if (e.msRequestFullscreen) {e.msRequestFullscreen();}
};

使用

1、initEcharts接收三个参数,参一为echarts渲染的目标元素
参二为echarts渲染的配置项
参三控制是否需要全屏放大的相关功能

2、screenFull 为控制某个元素放大的按钮,传递需要放大的元素即可

<template><div class="main-pie"><button @click="fn">点击此按钮进入全屏(根据需求可以换成icon的形式,搭配hover)</button><div class="main-echarts" ref="main-echarts"></div></div>
</template>
<script>import { initEcharts, screenFull } from "@/utils/initEcharts.js";export default {data() {return {option2: {tooltip: {trigger: "item"},legend: {type: "scroll",orient: "vertical",right: "center",top: 20,bottom: 20},series: [{type: "pie",radius: "90%",left: "50%",itemStyle: {borderColor: "#fff",borderWidth: 2},data: [{value: 1548,name: "CityE"},{ value: 735, name: "基础治疗" },{ value: 510, name: "挂号" },{ value: 434, name: "正畸形" },{ value: 335, name: "修复类" },{ value: 335, name: "种植类" }]},{itemStyle: {borderColor: "#fff",borderWidth: 2},type: "pie",radius: "90%",right: "50%",data: [{value: 1548,name: "CityE"},{ value: 735, name: "基础治疗" },{ value: 510, name: "挂号" },{ value: 434, name: "正畸形" },{ value: 335, name: "修复类" },{ value: 335, name: "种植类" }]}]}};},props: {},watch: {},mounted() {this.initMainEcharts();},created() {},methods: {initMainEcharts(resize = false) {let el = this.$refs["main-echarts"];initEcharts(el, this.option2, resize);},fn() {screenFull(this.$refs["main-echarts"]);//全屏放大,需要等待容器缩放完成后,重绘echarts图像setTimeout(() => {this.initMainEcharts(true);}, 50);}}};
</script>
<style lang="scss" scoped>.main-pie {li {list-style: none;display: flex;align-items: center;padding: 6px;cursor: pointer;white-space: nowrap;> div {width: 10%;height: 10px;margin-right: 10px;}}.title-echarts {width: 100%;height: 80px;display: flex;align-items: center;justify-content: space-between;p {text-align: center;width: 42%;font-size: 18px;}}width: 100%;> .main-echarts {background-color: white;height: calc(100vh - 270px);}.main-echarts {width: 100%;}p {padding: 0;margin: 0;color: #000000;}}
</style>

http://www.ppmy.cn/news/53429.html

相关文章

最近部门新的00后真是卷王,工作没1年,入职18K

都说00后躺平了&#xff0c;但是有一说一&#xff0c;该卷的还是卷。 这不&#xff0c;前段时间我们公司来了个00后&#xff0c;工作都没1年&#xff0c;到我们公司起薪18K&#xff0c;都快接近我了。后来才知道人家是个卷王&#xff0c;从早干到晚就差搬张床到工位睡觉了。 …

【5天打卡】学习Lodash的第二天——常用api学习

文接上篇,【5天打卡】学习Lodash的第一天——初体验 大家好,最近,我在学习Lodash这个工具库。Lodash最初是 Underscore 的分支,后来逐渐壮大后自立门户。Lodash 功能比 Underscore 更丰富,且 Underscore 已有3、4年没有更新,所以推荐使用 Loadash。但是和原生相比还是推…

【数据结构】二叉树的遍历

Yan-英杰的主页 悟已往之不谏 知来者之可追 C程序员&#xff0c;2024届电子信息研究生 目录 前序、中序以及后序遍历 前序遍历 中序遍历 后序遍历 前序、中序以及后序遍历 学习二叉树结构&#xff0c;最简单的方式就是遍历。所谓二叉树遍历 (Traversal) 是按照某种特定…

ThingsBoard教程(三六):规则节点解析 检查关系节点 check relation,实体类型节点 entity type

前言 本篇文章和大家一起来学习两个节点,检查关系节点 check relation和实体类型节点 entity type。 check relation 检查消息的发起者与其他实体之间是否存在关系。如果选择了“check relation to specific entity(检查与特定实体的关系)”,则必须指定相关实体。否则,…

微服务---分布式事务Seata(XA,AT,TCC,SAGA模式基本使用)

分布式事务 1.分布式事务问题 1.1.本地事务 本地事务&#xff0c;也就是传统的单机事务。在传统数据库事务中&#xff0c;必须要满足四个原则&#xff1a; 1.2.分布式事务 分布式事务&#xff0c;就是指不是在单个服务或单个数据库架构下&#xff0c;产生的事务&#xff0c…

SqlSugar操作MySQL数据库

SqlSugar操作MySQL数据库 C#操作DataTable排序 在C#中&#xff0c;我们可以使用DataTable类来表示内存中的数据表格。DataTable类提供了各种方法和属性来操作数据表格&#xff0c;包括排序。 要对DataTable进行排序&#xff0c;可以使用DataView类。DataView类是一个用于筛选…

收废品小程序开发中的常见问题及解决方法

常见问题 1. 用户界面设计 小程序的用户界面设计至关重要。设计师需要在用户界面中提供清晰的指示&#xff0c;以便用户可以轻松地找到他们需要的功能。同时&#xff0c;设计师还需要确保用户界面的整体风格与公司的品牌形象相符。 2. 功能开发 开发小程序的功能需要考虑到…

如何掌握PMO核心技能和知识?

想成为PMO大神&#xff0c;但不知道该从哪里入手&#xff1f;别慌&#xff01;按照步骤&#xff0c;一级级往上跳&#xff0c;学习项目管理的核心技能和知识。 一、项目管理流程 1、熟悉和理解项目管理的基本原则、流程和方法。 2、确保项目计划符合业务目标和价值。 3、确…