项目中用到了echarts,并且页面是自适应的,且页面中有一个【放大、缩小】功能,因此图表还需要根据盒子的大小来变化。

即:两个需求,如下:

  ① 图表根据窗口的大小自适应

  ② 图表根据所在盒子的大小自适应

1、窗口大小自适应好说,可以通过

  1. let myChart = This.$echarts.init(document.getElementById(This.id));
  2. window.addEventListener('resize',() => { myChart.resize(); });
  3.  
  4.  
  5. window.onresize = myChart.resize;

注意:如果是一个页面中插入多个echarts图,那么需要用第一种方式来监听resize事件,即:window.addEventListener,若选用第二种方法只能监听一个echarts图的自适应。区别在于addEventListener与on上,有兴趣可以自行查阅。

  

2、按理来说上面的代码对盒子大小的改变,图表自适应也是好使的,但就是出现了问题,而且各种给盒子添加监听也没有好使,所以就想了一个极端点儿的方法,如下,通过延时的方式进行加载

  1. let timer;
  2. clearTimeout(timer);
  3. timer = setTimeout(() => {
  4. let myChart = this.$echarts.init(document.getElementById('myChart'));
  5. myChart.resize();
  6. }, 10);

  这里时间定义的是10毫秒,从用户的角度来看是根本感觉不出来的,而且没有出现图表卡顿的地方,暂时搞定,有好的解决办法了再来重写

