解决vue iview UI 侧边导航折叠时,echarts图表组件不会自适应页面宽度的问题

iview UI 侧边栏折叠,echarts图表动态适应页面宽度

只能算是基本实现了,希望能帮助到有需要的人,网上搜了,也没有特别好的解决方法,也许是我太菜吧,还有就是不知道会不会导致内存问题,用在生产环境中时,请一定多多测试,若有更好的方法 ,希望能一起交流…上代码;

//main.vue

    <span  @click="collapsedSider"
            class=" inv-admin-layout-header-trigger inv-admin-layout-header-min">
            <Icon style="vertical-align: -0.145em;"
              :style="{transform: 'rotateZ(' + (isCollapsede ? '-90' : '0') + 'deg)'}" type="md-menu" size="18" />
    </span>


```javascript
<script>
 export default {
  data() {
      return {
        isCollapsede: false, //折叠
        }
    },
    methods: {
        //折叠
        collapsedSider() {
            this.isCollapsede = !this.isCollapsede;
            this.$store.commit('setCollapsede',this.isCollapsede);
      },
    }
 }
</script>

//vuex

const app = {
  state: {
    isCollapsed: false,
   },
  mutations: {
    setCollapsede (state, value) {
      state.isCollapsed = value;
    }
  }
 };
 export default app;

// 下面是图表组件 ,–可直接复制拿来用.
.只要折叠按钮触发,图表就会重绘.

<template>
  <div ref="myChart" :style="style"></div>
</template>

<script>
  import echarts from 'echarts'
  export default {
    data() {
      return {
        myChart: null,
        option: {}
      }
    },
    props: {
      width: {
        type: String,
        default: "100%"
      },
      height: {
        type: String,
        default: "100%"
      },
      xAxis: {
        type: Array,
      },
      totalNum: {
        type: Array,
      },
      intactRate: {
        type: Array,
      },
      chartTitle: {
        type: String
      },
      legendArray: {
        type: Array
      }

    },
    computed: {
      style() {
        return {
          height: this.height,
          width: this.width
        };
      },
      isCollapsede() {
        return this.$store.state.app.isCollapsed;
      },
    },
    watch: {
      totalNum(val) {
        this.initChart();
      },
      isCollapsede() {
        setTimeout(() => {
          this.myChart.resize();
        }, 400)
      }
    },
    methods: {
      initChart() {
        this.myChart = echarts.getInstanceByDom(this.$refs.myChart)
        if (!this.myChart) {
          this.myChart = echarts.init(this.$refs.myChart)
        }
        this.option = {
          tooltip: {
            trigger: 'axis',
            formatter: '{b}<br />{a0}: {c0}<br />{a1}: {c1}%',
          },
          legend: {
            data: this.legendArray,
            x: "center",
            textStyle: {
              fontStyle: 'normal',
              fontSize: 12,
            }
          },
          grid: {
            left: '3%',
            right: '1%',
            top: '45',
            bottom: '3%',
            containLabel: true,
          },
          xAxis: [{
            type: 'category',
            axisPointer: {
              type: "shadow"
            },
            axisLine: {
              lineStyle: {
                color: '#009688',//x轴线宽与颜色
                width: '2'
              },
            },
            axisLabel: {
              textStyle: {
                color: '#333' //x轴字体
              }
            },
            data: this.xAxis
          }],
          yAxis: [
            {
              type: 'value',
              name: "数量",
              axisLine: {
                lineStyle: {
                  color: '#009688',//y轴左侧线宽与颜色
                  width: '2'
                },

              },
              axisLabel: {
                textStyle: {
                  color: '#333' //y轴左侧字体
                },
                formatter: '{value}'
              },
              splitLine: {
                show: true,
                lineStyle: {
                  color: '#f5f5f5',//背景横线
                }
              },
            },
            {
              type: 'value',
              name: "百分比",
              min: '0',
              max: '100',
              axisLine: {
                lineStyle: {
                  color: '#009688',//y轴右侧线宽与颜色
                  width: '2'
                },

              },
              axisLabel: {
                textStyle: {
                  color: '#333' //y轴右侧字体
                },
                formatter: '{value} %'
              },
              splitLine: {
                show: false,
                lineStyle: {
                  color: '#f5f5f5',//背景横线
                }
              },
            },
          ],
          series: [
            {
              name: this.legendArray[0].name,
              type: "bar",
              barWidth: 37,
              showAllSymbol: true,
              smooth: true,
              symbol: 'circle',
              symbolSize: 10,
              itemStyle: {
                normal: {
                  barBorderRadius: [2, 2, 0, 0],
                  color: '#009688',
                  label: {
                    show: true,
                    position: "top",
                  }
                }
              },
              data: this.totalNum
            },
            {
              name: this.legendArray[1].name,
              type: 'line',
              showAllSymbol: true,
              smooth: true,
              symbol: 'circle',
              symbolSize: 10,
              yAxisIndex: 1,
              itemStyle: {
                normal: {
                  color: '#d87a80',
                  label: {
                    show: true,
                    position: "top",
                  }
                }
              },
              data: this.intactRate
            }

          ]
        };
        this.myChart.setOption(this.option, true);
        window.addEventListener("resize", this.myChart.resize);
        this.myChart.resize();

      },
    },
    mounted() {
      this.$nextTick(function () {
        this.initChart();
      });
    }

  }
</script>
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值