vue3按需引入echarts图表

公司项目需要升级vue3,对项目就行了重构根据项目整理了一下如何按需引入echarts

1. 安装echarts依赖

npm install echarts --save

2. 在文件中新建echarts.ts

import type { App } from "vue";
import * as echarts from "echarts/core";
// 官网推荐使用CanvasRenderer进行渲染
import { CanvasRenderer } from 'echarts/renderers';
// 按需引入的图表类型首字母大写后面带Chart
import { PieChart, BarChart, LineChart } from "echarts/charts";
// 必须的组件
import {
  GridComponent,
  TitleComponent,
  LegendComponent,
  GraphicComponent,
  ToolboxComponent,
  TooltipComponent,
  DataZoomComponent,
  VisualMapComponent
} from "echarts/components";

const { use } = echarts;
// 注册的组件类型
use([
  PieChart,
  BarChart,
  LineChart,
  CanvasRenderer,
  GridComponent,
  TitleComponent,
  LegendComponent,
  GraphicComponent,
  ToolboxComponent,
  TooltipComponent,
  DataZoomComponent,
  VisualMapComponent
]);

/**
 * @description 自定义主题
 * @see {@link https://echarts.apache.org/zh/download-theme.html}
 */
// import theme from "./theme.json";
// registerTheme("ovilia-green", theme);

/**
 * @description 按需引入echarts
 * @see {@link https://echarts.apache.org/handbook/zh/basics/import#%E6%8C%89%E9%9C%80%E5%BC%95%E5%85%A5-echarts-%E5%9B%BE%E8%A1%A8%E5%92%8C%E7%BB%84%E4%BB%B6}
 */
export function useEcharts(app: App) {
  app.config.globalProperties.$echarts = echarts;
}

export default echarts;

3. main.ts的代码中引入:

import { useEcharts } from "@/plugins/echarts";
getServerConfig(app).then(async config => {
//无关代码删掉了正常注册
  app.use(useEcharts)
  app.mount("#app");
});

4. 组件中使用

注意点:

1.先引入getCurrentInstance
import { getCurrentInstance } from "vue";
2.实例化(变量名一定得是proxy)
const { proxy } = getCurrentInstance() as any;
3.可以用ref 或者 document.getElementById(“chartDom1”)两种方式获取dom元素但是要注意, 必须在元素加载完成后,不然会报错(使用 onMounted或者nextTick 等等)
<script setup lang="ts">
import { ref, computed, type Ref, getCurrentInstance, nextTick } from "vue";
const { proxy } = getCurrentInstance(); // !!!!
let chartDom22 =  ref<HTMLDivElement | null>(null);
nextTick(() => {
  const chartDom1 = <HTMLElement>document.getElementById("chartDom1");
  const myChart = proxy.$echarts.init(chartDom22.value as HTMLDivElement);
  window.onresize = function () {
    myChart.resize();
  };
  myChart.setOption(option);
});
const option = {
  tooltip: {
    trigger: "axis",
    axisPointer: {
      type: "shadow"
    }
  },
  grid: {
    bottom: "20%",
    height: "68%",
    containLabel: true
  },
  xAxis: [
    {
      type: "category",
      axisTick: {
        alignWithLabel: true
      },
      axisLabel: {
        interval: 0
        // width: "70",
        // overflow: "truncate"
      },
      data: ["啊", "吧", "传", "的"]
    }
  ],
  yAxis: [
    {
      type: "value"
    }
  ],
  series: [
    {
      name: "测试",
      type: "bar",
      data: [3, 20, 107, 107]
    }
  ]
};
</script>
<template>
  <div id="chartDom1" ref="chartDom22" style="width: 100%; height: 35vh" />
</template>

网上查到一种vite打包优化还没测试过,有时间可以试试

(在vite.config.ts的build通过设置 manualChunks方案,将echarts单独打包并通过按需引入减少主包体积)

  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          echarts: ['echarts']
        }
      },
    },
  },

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue 3中使用ECharts,可以按照以下步骤进行设置: 1. 安装echarts 使用npm或yarn安装echarts: ``` npm install echarts --save ``` 或者 ``` yarn add echarts ``` 2. 在组件中引入echartsVue组件的`<script>`标签中,引入echarts并将其赋值给一个变量,例如: ``` import echarts from 'echarts'; ``` 3. 在`<template>`中创建一个容器 在组件的`<template>`标签中,创建一个容器,它将用于显示echarts图表。例如: ``` <template> <div id="chart" style="width: 100%; height: 500px;"></div> </template> ``` 4. 在`setup()`函数中创建echarts实例并初始化 在Vue 3中,可以使用`setup()`函数来创建echarts实例并初始化。例如: ``` import { ref, onMounted } from 'vue'; export default { setup() { const chartRef = ref(null); onMounted(() => { const chart = echarts.init(chartRef.value); chart.setOption({ // 在这里设置echarts图表的配置项 }); }); return { chartRef }; } }; ``` 在上面的示例中,我们使用了Vue 3的`ref()`和`onMounted()`函数来创建一个引用,并在组件挂载时初始化echarts实例。然后,我们可以在`return`语句中返回该引用,以便在`<template>`中使用。 5. 在`<template>`中使用echarts图表 最后,在`<template>`标签中,将echarts图表容器与`chartRef`引用绑定: ``` <template> <div ref="chartRef" style="width: 100%; height: 500px;"></div> </template> ``` 完成以上步骤后,您就可以在Vue 3中使用ECharts了。根据您的求,您可以在`setup()`函数中设置echarts图表的配置项来创建不同类型的图表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值