• vue+echarts的各种问题


    echarts官网:https://echarts.apache.org/examples/zh/index.html#chart-type-line
    1:一个页面存在多个echarts图形,resize失效,图形自适应窗口大小。
    当一个页面有两个echarts图形,想要页面大小发生改变,重新绘制图形如果还写为
    myChart.setOption(option);
    window.onresize = myChart.resize;
    则只有一个图形可自适应窗口大小,另外一个则不能随窗口大小而改变图形大小,

    解决办法为在每一个echarts图形页面将上面代码改为如下代码:
    myChart.setOption(option);
    window.addEventListener("resize",function(){
    myChart.resize();
    });
    例如:

    //这个设置echart的自适应高度,切记一定要给id的父级设置宽高,不然无法渲染图标的高度。
    <div class="chartBorderBox"> 
    	<!-- <div id='yieldChart' style='height:250px'></div> -->//官网是直接设置固定高度
    	<div id='yieldChart' class="board-border"></div>
    </div>
    css: 
    .chartBorderBox{100%;height:100%};//如果图标外部有标题,需要减去标题的高度比如1.5rem的高度:height: calc(100% - 1.5rem)
    .board-border {  100%; height: 100%; border: 1px solid #0d2451; position: relative; overflow: hidden; // background: #151456; }
     //以上2个盒子的设置即可自适应窗口高度的变化。 
    js:
    OeeChart(dataText, data1, data2, data3) {
    				// 温度趋势数据开始------------------------
    				let yieldChart = this.$echarts.init(document.getElementById('yieldChart'))
    				let option = {
    					title: {
    						text: '',
    						textAlign: 'center',
    						textStyle: {
    							color: '#80FFFF',
    							fontSize: 12
    						}
    					},
    					legend: {
    						data: ['OEE', '时间移动', '性能稼动'],
    						textStyle: {
    							color: "white"
    						}
    					},
    					grid: {
    						left: '2%',
    						right: '2%',
    						top: '20%',
    						bottom: '5%',
    						containLabel: true
    					},
    					tooltip: {
    						trigger: 'axis' //鼠标悬浮提示
    					},
    					xAxis: {
    						type: 'category',
    						data: dataText,
    						axisLine: { //这是x轴文字颜色
    							lineStyle: {
    								color: "#ccc",
    							}
    						},
    						axisLabel: {
    							interval: 0,
    							rotate: 40,
    							show: true,
    						},
    					},
    					yAxis: {
    						type: 'value',
    						splitLine: {
    							show: true,
    							lineStyle: {
    								type: 'dashed' //分割线为虚线
    							}
    						},
    						axisLabel: {
    							formatter: '{value}',
    							textStyle: {
    								color: '#ccc'
    							}
    						}
    					},
    					series: [{
    							name: 'OEE',
    							data: data1,
    							type: 'line',
    
    							itemStyle: {
    								normal: {
    									color: '#54AAFB',
    									lineStyle: {
    										color: "#54AAFB" //折线的颜色
    									},
    								}
    							},
    							label: {
    								show: true
    							},
    
    						},
    						{
    							name: '时间移动',
    							data: data2,
    							type: 'line',
    							symbol: 'emptyCircle',
    							symbolSize: 8,
    							itemStyle: {
    								normal: {
    									color: 'green',
    									lineStyle: {
    										color: "green" //折线的颜色
    									},
    								}
    							},
    							label: {
    								show: true
    							},
    
    						}, {
    							name: '性能稼动',
    							data: data3,
    							type: 'line',
    							symbol: 'emptyCircle',
    							symbolSize: 8,
    							itemStyle: {
    								normal: {
    									color: 'yellow',
    									lineStyle: {
    										color: "yellow" //折线的颜色
    									},
    								}
    							},
    							label: {
    								show: true
    							},
    
    						}
    					]
    				};
    				// 绘制图表
    				yieldChart.setOption(option);
    				// 温度趋势数据结束------------------------
    
    				setTimeout(function() {
    					window.onresize = function() {
    						yieldChart.resize();
    					}
    				}, 300)
    			}
    
    

    --------------------2022/3/29更新------------------------------------------------------------------------------------------------------------

    以上问题只能改变浏览器窗户的大小改变而自适应,同样现实场景还有可能通过页面某个按钮点击来改变内部窗口大小,这个时候就不完全实用了,所以以下:
    首先自定义指令:

    在main.js里引用:import './utils/btnDirectives' 
    btnDirectives.js:
    import Vue from 'vue'
    import router from '../router/index'
    
    Vue.directive('resize', {
        bind(el, binding) { // el为绑定的元素,binding为绑定给指令的对象
            // console.log(el, "绑定", binding);
            let width = '', height = '';
            function isReize() {
                const style = document.defaultView.getComputedStyle(el);
                if (width !== style.width || height !== style.height) {
                    binding.value({  style.width, height: style.height });  // 关键(这传入的是函数,所以执行此函数)
                }
                width = style.width;
                height = style.height;
            }
            el.__vueSetInterval__ = setInterval(isReize, 300);
        },
        unbind(el) { 
            clearInterval(el.__vueSetInterval__);
        }
    })
    // 按钮权限自定义指令 =============== 使用方法
    // 页面组件上 v-resize= 'monResize'
    

    应用:

    <template>
    	<div id="top5Chart"  v-resize='monResize'></div>
    </template>
    methods:{
    monResize(){
    		let completedRate=localStorage.getItem("completedRate")
    		this.progressChart(completedRate)//这个方法就是重新调用一下能够渲染图表的方法,比如也可以直接调用请求后台接口的方法(若渲染图标是在请求接口后直接调用渲染方法progressChart的时候)
    },
    progressChart(data) {
    				let progressChart = this.$echarts.init(document.getElementById('progressChart'))
    				let option = {
    					series: [{
    						type: 'gauge',
    						radius: "90%",
    						center: ['50%', '50%'],
    						axisLine: {
    							lineStyle: {
    								 20,
    								color: [
    									[0.3, '#67e0e3'],
    									[0.7, '#37a2da'],
    									[1, '#fd666d']
    								]
    							}
    						},
    						pointer: {
    							 5,
    							itemStyle: {
    								color: 'auto',
    							}
    						},
    						axisTick: {
    							distance: 10,
    							length: 8,
    							lineStyle: {
    								color: '#fff',
    								 2
    							}
    						},
    						splitLine: {
    							distance: -10,
    							length: 10,
    							lineStyle: {
    								color: '#fff',
    								 4
    							}
    						},
    						axisLabel: {
    							color: 'auto',
    							distance: 15, //内盘距离
    							fontSize: 10
    						},
    						detail: {
    							valueAnimation: true,
    							fontSize: 20, //表盘数据
    							formatter: '{value} %',
    							color: 'auto'
    						},
    						data: [{
    							value: data
    						}]
    					}]
    				};
    				// 绘制图表
    				option && progressChart.setOption(option);
    				progressChart.resize();
    				
    			},
    			
    }
    

    以上即可实现

  • 相关阅读:
    从苹果系统InstallESD.dmg里提取IOS
    Swift编程语言中文版教程---《The Swift Programming Language》
    VMware workstation 10.0的永久key
    VMware Workstation与Hyper-V不兼容。请先从系统中移除Hyper-V角色,然后再运行VMware Workstation。
    Unity3D游戏制作入门教程
    MyEclipse中文网
    机器学习中的两个概率模型
    机器学习中的两个概率模型
    EM算法概念
    EM算法概念
  • 原文地址:https://www.cnblogs.com/Fancy1486450630/p/16012102.html
Copyright © 2020-2023  润新知