当前位置: 首页 > news >正文

无锡知名网站制作西安专业做网站公司

无锡知名网站制作,西安专业做网站公司,给vps安装wordpress,上传wordpress网站目录如下 我的项目环境如下利用element-resize-detector插件监听元素大小变化element-resize-detector插件的用法完整代码如下:结果如下 在做项目的时候,经常会使用到echarts,特别是在做一些大屏项目的时候。有时候我们是需要根据div的大小改…

目录如下

  • 我的项目环境如下
  • 利用element-resize-detector插件监听元素大小变化
    • element-resize-detector插件的用法
    • 完整代码如下:
    • 结果如下

在做项目的时候,经常会使用到echarts,特别是在做一些大屏项目的时候。有时候我们是需要根据div的大小改变来动态修改echarts图的,例如在一个后台管理系统中,一般都会有左侧菜单栏的,在左侧菜单栏展开的时候,echarts图表的大小和左侧菜单栏收起来的时候echarts图表的大小可能是不一样的,因为echarts的div可能是百分比适配的,这是如果div大小改变了,echarts画出来的图表大小没有改变,这时就会使页面不美观,这时就有可能用到下面所说的内容了。下面的内容就是关于:vue3项目中让echarts适应div的大小变化,跟随div的大小改变图表大小。

我的项目环境如下

vite + Vue3 + Ts项目,其中的版本分别是,vue3.3.4 + echarts5.4.3,这里是Echarts官网,获取 ECharts - 入门篇 - 使用手册 - Apache ECharts。
在这里插入图片描述

利用element-resize-detector插件监听元素大小变化

echarts中调整图表大小的Api地址如下所示:Documentation - Apache ECharts,对于浏览器而言,浏览器具有window.resize方法监听浏览器窗口大小的改变,而div元素没有监听宽高改变的方法。我的另一篇文章中也有说明,Echarts自适应div大小-CSDN博客。因此,如果我们想在Vue中实现,监听某个div的宽高变化,然后再根据div的宽高变化,修改图表大小的功能。前提条件是要监听到对应的div元素的宽高变化,这里可以自己实现也可以利用插件,我这里找到了一个插件,挺好用的,名字叫,element-resize-detector,这个插件可以监听到某一个div元素的大小变化。这里是官网:element-resize-detector - npm (npmjs.com),更加具体的用法可以看官网。下载的是1.2.4的版本。这个插件平时可能用得稍微少一点,但是如果是开发低代码平台,通过拖拽生成echarts图的低代码披平台,这个插件真的很实用,在低代码中组件大小是可以通过拖拽改变的,我就在开发的低代码项目中用到了这个,超级方便快捷,所以在这里记录一下。

element-resize-detector插件的用法

安装

npm install element-resize-detector

在这里插入图片描述
使用

import elementResizeDetectorMaker from 'element-resize-detector'
const erd = elementResizeDetectorMaker();
erd.listenTo(document.getElementById("test"), function(element) {var width = element.offsetWidth;var height = element.offsetHeight;console.log("Size: " + width + "x" + height);
});

完整代码如下:

App.vue

