Vue中使用echarts

一、安装echarts

npm install echarts //默认安装最新版本
npm install echarts@4.8.0 //安装指定版本

二、导入echarts

在 main.js 文件中引入,全局使用

import Vue from 'vue'
//引入
import echarts from 'echarts'
Vue.prototype.$echarts = echarts //将echarts放在vue原型链上,使用this.$echarts即可调用

-------- 2021.5.16更新
当安装的echarts版本为最新版本,也就是v5版本不支持以上写法了,改为

import * as echarts from 'echarts';
// 按需引入
// import * as echarts from 'echarts/lib/echarts';
//参考官网 https://echarts.apache.org/zh/tutorial.html#ECharts%205%20%E5%8D%87%E7%BA%A7%E6%8C%87%E5%8D%97

三、页面中使用

1、提供容器,并为容器设定宽高

<div  id="chart" style="width: 700px; height: 300px; background: #020f2b"></div>

2、在methods中

methods:{
	initChart() {
      let colorsList = ["#00EA9C"];
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        color: colorsList,
        xAxis: {
          type: "category",
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
          axisLabel: {
            color: "#fff",
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          splitLine: {
            show: false,
          },
        },
        yAxis: {
          type: "value",
          axisLabel: {
            color: "#fff",
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          splitLine: {
            show: false,
          },
        },
        series: [
          {
            data: [120, 200, 150, 80, 70, 110, 130],
            type: "bar",
          },
        ],
      };
      //容器
      let myChart = this.$echarts.init(document.getElementById("chart"));
      myChart.setOption(option);
      //页面缩放时,echart图跟随缩放
      window.addEventListener("resize", function () {
        myChart.resize();
      });
    },
}

3、在mounted中调用

mounted(){
	this.initChart()
}

四、最终效果

在这里插入图片描述

以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值