Vue项目中引入Echarts_(2021年更新)

  • Post author:
  • Post category:vue


在Vue项目中使用Echats,可以极大程度的方便完成很多Canvas功能。


1. 安装Echats

npm install echarts --save


2. 项目全局引入形式


– main.js 全局引入形式

// main.js
import * as echarts from "echarts"
Vue.prototype.$echarts = echarts
// 页面文件中使用,如index.vue中
<template>
  <div>
    <!-- echarts -->
    <div id="main" class="main_container"></div>
  </div>
</template>
<script>
export default {
  data() {
    return {}
  },
  mounted() {
    this.initCharts()
  },
  methods: {
    initCharts() {
      // 初始化echarts实例
      var myChart = this.$echarts.init(document.getElementById("main"))
      // 绘制图表
      myChart.setOption({
        title: {
          text: "ECharts 入门示例",
        },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.main_container {
  width: 100%;
  height: 200px;
  overflow: hidden;
}
</style>



需要注意一点,挂载echarts实例的dom节点,需要给定宽高。


– 页面中全局引入

<template>
  <div>
    <!-- echarts -->
    <div id="main" class="main_container"></div>
  </div>
</template>
<script>
import * as echarts from "echarts"
export default {
  data() {
    return {}
  },
  mounted() {
    this.initCharts()
  },
  methods: {
    initCharts() {
      // 初始化echarts实例
      var myChart = echarts.init(document.getElementById("main"))
      // 绘制图表
      myChart.setOption({
        title: {
          text: "ECharts 入门示例",
        },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.main_container {
  width: 100%;
  height: 200px;
  overflow: hidden;
}
</style>


3. 项目按需引入形式

// 页面文件,如index.vue
<template>
  <div>
    <!-- echarts -->
    <div id="main" class="main_container"></div>
  </div>
</template>
<script>
import * as echarts from "echarts/core"
import { TitleComponent, GridComponent } from "echarts/components"
import { BarChart } from "echarts/charts"
import { CanvasRenderer } from "echarts/renderers"
echarts.use([TitleComponent, GridComponent, BarChart, CanvasRenderer])

export default {
  data() {
    return {}
  },
  mounted() {
    this.initCharts()
  },
  methods: {
    initCharts() {
      // 初始化echarts实例
      var myChart = echarts.init(document.getElementById("main"))
      // 绘制图表
      myChart.setOption({
        title: {
          text: "ECharts 入门示例",
        },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.main_container {
  width: 100%;
  height: 200px;
  overflow: hidden;
}
</style>


显示的效果如下:

更多可参考:

https://echarts.apache.org/zh/index.html



版权声明:本文为weixin_37861326原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。