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

相关文章

Oracle数据库如何切换登录用户(system和sys)

《Oracle数据库如何切换登录用户(system和sys)》文章介绍了如何使用SQL*Plus工具登录Oracle数据库的system用户,包括打开登录入口、输入用户名和口令、以及切换到sys用户的... 目录打开登录入口登录system用户总结打开登录入口win+R打开运行对话框,输php入:sqlp

IDEA如何切换数据库版本mysql5或mysql8

《IDEA如何切换数据库版本mysql5或mysql8》本文介绍了如何将IntelliJIDEA从MySQL5切换到MySQL8的详细步骤,包括下载MySQL8、安装、配置、停止旧服务、启动新服务以及... 目录问题描述解决方案第一步第二步第三步第四步第五步总结问题描述最近想开发一个新应用,想使用mysq

使用JavaScript将PDF页面中的标注扁平化的操作指南

《使用JavaScript将PDF页面中的标注扁平化的操作指南》扁平化(flatten)操作可以将标注作为矢量图形包含在PDF页面的内容中,使其不可编辑,DynamsoftDocumentViewer... 目录使用Dynamsoft Document Viewer打开一个PDF文件并启用标注添加功能扁平化

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

SpringBoot如何访问jsp页面

《SpringBoot如何访问jsp页面》本文介绍了如何在SpringBoot项目中进行Web开发,包括创建项目、配置文件、添加依赖、控制层修改、测试效果以及在IDEA中进行配置的详细步骤... 目录SpringBoot如何访问JSP页python面简介实现步骤1. 首先创建的项目一定要是web项目2. 在

Spring Boot实现多数据源连接和切换的解决方案

《SpringBoot实现多数据源连接和切换的解决方案》文章介绍了在SpringBoot中实现多数据源连接和切换的几种方案,并详细描述了一个使用AbstractRoutingDataSource的实... 目录前言一、多数据源配置与切换方案二、实现步骤总结前言在 Spring Boot 中实现多数据源连接

C#图表开发之Chart详解

《C#图表开发之Chart详解》C#中的Chart控件用于开发图表功能,具有Series和ChartArea两个重要属性,Series属性是SeriesCollection类型,包含多个Series对... 目录OverviChina编程ewSeries类总结OverviewC#中,开发图表功能的控件是Char

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

MyBatis 切换不同的类型数据库方案

下属案例例当前结合SpringBoot 配置进行讲解。 背景: 实现一个工程里面在部署阶段支持切换不同类型数据库支持。 方案一 数据源配置 关键代码(是什么数据库,该怎么配就怎么配) spring:datasource:name: test# 使用druid数据源type: com.alibaba.druid.pool.DruidDataSource# @需要修改 数据库连接及驱动u

解决Office Word不能切换中文输入

我们在使用WORD的时可能会经常碰到WORD中无法输入中文的情况。因为,虽然我们安装了搜狗输入法,但是到我们在WORD中使用搜狗的输入法的切换中英文的按键的时候会发现根本没有效果,无法将输入法切换成中文的。下面我就介绍一下如何在WORD中把搜狗输入法切换到中文。