Vue3 + echarts 统一封装
1. 新建 echartsLib.js 文件,统一导入需要的组件
import * as echarts from "echarts/core";
import { SVGRenderer, CanvasRenderer } from "echarts/renderers";
import { BarChart, LineChart, PieChart } from "echarts/charts";
import {
TitleComponent,
TooltipComponent,
GridComponent,
LegendComponent,
ToolboxComponent,
DataZoomComponent,
TimelineComponent,
} from "echarts/components";
echarts.use([
BarChart,
LineChart,
PieChart,
TitleComponent,
TooltipComponent,
LegendComponent,
GridComponent,
ToolboxComponent,
DataZoomComponent,
TimelineComponent,
CanvasRenderer
]);
export default echarts;
2. 新建 newChart.js 文件封装用法
import { nextTick, onMounted, onUnmounted, unref } from "vue";
import echarts from "./echartsLib.js";
import { SVGRenderer, CanvasRenderer } from "echarts/renderers";
export default function newChart(
elRef,
autoChartSize = false,
animation = false,
theme = "default"
) {
// 设置渲染模式
echarts.use(CanvasRenderer);
// echart实例
let chartInstance = null;
// 初始化图表
const initCharts = () => {
const el = unref(elRef);
if (!el || !unref(el)) {
return;
}
chartInstance = echarts.init(el, theme);
};
const setOption = (option) => {
nextTick(() => {
if (!chartInstance) {
initCharts();
if (!chartInstance) return;
}
chartInstance.setOption(option);
hideLoading();
});
};
// 获取echart实例
function getInstance() {
if (!chartInstance) {
initCharts();
}
return chartInstance;
}
function resize() {
chartInstance?.resize();
}
// 监听元素大小
function watchEl() {
// 添加过渡
if (animation) {
elRef.value.style.transition = "width 1s, height 1s";
}
const resizeObserver = new ResizeObserver((entries) => resize());
resizeObserver.observe(elRef.value);
}
// 显示加载
function showLoading() {
if (!chartInstance) {
initCharts();
}
chartInstance?.showLoading();
}
// 隐藏加载
function hideLoading() {
if (!chartInstance) {
initCharts();
}
chartInstance?.hideLoading();
}
onMounted(() => {
window.addEventListener("resize", resize);
if (autoChartSize) watchEl();
});
onUnmounted(() => {
window.removeEventListener("resize", resize);
});
return {
setOption,
getInstance,
showLoading,
hideLoading,
};
}
3. 使用
<div ref="myChart"></div>
import newChart from "@/utils/newChart";
import { reactive, onMounted, ref } from "vue";
const myChart = ref(null);
const { setOption } = newChart(myChart, true, true);
let option = {}
onMounted(() => {
setOption(option);
});
Vue3 + echarts 统一封装的更多相关文章
- 基于SqlSugar的开发框架循序渐进介绍(10)-- 利用axios组件的封装,实现对后端API数据的访问和基类的统一封装处理
在SqlSugar的开发框架的后端,我们基于Web API的封装了统一的返回结果,使得WebAPI的接口返回值更加简洁,而在前端,我们也需要统一对返回的结果进行解析,并获取和Web API接口对应的数 ...
- SpringBoot异常处理统一封装我来做-使用篇
SpringBoot异常处理统一封装我来做-使用篇 简介 重复功能我来写.在 SpringBoot 项目里都有全局异常处理以及返回包装等,返回前端是带上succ.code.msg.data等字段.单个 ...
- api接口统一封装
具体的接口api模块,例如authorization.js import axios from '../axiosWrapper' let prefix = process.env.API_ROOT ...
- SpringCloud异常处理统一封装我来做-使用篇
SpringCloud异常处理统一封装我来做-使用篇 简介 重复功能我来写.在 SpringBoot 项目里都有全局异常处理以及返回包装等,返回前端是带上succ.code.msg.data等字段.单 ...
- SpringBoot接口 - 如何优雅的对接口返回内容统一封装?
在以SpringBoot开发Restful接口时,统一返回方便前端进行开发和封装,以及出现时给出响应编码和信息.@pdai SpringBoot接口 - 如何优雅的对接口返回内容统一封装? RESTf ...
- ECharts图表——封装通用配置
前言 前段时间在做大屏项目,大量用到echarts图表,大屏对设计规范要求比较高,而大屏项目,经常会因为业务方面的原因.或者是数据方面的原因改动UI设计,所有图表的代码也是三天一小改.五天一大改 因此 ...
- vue3 专用 indexedDB 封装库,基于Promise告别回调地狱
IndexedDB 的官网 https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_API 这个大概是官网吧,原始是英文的,现在陆续是出中 ...
- 简单方便统一封装的傻瓜式GET/POST库AliasNet正式公布~开源喽~
在进行网页自动化时我们做得最多的工作就是不停的往某个URL GET/POST数据并得到相应的Response,通过分析Response的结果再进行下一步操作,通过网页自动化我们可以做很多工作,比如去某 ...
- Echarts后台封装option对象
该方法返回的keyword指向了前台负责图表显示的jsp页面 public String keyword() { if(this.dateNum == null || this.dateNum.equ ...
- axios统一封装
本文代码参考了网上别人的资料,经过修改而来 /** * Created by zxf on 2017/9/6. * 封装统一的ajax请求,统一拦截请求,对不同的请求状态封装 * 通常说, ajax ...
随机推荐
- 【转载】VMWare 各版本下载地址【centos7安装gerrit】
https://blog.csdn.net/weixin_44129085/article/details/110443135 centos7安装gerrit https://blog.csdn.ne ...
- NanoPi R1 安装 python环境 及opencv
(友善NanoPi 1G RAM/8GB eMMC) 安装python2/python3 pip/pip3 环境 sudo apt-get install python sudo apt-get ...
- Delphi 自定义窗体类名
原理就是覆盖原CreateParams函数,重写新CreateParams函数,在新CreateParams函数继承完之后马上修改Parames.WinClassName type TForm1 = ...
- html入門 如何 區別行級元素 和 塊級元素
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- ffmpeg 截取视频
ffmpeg -ss 00:00:03 -t 22 -i output20230301.mp4 output20230301_0.mp4 -ss:开始时间 -t:持续时间 -i:输入 接着是输出
- Vue 使用Lodop进行标签(条码)打印
一.使用到的插件:vue-barcode(vue条形码插件),Lodop打印控件(我这里使用windows64版,所以以此进行举例说明.). 详述:前者(指vue-barcode)针对在前端界面上观察 ...
- NLP-transformer-分词库用法
NLP-transformer-分词库用法 参考文档: https://blog.csdn.net/orangerfun/article/details/124089467 1 pip install ...
- 'xxx' must be unique because it is referenced by a foreign key.
'xxx' must be unique because it is referenced by a foreign key. 原因:在绑定外键时,对应的外键字段的没有设置成唯一. 说明:在定义字段时 ...
- 修改Linux终端默认编辑器,实现代码高亮
echo export EDITOR=vim >>/etc/profile.d/env.sh
- 397. 整数替换 (Medium
问题描述 397. 整数替换 (Medium) 给定一个正整数 n ,你可以做如下操作: 如果 n 是偶数,则用 n / 2 替换 n. 如果 n 是奇数,则可以用 n + 1 或 n - 1 替换 ...