一、echarts 升级为 5.0.1了,之前用 4.9.0 的小伙伴会发现,我用原来的方法发现echarts导不进来了。让我们来从头讲一遍 echarts的正确开启方式。

        记住,安装时务必要加 @4.9.0,版本,不然系统会默认给下载 5.0.1,然后你用原来的方法,发现会各种报错。

npm install echarts@4.9.0 -S
或者使用淘宝的镜像
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install echarts@4.9.0 -S

 二、main.js 里引入 echarts 包。

import echarts from 'echarts'
Vue.prototype.$echarts = echarts

三、在Echarts.vue中

<div id="myChart" :style="{width: '900px', height: '300px'}"></div>

四、在methods 下放置图表的函数,mounted 开启浏览器时自动调用 drawLine 函数。

methods:{
      drawLine(){
        // 基于准备好的dom,初始化echarts实例
        alert("xx");
        let myChart = this.$echarts.init(document.getElementById('myChart'))
        // 绘制图表
        alert(myChart);
        myChart.setOption({
            title: { text: '在Vue中使用echarts' },
            tooltip: {},
            xAxis: {
                data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
            },
            yAxis: {},
            series: [{
                name: '销量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20]
            }]
        });
    }
  },

  mounted(){
    this.drawLine();
  }

五、渲染效果图。

 

Logo

前往低代码交流专区

更多推荐