项目背景:

  需要使用echarts进行图表展示。由于div宽高是不固定的,因此需要先获取父级的宽高再把值赋予到图表的div中。

  需要使用 this.$nextTick(() => {    });方法,在mounted中,保证DOM渲染完全后,在进行echarts初始化。

<!--wifi数据采集-->
<template>
<div class="wifiCollectCtrl">
<div id="wifiCollectID"></div>
</div>
</template> <script>
import echarts from 'echarts'; export default {
name: "wifiCollect",
props: {
className: {type: String, default: 'chart'},
id: {type: String, default: 'wifiCollectID'}, },
data() {
return {
chart: null,
seriesData: '',
xAxisTxt: '',
color: [
'#3D89FE', '#9bca62', '#2ec7c9', '#b6a2de', '#5ab1ef',
'#8d98b3', '#e5cf0d', '#97b552', '#95706d', '#dc69aa',
'#07a2a4', '#9a7fd1', '#588dd5', '#f5994e', '#c05050',
'#59678c', '#c9ab00', '#7eb00a', '#6f5553', '#c14089'
]
};
},
mounted() {
this.$nextTick(() => { //使用nextTick为了保证dom元素都已经渲染完毕
this.initChart();
});
},
beforeDestroy() {
if (!this.chart) {
return
}
this.chart.dispose();
this.chart = null;
},
created() { },
methods: {
initChart() {
$("#wifiCollectID").width($(".wifiCollectCtrl").width());
$("#wifiCollectID").height($(".wifiCollectCtrl").height());
this.chart = echarts.init(document.getElementById(this.id));
this.chart.setOption({
color: this.color,
title: {
text: '',
},
calculable: true,
grid: {
left: '50px',
right: '3%',
bottom: '20px',
top: '15px'
},
xAxis: [
{
type: 'category',
// data : this.xAxisTxt,
data: ['花园小区', '锦园小区', '云台小区', '教师宿舍区'],
splitLine: {show: false},//去除网格线
axisLine: {
lineStyle: {
color: '#3D89FE',
width: 1
}
},
axisLabel: {
show: true,
textStyle: {
color: '#fff'
}
}
}
],
yAxis: [
{
type: 'value',
splitLine: {//去除网格线
show: true,
lineStyle: {
color: ['#1F4781'],
width: 1,
}
},
axisLine: {
lineStyle: {
color: '#3D89FE',
width: 1
}
},
axisLabel: {
show: true,
textStyle: {
color: '#fff'
}
}
}
],
series: [
{
name: '所属小区',
type: 'bar',
barMaxWidth: '30',
// data:this.seriesData,
data: [20, 340, 40, 39]
} ]
})
}
} }
</script> <style scoped>
.wifiCollectCtrl {
color: #333;
padding: 5px;
width: 100%;
height: 100%;
position: relative;
}
</style>

  

vue 动态获取div宽高有时候为0的情况的更多相关文章

  1. 微信小程序之动态获取元素宽高

    我以前一直以为微信小程序不能动态获取view元素的宽高.但是自从看到: wx.createSelectorQuery() 这个api接口,以前的某些问题就能得到解决了... 那么,这个api接口怎么用 ...

  2. Vue中获取元素宽高

    <div ref="init"></div> 写在 页面 方法 部分 这里的 offsetHeight 是返回元素的宽度(包括元素宽度.内边距和边框,不包括 ...

  3. js获取图片信息(二)-----js获取img的height、width宽高值为0

    首先,创建一个图片对象: var oImg= new Image(); oImg.src = "apple.jpg"; 然后我们打印一下图片的信息: console.log(oIm ...

  4. vue 获取屏幕宽高 width height

    /**  * 获取屏幕宽高  */ Vue.prototype.getViewportSize = function(){   return {     width: window.innerWidt ...

  5. JS快速获取图片宽高的方法

    快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器. 我们一步一 ...

  6. 动态布局--动态修改RelativeLayout宽高的方法

    本文实例讲述了Android编程动态修改RelativeLayout宽高的方法.分享给大家供大家参考,具体如下: 我们经常会动态修改RelativeLayout的宽高,这样的代码,比较简单,就是修改R ...

  7. 转载:JS快速获取图片宽高的方法

    快速获取图片的宽高其实是为了预先做好排版样式布局做准备,通过快速获取图片宽高的方法比onload方法要节省很多时间,甚至一分钟以上都有可能,并且这种方法适用主流浏览器包括IE低版本浏览器. 我们一步一 ...

  8. iOS根据文字字数动态确定Label宽高

    我们有时候在写项目的时候,会碰到,意见反馈,还有其他地方,讲座活动细则等需要大篇展示的文本, 因为每次服务器返回的内容大小不一,所以需要动态的调整label的宽高: 在ios 6 的时候可以: -(v ...

  9. 写个js动态调整图片宽高 (原创)

    <body style="TEXT-ALIGN: center;"> <div id="testID" style="backgro ...

随机推荐

  1. HDU 3987 && DINIC

    很容易发现是网络流的题目,但最少边怎么求呢?初时想不到,但画图后忽然发现可以这样: 求一次网络流最小割后,把满流的边置1,不满流的置INF.再求一次最大流即可. 为什么呢? 是否会存在一些边当前不满流 ...

  2. POJ 1679 The Unique MST(推断最小生成树_Kruskal)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique.  Defini ...

  3. adb命令--之查看进程及Kill进程

    adb shell kill [PID]       //杀死进程 adb 命令查看程序进程方便简洁高效 adb shell ps       //查看所有进程列表,Process Status ad ...

  4. 【POJ 2096】 Collecting Bugs

    [题目链接] http://poj.org/problem?id=2096 [算法] 概率DP [代码] #include <algorithm> #include <bitset& ...

  5. Springboot 之 引入Thymeleaf

    转自:https://segmentfault.com/a/1190000011149325 前言 Spring-boot-starter-web集成了Tomcat以及Spring MVC,会自动配置 ...

  6. vue插件 vue-seamless-scroll 无缝滚动插件ES6使用总结

    最近因为需求需要写一个项目信息无缝向上滚动组件,在网上搜了一下,看到大家的一致好评就果断的使用了vue-seamless-scroll组件.下面就简单的介绍它的使用,具体详细的使用推荐大家去看下开发者 ...

  7. Css float 盒子模型 position

    属性: float 浮动 浮动的内容用div包起来,给div设置宽高 clear 清除浮动. box-sizing 标准模式下的盒模型 content-box:(默认属性) padding和borde ...

  8. 织梦dedecms红黑配图片模板源码v2.0

    dedecms红黑配风格美女图片站是采用dedecms程序搭建的图片网站源码,网站感觉很大气,简约但是不简单,适合做图片网站.网站模板是收集其他网站的模板,感谢原网站提供者.在安装过程中出现问题,现已 ...

  9. 关于iscroll.js插件的使用

    iscroll 作用: 可以让区域滚动效果好看一些 使用: 1. html结构 外面必须包一层盒子,切内部的元素要尽量简单,不然会影响滚动效果 <div id="wrapper&quo ...

  10. oracle 编译包的时候,一直提示正在编译

    select b.sid,b.serial#,b.machine,b.terminal,b.program,b.process,b.status from v$lock a , v$session b ...