以下是全部代码:

  1. <template>
  2. <div class="comprehensiveAnalysis">
  3. <!-- 调度机构 -->
  4. <div class="dispatchOrgan">
  5. <span class="title">调度机构</span>
  6. <ul class="navList">
  7. <li v-for="(item, index) in dispatchOrgan" :key="index" :class="{'active' : currentIndex == index}" :data-id="item.id" @click="chooseOragn(index)">{{item.title}}</li>
  8. </ul>
  9. </div>
  10.  
  11. <!-- 综合分析 -->
  12. <ul class="synthesizeAnalysis">
  13. <!-- 全网装机规模统计 -->
  14. <li>
  15. <div class="cnt" id="ancestors" ref="ancestors" :class="{'full' : isFullScreen}">
  16. <!-- <h3 class="title">全网装机规模统计</h3> -->
  17. <div class="titleOperate">
  18. <span class="title">全网装机规模统计</span>
  19. <ul class="operateWrapper">
  20. <li title="切换表格数据"><span class="tableBtn" @click="changeTableData"></span></li>
  21. <li title="切换折线图"><span class="lineBtn" @click="changeCharts('line')"></span></li>
  22. <li title="切换柱状图"><span class="barBtn" @click="changeCharts('bar')"></span></li>
  23. <li title="切换饼图"><span class="pieBtn" @click="changeCharts('pie')"></span></li>
  24. <li title="放大" v-show="isFullBtn"><span class="fullBtn" @click="fullScreen"></span></li>
  25. <li title="缩小" v-show="isNarrow"><span class="narrowBtn" @click="narrowScreen"></span></li>
  26. <li title="统计提示"><span class="helpBtn"></span></li>
  27. </ul>
  28. </div>
  29. <div class="countShow">装机容量(MW): <span>{{installCapacity}}</span></div>
  30. <div class="chartWrapper">
  31. <div id="myChart" :style="{width: '100%', height: '100%'}"></div>
  32. </div>
  33. </div>
  34. </li>
  35. <!-- 全网电站规模统计 -->
  36. <li>
  37. <div class="cnt">
  38. <h3 class="title">全网电站规模统计</h3>
  39. <div class="chartWrapper"></div>
  40. </div>
  41. </li>
  42. <!-- 排名前十流域装机容量统计 -->
  43. <li>
  44. <div class="cnt">
  45. <h3 class="title">排名前十流域装机容量统计</h3>
  46. <div class="chartWrapper"></div>
  47. </div>
  48. </li>
  49. <!-- 文档规模统计 -->
  50. <li>
  51. <div class="cnt">
  52. <h3 class="title">文档规模统计</h3>
  53. <div class="chartWrapper"></div>
  54. </div>
  55. </li>
  56. </ul>
  57. </div>
  58. </template>
  59.  
  60. <script>
  61. export default {
  62. data() {
  63. return {
  64. isFullBtn: true, // flag:是否显示放大图标
  65. isNarrow: false, // flag:是否显示缩小图标
  66. isFullScreen: false, // flag:小模块是否全屏展示
  67. currentIndex: 0, // 导航添加类名的元素
  68. installCapacity: 0, // 装机容量
  69. stationCount: 0, // 电站数量(座)
  70. installEquipmentName: [], // 全网装机规模 -- 容量数组
  71. nameCapacity: [], // 全网装机规模 -- 容量、名称数组
  72. capacityColor: ['#fef200','#1682ac','#fba644','#67c4e8','#d056ff','#70a442','#bba4ff'], // 可视化图形echarts颜色
  73. // 调度机构tab
  74. dispatchOrgan: [{
  75. id: '002199010000000001',
  76. title: ' 全网'
  77. },{
  78. id: '002199010000000001',
  79. title: '国调直调'
  80. },{
  81. id: '002199010100000001',
  82. title: '华北'
  83. },{
  84. id: '002199010200000001',
  85. title: '华东'
  86. },{
  87. id: '002199010300000001',
  88. title: '华中'
  89. },{
  90. id: '002199010400000001',
  91. title: '东北'
  92. },{
  93. id: '002199010500000001',
  94. title: '西北'
  95. },{
  96. id: '002199010600000001',
  97. title: '西南'
  98. }],
  99. // 全网装机规模
  100. installEquipmentScale: [{
  101. "NAME": "火电厂",
  102. "CODE": 1001,
  103. "CAPACITY": 855291.140000,
  104. "COU": 1132
  105. }, {
  106. "NAME": "水电厂",
  107. "CODE": 1002,
  108. "CAPACITY": 214782.470000,
  109. "COU": 603
  110. }, {
  111. "NAME": "风电场",
  112. "CODE": 1004,
  113. "CAPACITY": 145876.400000,
  114. "COU": 786
  115. }, {
  116. "NAME": "光伏发电站",
  117. "CODE": 1005,
  118. "CAPACITY": 115118.670000,
  119. "COU": 437
  120. }, {
  121. "NAME": "核电站",
  122. "CODE": 1003,
  123. "CAPACITY": 27687.600000,
  124. "COU": 10
  125. }, {
  126. "NAME": "抽水蓄能电站",
  127. "CODE": 1006,
  128. "CAPACITY": 19379.200000,
  129. "COU": 22
  130. }, {
  131. "CAPACITY": 39019.91,
  132. "COU": 2885,
  133. "CODE": "1009",
  134. "NAME": "其他"
  135. }],
  136. }
  137. },
  138. created() {
  139. // 计算:装机容量
  140. this.installEquipmentScale.forEach(item => {
  141. this.installCapacity += item.CAPACITY;
  142. this.installEquipmentName.push(item.NAME);
  143. this.nameCapacity.push({
  144. name: item.NAME,
  145. value: item.CAPACITY
  146. })
  147. });
  148.  
  149. // 装机容量取整
  150. this.installCapacity = parseInt(this.installCapacity);
  151.  
  152. // 计算:电站数量(座)
  153. this.powerStationScale.forEach(item => {
  154. this.stationCount += item.cou;
  155. });
  156.  
  157. // echarts1:全网装机规模
  158. },
  159. mounted() {
  160. this.drawLine();
  161. },
  162. methods: {
  163. // 点击'调度机构'
  164. chooseOragn(index) {
  165. this.currentIndex = index;
  166. },
  167. // 切换表格数据
  168. changeTableData() {
  169. console.log('切换表格数据')
  170. },
  171. // 切换折线图、切换柱状图
  172. changeCharts(type) {
  173. console.log('切换折线图:',type)
  174. },
  175. // 放大
  176. fullScreen() {
  177. this.isFullBtn = false; // flag:是否显示放大图标
  178. this.isNarrow = true; // flag:是否显示缩小图标
  179. this.isFullScreen = true; // flag:小模块是否全屏展示
  180.  
  181. this.changeChartsSize(); // 点击'放大'、'缩小'时改变图表的大小
  182. },
  183. // 缩小
  184. narrowScreen() {
  185. this.isFullBtn = true; // flag:是否显示放大图标
  186. this.isNarrow = false; // flag:是否显示缩小图标
  187. this.isFullScreen = false; // flag:小模块是否全屏展示
  188.  
  189. this.changeChartsSize(); // 点击'放大'、'缩小'时改变图表的大小
  190.  
  191. },
  192. // 点击'放大'、'缩小'时改变图表的大小
  193. changeChartsSize() {
  194. let timer;
  195. clearTimeout(timer);
  196. timer = setTimeout(() => {
  197. let myChart = this.$echarts.init(document.getElementById('myChart'));
  198. myChart.resize();
  199. }, 10);
  200. },
  201. // 绘图
  202. drawLine() {
  203. // 基于准备好的dom,初始化echarts实例
  204. let myChart = this.$echarts.init(document.getElementById('myChart'));
  205. // 绘制图表 -- pie 饼状图
  206. myChart.setOption({
  207. color: this.capacityColor,
  208. tooltip : { // 提示框组件
  209. trigger: 'item', // item:数据项图形触发,散点图、饼图等无类目轴,axis:坐标轴触发,柱状图、折线图,none:不触发
  210. // formatter: "{a} <br/>{b} : {c} ({d}%)"
  211. },
  212. legend: { // 图例组件:展现了不同系列的标记、颜色和名字。可以通过点击图例控制哪些系列不显示
  213. type: 'plain', // scroll:可滚动翻页的图例,图例较多时使用,plain:普通图例
  214. orient: 'vertical', // 图例列表的布局朝向,vertical:垂直,horizontal:水平
  215. right: 10, // 图例组件离容器【右侧】的距离
  216. top: 100, // 图例组件离容器【上侧】的距离
  217. data: this.installEquipmentName,
  218. textStyle: { // 图例的公用文本样式
  219. color: '#fff'
  220. }
  221. },
  222. series : [ // 系列列表,每个系列通过type决定自己的图表类型
  223. {
  224. type: 'pie', // line:折线/面积图,bar:柱状/条形图,pie:饼图,scatter:散点(气泡)图,radar:雷达图,
  225. radius : '60%', // 饼图的半径,'60%':外半径为可视区域尺寸的60%
  226. center: ['50%', '50%'], // 饼图的中心坐标,横/纵坐标
  227. data: this.nameCapacity,
  228. itemStyle: { // 图形样式
  229. emphasis: { // 高亮的扇区和标签样式
  230. shadowBlur: 10,
  231. shadowOffsetX: 0,
  232. shadowColor: 'rgba(0, 0, 0, 0.5)'
  233. }
  234. }
  235. }
  236. ]
  237. })
  238.  
  239. window.onresize = myChart.resize;
  240. }
  241. }
  242. }
  243. </script>
  244.  
  245. <style lang="scss" type="text/css">
  246. @import '../../common/scss/vars.scss';
  247. .comprehensiveAnalysis {
  248. width: 100%;
  249. height: 100%;
  250. .dispatchOrgan {
  251. padding-left: 1px;
  252. height: 24px;
  253. line-height: 24px;
  254. box-sizing: border-box;
  255. font-size: 0;
  256. .title {
  257. margin-right: 12px;
  258. display: inline-block;
  259. width: 75px;
  260. height: 24px;
  261. background: url('./xdbg.png') no-repeat;
  262. text-align: center;
  263. font-size: 14px;
  264. color: $dispatchFontColor;
  265. }
  266. .navList {
  267. display: inline-block;
  268. >li {
  269. margin-right: 5px;
  270. display: inline-block;
  271. width: 66px;
  272. height: 24px;
  273. font-size: 14px;
  274. color: $whiteFontColor;
  275. text-align: center;
  276. border: 1px solid transparent;
  277. box-sizing: border-box;
  278. cursor: pointer;
  279. &.active {
  280. color: $activeFontColor;
  281. background-color: $dispatchBgColor;
  282. border: 1px solid $dispatchBorderColor;
  283. }
  284. }
  285. }
  286. }
  287. .synthesizeAnalysis {
  288. height: calc(100% - 24px);
  289. >li {
  290. float: left;
  291. position: relative;
  292. padding: 12px 6px 0;
  293. width: 50%;
  294. height: 50%;
  295. box-sizing: border-box;
  296. &::before, &::after {
  297. content: '';
  298. position: absolute;
  299. top: 6px;
  300. width: 32px;
  301. height: 17px;
  302. }
  303. &::before {
  304. left: 0px;
  305. background: url('./lefconer.png') no-repeat;
  306. }
  307. &::after {
  308. right: 0px;
  309. background: url('./rigconer.png') no-repeat;
  310. }
  311. >.cnt {
  312. position: relative;
  313. width: 100%;
  314. height: 100%;
  315. background-color: $statisticsBgColor;
  316. border: 1px solid $statisticsBorderColor;
  317. border-top-color: $statisticsBdTopColor;
  318. box-sizing: border-box;
  319. &.full {
  320. position: fixed;
  321. top: 0;
  322. left: 0;
  323. z-index: 999;
  324. }
  325. >.titleOperate {
  326. padding: 10px;
  327. height: 50px;
  328. line-height: 30px;
  329. color: $whiteFontColor;
  330. box-sizing: border-box;
  331. >.title {
  332. float: left;
  333. font-size: 14px;
  334. background: url('./light.png') no-repeat center bottom;
  335. background-size: contain;
  336. }
  337. >.operateWrapper {
  338. float: right;
  339. display: flex;
  340. padding-top: 3px;
  341. >li {
  342. flex: 1;
  343. margin: 0 3px;
  344. width: 24px;
  345. height: 24px;
  346. cursor: pointer;
  347. background: url('../../images/into.png') no-repeat center center;
  348. &:hover,&.hover {
  349. background: url('../../images/hover.png') no-repeat center center;
  350. }
  351. >span {
  352. display: block;
  353. width: 100%;
  354. height: 100%;
  355. &.tableBtn {
  356. background: url('../../images/table.png') no-repeat center center;
  357. }
  358. &.lineBtn {
  359. background: url('../../images/chart_line.png') no-repeat center center;
  360. }
  361. &.barBtn {
  362. background: url('../../images/chart_bar.png') no-repeat center center;
  363. }
  364. &.pieBtn {
  365. background: url('../../images/chart_pie.png') no-repeat center center;
  366. }
  367. &.fullBtn {
  368. background: url('../../images/fullscreen.png') no-repeat center center;
  369. }
  370. &.narrowBtn {
  371. background: url('../../images/narrow.png') no-repeat center center;
  372. }
  373. &.helpBtn {
  374. background: url('../../images/help.png') no-repeat center center;
  375. }
  376. }
  377. }
  378. }
  379. }
  380. >.countShow {
  381. line-height: 25px;
  382. text-align: center;
  383. color: $numTipFontColor;
  384. font-size: 12px;
  385. >span {
  386. color: $whiteFontColor;
  387. }
  388. }
  389. >.chartWrapper {
  390. position: absolute;
  391. top: 50px;
  392. left: 0;
  393. width: 100%;
  394. height: 100%;
  395. }
  396. }
  397. }
  398. }
  399. }
  400. </style>