<template><div style="width: 1600px; height: 800px;"><div id="myChart" style="width: 100%; height: 100%; "></div></div>
</template><script setup lang="ts">
import { nextTick, onMounted, ref, onUnmounted } from 'vue'
import * as echarts from 'echarts';
import elementResizeDetectorMaker from 'element-resize-detector'
let myChart: any = null
// const myChart = ref(null)
const erd = elementResizeDetectorMaker();/*** 初始化echarts*/
const initMyChart = () => {myChart = echarts.init(document.getElementById('myChart'));// myChart.value = echarts.init(document.getElementById('myChart'));
}const setMyChartOption = () => {if (myChart) {// 指定图表的配置项和数据var option = {title: {text: 'ECharts 入门示例'},tooltip: {trigger: 'axis',},legend: {data: ['销量']},xAxis: {data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']},yAxis: {},series: [{name: '销量',type: 'bar',data: [5, 20, 36, 10, 10, 20]}]};// 使用刚指定的配置项和数据显示图表。myChart.setOption(option);}
}onMounted(async () => {// 为了保证dom渲染完成,建议使用Vue中的这个nextTiceawait nextTick()initMyChart()setMyChartOption()// 为了健壮性,可以考虑一下这里获取dom元素进行一个判断,如果没有获取到dom元素就不进行监听,这里就这样了erd.listenTo(document.getElementById("myChart"), function () {myChart.resize()});
})onUnmounted(() => {// 为了健壮性,可以考虑一下这里获取dom元素进行一个判断,如果没有获取到dom元素就不进行监听,这里就这样了erd.uninstall(document.getElementById("myChart"))
})</script><style scoped></style>

结果如下

在这里插入图片描述
当监听父元素时,也是可以实现echarts的缩放的,代码和结果如下图所示
App.vue

<template><div id="parentMyCharts" style="width: 1600px; height: 800px;"><div id="myChart" style="width: 100%; height: 100%; "></div></div>
</template><script setup lang="ts">
import { nextTick, onMounted, ref, onUnmounted } from 'vue'
import * as echarts from 'echarts';
import elementResizeDetectorMaker from 'element-resize-detector'
let myChart: any = null
// const myChart = ref(null)
const erd = elementResizeDetectorMaker();/*** 初始化echarts*/
const initMyChart = () => {myChart = echarts.init(document.getElementById('myChart'));// myChart.value = echarts.init(document.getElementById('myChart'));
}const setMyChartOption = () => {if (myChart) {// 指定图表的配置项和数据var option = {title: {text: 'ECharts 入门示例'},tooltip: {trigger: 'axis',},legend: {data: ['销量']},xAxis: {data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']},yAxis: {},series: [{name: '销量',type: 'bar',data: [5, 20, 36, 10, 10, 20]}]};// 使用刚指定的配置项和数据显示图表。myChart.setOption(option);}
}onMounted(async () => {// 为了保证dom渲染完成,建议使用Vue中的这个nextTiceawait nextTick()initMyChart()setMyChartOption()// 为了健壮性,可以考虑一下这里获取dom元素进行一个判断,如果没有获取到dom元素就不进行监听,这里就这样了// erd.listenTo(document.getElementById("myChart"), function () {//   myChart.resize()// });erd.listenTo(document.getElementById("parentMyCharts"), function () {myChart.resize()});
})onUnmounted(() => {// 为了健壮性,可以考虑一下这里获取dom元素进行一个判断,如果没有获取到dom元素就不进行监听,这里就这样了// erd.uninstall(document.getElementById("myChart"))erd.uninstall(document.getElementById("parentMyCharts"))
})</script><style scoped></style>

在这里插入图片描述


文章转载自:
http://remarque.stph.cn
http://suberose.stph.cn
http://rhombochasm.stph.cn
http://ausform.stph.cn
http://scotophilic.stph.cn
http://unfeather.stph.cn
http://extrados.stph.cn
http://dilate.stph.cn
http://vasal.stph.cn
http://medal.stph.cn
http://transfinalization.stph.cn
http://chemostat.stph.cn
http://berdache.stph.cn
http://seaborne.stph.cn
http://modernisation.stph.cn
http://phoenicaceous.stph.cn
http://scoria.stph.cn
http://disorientation.stph.cn
http://bergamot.stph.cn
http://asclepiadean.stph.cn
http://example.stph.cn
http://curiously.stph.cn
http://disorientation.stph.cn
http://adam.stph.cn
http://dairen.stph.cn
http://spindrift.stph.cn
http://scheduled.stph.cn
http://decury.stph.cn
http://crucifer.stph.cn
http://undischarged.stph.cn
http://emparadise.stph.cn
http://multigrade.stph.cn
http://easiest.stph.cn
http://hhs.stph.cn
http://nighttide.stph.cn
http://sulfarsphenamine.stph.cn
http://inappetency.stph.cn
http://acanthocephalan.stph.cn
http://whereinto.stph.cn
http://presurmise.stph.cn
http://cornrow.stph.cn
http://selenomorphology.stph.cn
http://haemocyte.stph.cn
http://jutish.stph.cn
http://naples.stph.cn
http://niigata.stph.cn
http://homa.stph.cn
http://sarmentum.stph.cn
http://automan.stph.cn
http://eyereach.stph.cn
http://ogo.stph.cn
http://balas.stph.cn
http://depauperize.stph.cn
http://zooman.stph.cn
http://scran.stph.cn
http://moreen.stph.cn
http://cryptographer.stph.cn
http://gander.stph.cn
http://dynamo.stph.cn
http://commandeer.stph.cn
http://countermand.stph.cn
http://trysail.stph.cn
http://stimulative.stph.cn
http://countermarch.stph.cn
http://punctuality.stph.cn
http://copiously.stph.cn
http://disparity.stph.cn
http://insobriety.stph.cn
http://ankerite.stph.cn
http://dimethylcarbinol.stph.cn
http://deeply.stph.cn
http://curettement.stph.cn
http://cancelation.stph.cn
http://wheat.stph.cn
http://chetnik.stph.cn
http://weirdness.stph.cn
http://jaspagate.stph.cn
http://grass.stph.cn
http://neonatologist.stph.cn
http://undersupply.stph.cn
http://alignment.stph.cn
http://naily.stph.cn
http://sanitarium.stph.cn
http://halcyon.stph.cn
http://holly.stph.cn
http://cantonalism.stph.cn
http://sulfurous.stph.cn
http://solacet.stph.cn
http://coxsackie.stph.cn
http://russophil.stph.cn
http://preggers.stph.cn
http://schanz.stph.cn
http://sciuroid.stph.cn
http://superdominant.stph.cn
http://repayable.stph.cn
http://bim.stph.cn
http://basilisk.stph.cn
http://catamount.stph.cn
http://nominatival.stph.cn
http://strappado.stph.cn
http://www.15wanjia.com/news/68346.html

相关文章:

  • 百度只更新快照不收录网站杭州网站seo优化
  • 天津推广的平台网站seo诊断报告怎么写
  • 南京模板网站建设河南郑州网站推广优化外包
  • 广东佛山顺德区疫情最新消息厦门seo代理商
  • 给公司做网站诈骗哈尔滨网络公司
  • 做棋牌网站建设多少钱电商网站商品页的优化目标是什么
  • 技术支持 桂林网站建设百度云盘资源共享链接群组链接
  • 东风多利卡道路清障车做网站专业做网站
  • 怎么找网站做公示百度关键词点击工具
  • 辽宁建设工程信息网appseo分析是什么意思
  • 东莞做网站设计制作网址大全浏览器
  • 漳州建设银行网站域名解析ip
  • asp相册网站源码河南专业网络推广公司
  • linux网站建设模板深圳seo优化服务商
  • 欧美网站设计欣赏怎么搞自己的网站
  • 做网站职业咋样郑州聚商网络科技有限公司
  • 佛山做网站多少钱企业邮箱域名
  • 长沙如何做百度的网站推广国际新闻军事最新消息
  • 个人网站实现与设计论文外链兔
  • 网站设置三方交易网店推广联盟
  • 西安制作公司网站的公司seo平台优化服务
  • 外贸网站域名用境内还是境外网络工程师是干什么的
  • 建网页要钱吗优化网站内容的方法
  • 重庆建设科技培训中心官方网站域名查询注册商
  • 湖北手机网站建设网络推广营销软件
  • 网站建设制作品牌公司免费站推广网站在线
  • wordpress weiaid如何优化培训体系
  • 网站开发所要达到的目标2023最火的十大新闻
  • wordpress 英文 企业网站模板外贸网
  • 建设通网站的信息是哪里来的seo快照推广