1:废话不多说 老步骤安装

npm install echarts --save

有淘宝镜像的可以选择  (安装速度快)
cnpm install echarts --save

2:自己新建一个 js 文件(名字随便起)

3:js文件中的内容(必须有的)

// 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";

/** 引入柱状图and折线图图表,图表后缀都为 Chart  */
import { BarChart, LineChart } from "echarts/charts";

// 引入提示框,标题,直角坐标系,数据集,内置数据转换器组件,组件后缀都为 Component
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
} from "echarts/components";

// 标签自动布局,全局过渡动画等特性
import { LabelLayout, UniversalTransition } from "echarts/features";

// 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";

// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  BarChart,
  LabelLayout,
  UniversalTransition,
  CanvasRenderer,
  LineChart,
]);

// 导出
export default echarts;

3.5:这里需要注意一(你想要按需引入的图表后缀都是Chart 开头就是官网上面的图例 看下图)

按需引入时:import {  LineChart  } from "echarts/charts"  (注意  开头一定要大写!!!)

 

4:把自己创建好的js文件引入 全局main.js中

// 引入echarts
import echarts from "./echarts/echarts";

// 挂载到vue实例中
Vue.prototype.$echarts = echarts

调用的时候就是 :  this.$echarts

5:要使用的vue组件中 (以下实例)

<template>
  <div>
    <div id="main"></div>
    <div id="maychar"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  methods: {
    // 基本柱形图
    change() {
      const chartBox = this.$echarts.init(document.getElementById("main"));
      const option = {
        xAxis: {
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        },
        yAxis: {},
        series: [
          {
            type: "bar",
            data: [23, 24, 18, 25, 27, 28, 25],
          },
        ],
      };
      chartBox.setOption(option);
      // 根据页面大小自动响应图表大小
      window.addEventListener("resize", function () {
        chartBox.resize();
      });
    },
    // 折线图
    changetype() {
      // 获取组件实例
      const machart = this.$echarts.init(document.getElementById("maychar"));
      // 设置配置项
      const option = {
        xAxis: {
          data: ["A", "B", "C", "D", "E"],
        },
        yAxis: {},
        series: [
          {
            data: [10, 22, 28, 43, 49],
            type: "line",
            stack: "x",
          },
          {
            data: [5, 4, 3, 5, 10],
            type: "line",
            stack: "x",
          },
        ],
      };
      // 复制
      machart.setOption(option);
      // 根据页面大小自动响应图表大小
      window.addEventListener("resize", function () {
        machart.resize();
      });
    },
  },
  mounted() {
    this.change();
    this.changetype();
  },
};
</script>

<style lang="scss" scoped>
#main {
  min-width: 31.25rem;
  min-height: 31.25rem;
  // max-height: 500px;
}
#maychar {
  max-height: 500px;
  // max-height: 400px;
  height: 500px;
}
</style>

6:效果图如下 (成功了回来给我点个赞嗷客官)

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