补充:如何实现点击放大按钮时全屏展示?

修改所在盒子的样式,如下:

  1. .full {
  2. position: fixed;
  3. top: 0;
  4. left: 0;
  5. z-index: 9;
  6. }

缩小时,移除这个类型即可。

echarts图表自适应盒子的大小(盒子的大小是动态改变的),大到需要全屏展示的更多相关文章

  1. echarts 图表自适应外部盒子大小

    项目中用到了echarts,由于页面是自适应的,还得兼容移动, 因此图表还需要根据盒子的大小来变化. 自适应窗口及盒子大小 页面中有一个[放大.缩小]功能,及全屏展示和预览图表 窗口自适应 let m ...

  2. Angular echarts图表自适应屏幕指令

    关于echarts图表自适应问题 一.引入js文件 1. 在html页面引入angular.min.js文件 2. 在html页面引入echarts.min.js文件 3. 在html页面引入app. ...

  3. echarts图表自适应浏览器窗口的大小

    echarts问题描述 当浏览器窗口发生变化时,echarts图表未能自适应浏览器的变化,产生如下的效果 解决方案 在$(function(){}中添加 window.onresize = funct ...

  4. 给echarts加个“全屏展示”

    echarts的工具箱并没有提供放大/全屏的功能, 查找文档发现可自定义工具https://www.echartsjs.com/option.html#toolbox.feature show代码 t ...

  5. 一起来玩echarts系列(二)------echarts图表自适应问题

    为了直观查看公司服务器各个进程占用的内存动态情况,我使用echarts进行数据可视化,具体的实现过程按下不表. 最后实现的效果如图: 然后问题就来了,因UI采用了Bootstrap响应式框架,所以除了 ...

  6. echarts图表自适应,容器宽度设置为百分比,但是图表显示不全,缩到一起

    <div  id="chartContainer" style="height:100%;width:100%;"></div> cha ...

  7. echarts图表自适应

    当页面上只引入一个图表 window.onresize= () =>{ myEchart.resize() } 当引入多个时,上面的方法只会影响最后一个图表 window.addEventLis ...

  8. echarts 饼图 + 全屏显示

    效果图: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  9. echarts图表初始大小问题及echarts随窗口变化自适应

    最近在做一个轮播图,使用的是element的Carousel走马灯,每一个走马灯里是eachrts图,开始页面加载的时候发现echarts图并不能自适应,开始以为是走马灯的问题,后来发现不是 不知道大 ...

随机推荐

  1. 开源运维自动化平台-opendevops

    开源运维自动化平台-opendevops 简介 官网 | Github|  在线体验 CODO是一款为用户提供企业多混合云.自动化运维.完全开源的云管理平台. CODO前端基于Vue iview开发. ...

  2. 微信小程序之条件判断

    前文: 今天踩了一下午的坑,但是确实很简单的问题. 我说一下需求:扫描商品的二维码,从而判断,同一个二维码不可多次扫描: 点击扫一扫 会在灰色区域展示 扫描的商品信息,比如商品名称,商品码等,但是我们 ...

  3. Android通过JNI实现守护进程与卸载后跳转指定网页

    JNI进程守护 c代码部分如下:JNIEXPORT void JNICALL Java_com_sharetimes_qude_jni_JNIDaemon_daemon(JNIEnv * env, j ...

  4. GitHub:Youtube

    ylbtech-GitHub:Youtube 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://github.com/youtube 2.   ...

  5. linux之gzip命令

    命令格式: gzip [选项] 压缩(解压缩)的文件名 参数: -d 将压缩文件解压. -l  对每个压缩文件,显示压缩文件的大小,未压缩文件的大小,压缩比,未压缩文件的名字 -v 对每一个压缩和解压 ...

  6. 代码实现:定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)

    package com.loaderman.test; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; im ...

  7. CnPack开发包基础库

    unit CnCommon; {* |<PRE> ===================================================================== ...

  8. 002. Add Two Numbers

    题目链接:https://leetcode.com/problems/add-two-numbers/description/ Example: Input: (2 -> 4 -> 3) ...

  9. 2019CVPR:Classification-Reconstruction Learning for Open-Set Recogition(Abstract)

    Abstract Open-set classification is a problem of handling 'unknown' classes that are not contained i ...

  10. flask 框架快速入门

    flask 框架快速入门 搭建一个简易flask项目 首先使用 Pycharm创建flask项目 运行flask项目 1.使用Pycharm搭建flask项目 (如果Pycharm新建项目中未出现该图 ...