Echats-页面切换时echats图表刷新

2023-10-31 08:04

本文主要是介绍Echats-页面切换时echats图表刷新,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 在src文件夹下的composables创建echats.ts

 echats.ts的内容为:

import { nextTick, effectScope, onScopeDispose, ref, watch } from 'vue';
import type { ComputedRef, Ref } from 'vue';
import * as echarts from 'echarts/core';
import { BarChart, GaugeChart, LineChart, PictorialBarChart, PieChart, RadarChart, ScatterChart } from 'echarts/charts';
import type {BarSeriesOption,GaugeSeriesOption,LineSeriesOption,PictorialBarSeriesOption,PieSeriesOption,RadarSeriesOption,ScatterSeriesOption
} from 'echarts/charts';
import {DatasetComponent,GridComponent,LegendComponent,TitleComponent,ToolboxComponent,TooltipComponent,TransformComponent
} from 'echarts/components';
import type {DatasetComponentOption,GridComponentOption,LegendComponentOption,TitleComponentOption,ToolboxComponentOption,TooltipComponentOption
} from 'echarts/components';
import { LabelLayout, UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
import { useElementSize } from '@vueuse/core';
import { useThemeStore } from '@/store';export type ECOption = echarts.ComposeOption<| BarSeriesOption| LineSeriesOption| PieSeriesOption| ScatterSeriesOption| PictorialBarSeriesOption| RadarSeriesOption| GaugeSeriesOption| TitleComponentOption| LegendComponentOption| TooltipComponentOption| GridComponentOption| ToolboxComponentOption| DatasetComponentOption
>;echarts.use([TitleComponent,LegendComponent,TooltipComponent,GridComponent,DatasetComponent,TransformComponent,ToolboxComponent,BarChart,LineChart,PieChart,ScatterChart,PictorialBarChart,RadarChart,GaugeChart,LabelLayout,UniversalTransition,CanvasRenderer
]);/*** Echarts hooks函数* @param options - 图表配置* @param renderFun - 图表渲染函数(例如:图表监听函数)* @description 按需引入图表组件,没注册的组件需要先引入*/
export function useEcharts(options: Ref<ECOption> | ComputedRef<ECOption>,renderFun?: (chartInstance: echarts.ECharts) => void
) {const theme = useThemeStore();const domRef = ref<HTMLElement>();const initialSize = { width: 0, height: 0 };const { width, height } = useElementSize(domRef, initialSize);let chart: echarts.ECharts | null = null;function canRender() {return initialSize.width > 0 && initialSize.height > 0;}function isRendered() {return Boolean(domRef.value && chart);}function update(updateOptions: ECOption) {if (isRendered()) {chart?.clear();chart!.setOption({ ...updateOptions, backgroundColor: 'transparent' });}}async function render() {if (domRef.value) {const chartTheme = theme.darkMode ? 'dark' : 'light';await nextTick();chart = echarts.init(domRef.value, chartTheme);if (renderFun) {renderFun(chart);}update(options.value);}}function resize() {chart?.resize();}function destroy() {chart?.dispose();}function updateTheme() {destroy();render();}const scope = effectScope();scope.run(() => {watch([width, height], ([newWidth, newHeight]) => {initialSize.width = newWidth;initialSize.height = newHeight;if (newWidth === 0 && newHeight === 0) {// 节点被删除 将chart置为空chart = null;}if (canRender()) {if (!isRendered()) {render();} else {resize();}}});watch(options,newValue => {update(newValue);},{ deep: true });watch(() => theme.darkMode,() => {updateTheme();});});onScopeDispose(() => {destroy();scope.stop();});return {domRef};
}

vue文件下:

<template><div ref="Echart1" style="width:237px;height:250px;"></div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useEcharts } from '@/composables';const lineOptions = ref({tooltip: {trigger: 'item'},legend: {top: '5%',left: 'center'},// series 渲染的图表,可以配置多个series: [//图表内容]
});
const { domRef: Echart1 } = useEcharts(lineOptions as any);
</script>

这篇关于Echats-页面切换时echats图表刷新的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/313949

相关文章

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur

SpringBoot实现多环境配置文件切换

《SpringBoot实现多环境配置文件切换》这篇文章主要为大家详细介绍了如何使用SpringBoot实现多环境配置文件切换功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 示例代码结构2. pom文件3. application文件4. application-dev文

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

C#实现查找并删除PDF中的空白页面

《C#实现查找并删除PDF中的空白页面》PDF文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的,下面我们来看看如何使用Spire.PDFfor.NET通过C#... 目录安装 Spire.PDF for .NETC# 查找并删除 PDF 文档中的空白页C# 添加与删

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.