vue中使用echarts 多图表 随着父级div大小的改变而改变。
2020-02-06 07:58
127 查看
首先安装 npm install echarts --save (推荐使用cnpm,速度更快)
然后首先需要全局引入
在main.js中
import echarts from 'echarts' Vue.prototype.$echarts = echarts
然后在vue组件中:
页面(html)
<template> <div> <div> <el-col :span="12"> <div style="width: 100%;height:500px" ref="chart"></div> </el-col> <el-col :span="12"> <div style="width: 100%;height:500px" ref="myPie"></div> </el-col> </div> </div> </template>
页面(script)
export default { name: "index", data(){ return { chart: null, //关键一 myPie: null, } } mounted(){ this.initCharts(); this.initCharts2(); this.init(); }, methods:{ initCharts () { this.chart= this.$echarts.init(this.$refs.chart); console.log(this.$refs.chart); // 绘制图表 this.chart.setOption({ title: { text: '在Vue中使用echarts' }, tooltip: {}, xAxis: { data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }); }, initCharts2 () { this.myPie = this.$echarts.init(this.$refs.myPie); console.log(this.$refs.myPie); // 绘制图表 this.myPie.setOption({ title: { text: '在Vue中使用echarts' }, tooltip: {}, xAxis: { data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [25, 23, 66, 10, 10, 20] }] }); }, init() { //关键三 init的时候使用定时器监听窗口的变化,再调用echarts的resize方法 setTimeout(() => { window.addEventListener('resize', () => { this.chart.resize(); this.myPie.resize(); }) }, 20) }, destroyed() { //关键四 定时清除init带来的定时器 window.removeEventListener('resize', this.init, 20) }, } }
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 完整版ajax+百度echarts实现统计图表demo并随着窗口大小改变而自适应
- echarts图表大小随着外部div大小变化
- echarts图表自适应resize问题,div大小改变触发事件
- echarts图表根据外层div大小改变而改变
- 完整版ajax+百度echarts实现统计图表demo并随着窗口大小改变而自适应
- Vue中使用Echarts建立图表
- Vue 爬坑之路(八)—— 使用 Echarts 创建图表
- div随着浏览器窗口大小变更,改变大小(resize)
- vue项目中使用Echarts 动态更改图表数据 , Vue 折线图、柱状图等图表动态刷新 ,
- vue中使用echarts图表自适应的几种基本解决方案
- 实现div的大小随着内容进行动态改变
- 在vue中使用echarts图表
- vue-echarts图表使用方法及连接后端idea
- vue项目里使用ECharts图表后台返回图表数据
- Vue怎么使用Echarts创建图表
- 在vue中添加Echarts图表的基本使用教程
- 在编写网站的时候,给一个div设置背景图片,怎么让这个图片随着div大小的改变,而等比例的缩放。
- arcgis api for javascript 地图随着所在div大小的变化而改变的resize()事件