美文网首页
《图解Echarts5 in Vue3》前言

《图解Echarts5 in Vue3》前言

作者: 张中华 | 来源:发表于2021-05-29 07:15 被阅读0次

前段时间一直在面试,然后在需求中看到很多需要使用Echarts的需求。虽然说UI组件可能上手并不难,但是当别人问的时候,如果说没用过,多少有些不合适。之后针对这个专题简单了解下,毕竟没有实际应用场景,打发时间用~
官方网址:
https://echarts.apache.org/zh/index.html

最近在学习vue,便基于vue对echarts进行了解。

第一步:新建一个vue项目
vue create vue-echarts

第二步: 引入echarts
yarn add echarts
如果你不喜欢这种引入方式,通过下面的链接可以了解如何获取echarts
https://echarts.apache.org/zh/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20ECharts

  • 官网下载
  • GitHub
  • npm: npm install echarts --save
  • CDN: 参考jsDelivr

在main.js里面添加引用:

注意:这里使用import * as echarts from 'echarts'
import { createApp } from "vue";
import App from "./App.vue";
import * as echarts from "echarts";

const app = createApp(App);

app.config.globalProperties.$echarts = echarts;

app.mount("#app");

第三步:使用:绘制一个简单的图表
参考:https://echarts.apache.org/zh/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20ECharts
https://echarts.apache.org/en/tutorial.html#Use%20ECharts%20with%20bundler%20and%20NPM

  • 先准备一个DOM元素,定义好高度和宽度
  • 使用echarts.init()方法,初始化一个ECharts的实例
  • 使用setOption设置配置项
示例:

App.vue

<template>
  <div id="app">
    <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
    <div id="main" style="width: 600px; height: 400px"></div>
  </div>
</template>

<script>
export default {
  name: "App",
  components: {},
  methods: {
    myEcharts() {
      // 基于准备好的dom,初始化echarts实例
      var myChart = this.$echarts.init(document.getElementById("main"));

      // 指定图表的配置项和数据
      var option = {
        title: {
          text: "ECharts 入门示例",
        },
        tooltip: {},
        legend: {
          data: ["销量"],
        },
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },
  },
  mounted() {
    this.myEcharts();
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

测试结果

相关文章

网友评论

      本文标题:《图解Echarts5 in Vue3》前言

      本文链接:https://www.haomeiwen.com/subject/wlkssltx.html