前言
在后台项目中,图表特别常见,在使用echarts作图的时候肯定会有自适应的问题,这里我向大家介绍一下,我使用的方法,基于vue 和element 的。其他框架同理。
正文
- 配置
npm 导入eachart,并在main.js配置。把echarts挂载在 prototype上方便页面调用
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
- 页面使用
为了实现自适应,所以在行内样式的width设置为100%。
<template>
<div>
<div id="myChart" :style="{width: '100%', height: '400px'}"></div>
</div>
</template>
js部分
<script>
export default {
data() {
return {
}
},
mounted() {
this.drawLine();
//实现自适应部分
this.resizefun = ()=>{
this.$echarts.init(document.getElementById('myChart')).resize()
}
window