文章的最初总是很难水的,就不多说了
本文触及 : TypeScript、Vue3、 echarts
由于 ECharts 的运用场景及其广阔,而且定制化的场景比较多,所以就不封装可以复用的组件了,在我看来每个组件仍是需要一个独立的 option ,这儿仅封装更好运用的 echats
目录
|--src
|--components // 组件
|--echarts // echats 封装目录
|--echarts-types.ts // 一些类型
|--library.ts // 为 echats 增加的一些功用
|--useECharts.ts // 主函数
|--EChartsComponents
|--a-echarts.vue // 组件运用
|--App.vue
代码
library.ts
在 library.ts 中会集引进,挂载 echarts 需要用到的组件和功用
import * as echarts from 'echarts/core';
import {
BarChart,
LineChart,
PieChart,
MapChart,
PictorialBarChart,
RadarChart,
ScatterChart
} from 'echarts/charts';
import {
TitleComponent,
TooltipComponent,
GridComponent,
PolarComponent,
AriaComponent,
ParallelComponent,
LegendComponent,
RadarComponent,
ToolboxComponent,
DataZoomComponent,
VisualMapComponent,
TimelineComponent,
CalendarComponent,
GraphicComponent
} from 'echarts/components';
echarts.use([
LegendComponent,
TitleComponent,
TooltipComponent,
GridComponent,
PolarComponent,
AriaComponent,
ParallelComponent,
BarChart,
LineChart,
PieChart,
MapChart,
RadarChart,
PictorialBarChart,
RadarComponent,
ToolboxComponent,
DataZoomComponent,
VisualMapComponent,
TimelineComponent,
CalendarComponent,
GraphicComponent,
ScatterChart
]);
export default echarts;
echarts-types.ts
一些需要运用的类型,在这儿标准
export enum RenderType {
SVGRenderer = 'SVGRenderer',
CanvasRenderer = 'SVGRenderer'
}
export enum ThemeType {
Light = 'light',
Default = 'default',
}
useECharts.ts 首要文件
引进需要运用的功用模块,EChartsOption 类型在运用时简单报红,这儿暂时用 any
import { onMounted, onUnmounted, Ref, unref } from "vue";
import echarts from "./library";
// import type { EChartsOption } from 'echarts'
import { SVGRenderer, CanvasRenderer } from 'echarts/renderers'
import { RenderType, ThemeType } from './echarts-types'
export function useECharts(elparams: Ref<HTMLDivElement> | HTMLDivElement, autoUpdateSize: boolean = false, render: RenderType = RenderType.SVGRenderer, theme = ThemeType.Default) {
// 渲染形式
echarts.use(render === RenderType.SVGRenderer ? SVGRenderer : CanvasRenderer)
// echats实例
let echartsInstance: echarts.ECharts | null = null
// 初始化 echats实例
function initCharts() {
const el = unref(elparams)
if (!el) return
echartsInstance = echarts.init(el, theme)
}
// 装备
function setOption(option: any) {
showLoading()
if (!echartsInstance) initCharts()
if (!echartsInstance) return
echartsInstance.setOption(option)
hideLoading()
}
// 获取 echats实例
function getInstance() {
if (!echartsInstance) initCharts()
return echartsInstance
}
// 更新巨细
function onResize() {
echartsInstance?.resize()
}
// 监听元素巨细改变
function watchEl() {
if (animation) unref(elparams).style.transition = 'width 1s, height 1s'
const resizeObserve = new ResizeObserver(() => onResize())
resizeObserve.observe(unref(elparams))
}
// 显示加载状况
function showLoading() {
if (!echartsInstance) initCharts()
echartsInstance?.showLoading()
}
// 隐藏加载状况
function hideLoading() {
if (!echartsInstance) initCharts()
echartsInstance?.hideLoading()
}
// 生命钩子——组件挂载完成
onMounted(() => {
window.addEventListener('resize', onResize)
if (autoUpdateSize) watchEl()
})
// 生命钩子——页面销毁
onUnmounted(() => {
window.removeEventListener('resize', onResize)
})
return { setOptions, getInstance }
}
在组件中的运用
a-echarts.vue 运用,咱们现在只需要去找一些 option 就可以完成不同的图表了
这个还不错的网站,有许多示例 PPChart 咱们随意拿一个来试试吧,
把装备代码复制到下面,就可以看见作用了
<template>
<div class="chart" ref="MyEcharts"></div>
</template>
<script lang="ts" setup>
import { onMounted, Ref, ref } from "vue";
import echarts from "../echarts/library";
//获取echarts实例
const MyEcharts = ref<HTMLDivElement | null>(null)
const { setOption, getInstance } = useECharts(MyEcharts as Ref<HTMLDivElement>, false, true)
onMounted(() => {
setOption(option);
const echartsInstance = getInstance()
})
</script>
App.vue
<template>
<echarts></echarts>
</template>
<script setup lang="ts">
import echarts from './components/EchartsComponents/a-echarts.vue'
</script>
<style scoped></style>
完毕! 感觉有帮助的话,求个小赞!!!