echarts5在Vue中按需引入

痛定思痛。 2023-01-12 04:14 484阅读 0赞

安装 echarts5.x

  1. npm install echarts --save

按需引入 echarts 图表和组件

先在项目根路径下新建 plugin/echartsUI.js 文件

  1. // 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口。
  2. import * as echarts from 'echarts/core';
  3. // 引入各种图表,图表后缀都为 Chart
  4. import {
  5. BarChart
  6. } from 'echarts/charts';
  7. // 引入提示框,标题,直角坐标系等组件,组件后缀都为 Component
  8. import {
  9. TitleComponent,
  10. TooltipComponent,
  11. GridComponent,
  12. LegendComponent
  13. // GeoCoComponent
  14. } from 'echarts/components';
  15. // 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
  16. import {
  17. CanvasRenderer
  18. } from 'echarts/renderers';
  19. // 注册必须的组件
  20. echarts.use(
  21. [TitleComponent, TooltipComponent, GridComponent, LegendComponent, BarChart, CanvasRenderer]
  22. )
  23. export default echarts

修改 main.js

  1. // 按需引入 echarts 5.x
  2. import echarts from './plugins/echartsUI'
  3. Vue.prototype.$echarts = echarts

测试

  1. initEcharts () {
  2. let myEcharts = this.$echarts.init(this.$refs.bar)
  3. let option = {
  4. title: {
  5. text: '按需引入'
  6. },
  7. tooltip: {
  8. trigger: 'axis',
  9. axisPointer: {
  10. type: 'shadow'
  11. }
  12. },
  13. xAxis: {
  14. type: 'category',
  15. data: ['周一', '周二', '周三', '周四', '周五']
  16. },
  17. yAxis: {
  18. type: 'value'
  19. },
  20. legend: { },
  21. series: [
  22. {
  23. name: '星期',
  24. type: 'bar',
  25. data: [23, 33, 45, 56, 78]
  26. }
  27. ]
  28. }
  29. myEcharts.setOption(option)
  30. }

效果
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,484人围观)

还没有评论,来说两句吧...

相关阅读