创建窗体自适应Echarts图表(基于Vue2.0)

5 篇文章 0 订阅
1 篇文章 0 订阅

1 安装echarts

npm install echarts

2 查看版本

在package.json里面可以查看版本
在这里插入图片描述

3 导入包文件

在main.js文件中添加下面代码

import * as echarts from "echarts"
Vue.prototype.$echarts = echarts

4 在Vue组件中使用

创建一个Helloworld.vue组件

<template>
  <div
    class="hello"
    :style="{ height: divHeight + 'px', width: divWidth + 'px' }"
  ></div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      divHeight: null,
      divWidth: null
    };
  },
  created() {
    this.divHeight = window.innerHeight * 0.8;
    this.divWidth = window.innerWidth * 0.8;
    const that = this;
    window.onresize = function() {
      that.divHeight = this.innerHeight * 0.8;
      that.divWidth = this.innerWidth * 0.8;
      that.charts.resize();
    };
  },
  mounted() {
    this.drawCharts();
  },
  methods: {
    drawCharts() {
      this.charts = this.$echarts.init(document.querySelector(".hello"));
      this.charts.setOption({
        title: {
          text: "列表"
        },
        tooltip: {},
        legend: {
          data: ["销量"]
        },
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      });
    }
  }
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>

5 添加路由

index.js文件

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    }
  ]
})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值