vue整合echarts,动态改变宽高

npm引入

npm install echarts --save

main.js中添加

import echarts from “echarts”
Vue.prototype.$echarts = echarts

echarts.vue

<template>
  <div :style="{width: width, height: height}">
    <div ref="echart" style="width: 100%; height: 100%"></div>
  </div>

</template>

<script>
  export default {
    props: {
      option: {
        type: Object,
        default: {}
      }
    },
    name: "echart",
    data() {
      return {
        id: 'chart',
        myChart: null,
      }
    },
    methods: {
      resize() {
        this.myChart.resize();
      }
    },
    watch: {
      option: {
        deep: true,
        handler() {
          if (this.myChart) {
            this.$nextTick(() => this.resize())
            this.myChart.setOption(this.option, true);
          }
        }
      },
    },
    computed: {
      width() {
        return this.option.width1? this.option.width1 : '100%'
      },
      height() {
        return this.option.height1? this.option.height1 : '100%'
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.myChart = this.$echarts.init(this.$refs.echart, 'shine');
        this.myChart.setOption(this.option);
        this.myChart.on('click', (params) => {
          this.$emit('click', params)
        });
      })
      window.addEventListener("resize", () => {
        this.resize();
      })
    }
  }
</script>

<style scoped>

</style>

下载链接(免费):https://download.csdn.net/download/qq_35134375/13008296

Vue整合echarts可以通过以下步骤完成: 1. 在main.js中引入echarts库,可以使用以下代码: ```javascript import echarts from 'echarts' Vue.prototype.$echarts = echarts ``` 2. 创建一个Echarts.vue组件,可以使用以下代码作为模板: ```html <template> <div id="myChart" :style="{width: '300px', height: '300px'}"></div> </template> ``` 3. 在Echarts.vue组件中的mounted方法中调用绘制图表的方法,可以将以下代码添加到mounted方法中: ```javascript mounted() { this.drawLine(); } ``` 4. 在Echarts.vue组件中定义绘制图表的方法drawLine,可以使用以下代码作为示例: ```javascript methods: { drawLine() { // 基于准备好的dom,初始化echarts实例 let myChart = this.$echarts.init(document.getElementById('myChart')) // 绘制图表 myChart.setOption({ title: { text: '在Vue中使用echarts' }, tooltip: {}, xAxis: { data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20 }] }); } } ``` 这样,你就可以在Vue整合echarts,并在Echarts.vue组件中绘制图表了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue整合echarts(数据可视化)](https://blog.csdn.net/qq_43620423/article/details/108826561)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值