feat: add options for autoresize

* add `throttle: number` to specify throttle delay (#419 #562)
* add `onResize: () => void` to specify a resize callback (#714)
This commit is contained in:
Justineo
2023-06-13 20:35:45 +08:00
parent 47f7885f19
commit e8697382a1
8 changed files with 99 additions and 56 deletions

View File

@ -225,9 +225,9 @@ Drop `<script>` inside your HTML file and access the component via `window.VueEC
<!-- vue3Scripts:start --> <!-- vue3Scripts:start -->
```html ```html
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.47"></script> <script src="https://cdn.jsdelivr.net/npm/vue@3.3.4"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2"></script> <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.5.5"></script> <script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.6.0"></script>
``` ```
<!-- vue3Scripts:end --> <!-- vue3Scripts:end -->
@ -247,7 +247,7 @@ app.component('v-chart', VueECharts)
```html ```html
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14"></script> <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2"></script> <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.5.5"></script> <script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.6.0"></script>
``` ```
<!-- vue2Scripts:end --> <!-- vue2Scripts:end -->
@ -290,9 +290,9 @@ See more examples [here](https://github.com/ecomfe/vue-echarts/tree/main/src/dem
Group name to be used in chart [connection](https://echarts.apache.org/en/api.html#echarts.connect). See `echartsInstance.group` [here →](https://echarts.apache.org/en/api.html#echartsInstance.group) Group name to be used in chart [connection](https://echarts.apache.org/en/api.html#echarts.connect). See `echartsInstance.group` [here →](https://echarts.apache.org/en/api.html#echartsInstance.group)
- `autoresize: boolean` (default: `false`) - `autoresize: boolean | { throttle?: number, onResize?: () => void }` (default: `false`)
Whether the chart should be resized automatically whenever its root is resized. Whether the chart should be resized automatically whenever its root is resized. Use the options object to specify a custom throttle delay (in milliseconds) and/or an extra resize callback function.
- `loading: boolean` (default: `false`) - `loading: boolean` (default: `false`)

View File

@ -225,9 +225,9 @@ import "echarts";
<!-- vue3Scripts:start --> <!-- vue3Scripts:start -->
```html ```html
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.47"></script> <script src="https://cdn.jsdelivr.net/npm/vue@3.3.4"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2"></script> <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.5.5"></script> <script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.6.0"></script>
``` ```
<!-- vue3Scripts:end --> <!-- vue3Scripts:end -->
@ -247,7 +247,7 @@ app.component('v-chart', VueECharts)
```html ```html
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14"></script> <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2"></script> <script src="https://cdn.jsdelivr.net/npm/echarts@5.4.2"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.5.5"></script> <script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.6.0"></script>
``` ```
<!-- vue2Scripts:end --> <!-- vue2Scripts:end -->
@ -290,9 +290,9 @@ Vue.component("v-chart", VueECharts);
图表的分组,用于[联动](https://echarts.apache.org/zh/api.html#echarts.connect)。请参考 `echartsInstance.group`。[前往 →](https://echarts.apache.org/zh/api.html#echartsInstance.group) 图表的分组,用于[联动](https://echarts.apache.org/zh/api.html#echarts.connect)。请参考 `echartsInstance.group`。[前往 →](https://echarts.apache.org/zh/api.html#echartsInstance.group)
- `autoresize: boolean`(默认值`false` - `autoresize: boolean | { throttle?: number, onResize?: () => void }`(默认值`false`
图表在组件根元素尺寸变化时是否需要自动进行重绘。 图表在组件根元素尺寸变化时是否需要自动进行重绘。也可以传入一个选项对象来指定自定义的节流延迟和尺寸变化时的额外回调函数。
- `loading: boolean`(默认值:`false` - `loading: boolean`(默认值:`false`

View File

@ -1,6 +1,6 @@
{ {
"name": "vue-echarts", "name": "vue-echarts",
"version": "6.5.5", "version": "6.6.0",
"description": "Vue.js component for Apache ECharts.", "description": "Vue.js component for Apache ECharts.",
"author": "GU Yiling <justice360@gmail.com>", "author": "GU Yiling <justice360@gmail.com>",
"scripts": { "scripts": {

69
pnpm-lock.yaml generated
View File

@ -160,6 +160,15 @@ packages:
'@babel/highlight': 7.18.6 '@babel/highlight': 7.18.6
dev: true dev: true
/@babel/code-frame@7.22.5:
resolution: {integrity: sha512-Xmwn266vad+6DAqEB2A6V/CcZVp62BbwVmcOJc2RPuwih1kw02TjQvWVWlcKGbBPd+8/0V5DEkOcizRGYsspYQ==}
engines: {node: '>=6.9.0'}
requiresBuild: true
dependencies:
'@babel/highlight': 7.22.5
dev: true
optional: true
/@babel/compat-data@7.18.6: /@babel/compat-data@7.18.6:
resolution: {integrity: sha512-tzulrgDT0QD6U7BJ4TKVk2SDDg7wlP39P9yAx1RfLy7vP/7rsDRlWVfbWxElslu56+r7QOhB2NSDsabYYruoZQ==} resolution: {integrity: sha512-tzulrgDT0QD6U7BJ4TKVk2SDDg7wlP39P9yAx1RfLy7vP/7rsDRlWVfbWxElslu56+r7QOhB2NSDsabYYruoZQ==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
@ -403,6 +412,12 @@ packages:
resolution: {integrity: sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==} resolution: {integrity: sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
/@babel/helper-validator-identifier@7.22.5:
resolution: {integrity: sha512-aJXu+6lErq8ltp+JhkJUfk1MTGyuA4v7f3pA+BJ5HLfNC6nAQ0Cpi9uOquUj8Hehg0aUiHzWQbOVJGao6ztBAQ==}
engines: {node: '>=6.9.0'}
dev: true
optional: true
/@babel/helper-validator-option@7.18.6: /@babel/helper-validator-option@7.18.6:
resolution: {integrity: sha512-XO7gESt5ouv/LRJdrVjkShckw6STTaB7l9BrpBaAHDeF5YZT+01PCwmR0SJHnkW6i8OwW/EVWRShfi4j2x+KQw==} resolution: {integrity: sha512-XO7gESt5ouv/LRJdrVjkShckw6STTaB7l9BrpBaAHDeF5YZT+01PCwmR0SJHnkW6i8OwW/EVWRShfi4j2x+KQw==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
@ -440,6 +455,16 @@ packages:
js-tokens: 4.0.0 js-tokens: 4.0.0
dev: true dev: true
/@babel/highlight@7.22.5:
resolution: {integrity: sha512-BSKlD1hgnedS5XRnGOljZawtag7H1yPfQp0tdNJCHoH6AZ+Pcm9VvkrK59/Yy593Ypg0zMxH2BxD1VPYUQ7UIw==}
engines: {node: '>=6.9.0'}
dependencies:
'@babel/helper-validator-identifier': 7.22.5
chalk: 2.4.2
js-tokens: 4.0.0
dev: true
optional: true
/@babel/parser@7.18.6: /@babel/parser@7.18.6:
resolution: {integrity: sha512-uQVSa9jJUe/G/304lXspfWVpKpK4euFLgGiMQFOCpM/bgcAdeoHwi/OQz23O9GK2osz26ZiXRRV9aV+Yl1O8tw==} resolution: {integrity: sha512-uQVSa9jJUe/G/304lXspfWVpKpK4euFLgGiMQFOCpM/bgcAdeoHwi/OQz23O9GK2osz26ZiXRRV9aV+Yl1O8tw==}
engines: {node: '>=6.0.0'} engines: {node: '>=6.0.0'}
@ -448,14 +473,6 @@ packages:
'@babel/types': 7.18.7 '@babel/types': 7.18.7
dev: true dev: true
/@babel/parser@7.20.7:
resolution: {integrity: sha512-T3Z9oHybU+0vZlY9CiDSJQTD5ZapcW18ZctFMi0MOAl/4BjFF4ul7NVSARLdbGO5vDqy9eQiGTV0LtKfvCYvcg==}
engines: {node: '>=6.0.0'}
hasBin: true
dependencies:
'@babel/types': 7.20.7
dev: true
/@babel/parser@7.22.5: /@babel/parser@7.22.5:
resolution: {integrity: sha512-DFZMC9LJUG9PLOclRC32G63UXwzqS2koQC8dkx+PLdmt1xSePYpbT/NbsrJy8Q/muXz7o/h/d4A7Fuyixm559Q==} resolution: {integrity: sha512-DFZMC9LJUG9PLOclRC32G63UXwzqS2koQC8dkx+PLdmt1xSePYpbT/NbsrJy8Q/muXz7o/h/d4A7Fuyixm559Q==}
engines: {node: '>=6.0.0'} engines: {node: '>=6.0.0'}
@ -2469,8 +2486,8 @@ packages:
/@vue/compiler-sfc@2.7.14: /@vue/compiler-sfc@2.7.14:
resolution: {integrity: sha512-aNmNHyLPsw+sVvlQFQ2/8sjNuLtK54TC6cuKnVzAY93ks4ZBrvwQSnkkIh7bsbNhum5hJBS00wSDipQ937f5DA==} resolution: {integrity: sha512-aNmNHyLPsw+sVvlQFQ2/8sjNuLtK54TC6cuKnVzAY93ks4ZBrvwQSnkkIh7bsbNhum5hJBS00wSDipQ937f5DA==}
dependencies: dependencies:
'@babel/parser': 7.20.7 '@babel/parser': 7.22.5
postcss: 8.4.21 postcss: 8.4.14
source-map: 0.6.1 source-map: 0.6.1
dev: true dev: true
@ -2528,7 +2545,7 @@ packages:
source-map: 0.6.1 source-map: 0.6.1
vue-template-es2015-compiler: 1.9.1 vue-template-es2015-compiler: 1.9.1
optionalDependencies: optionalDependencies:
prettier: 2.8.2 prettier: 2.7.1
transitivePeerDependencies: transitivePeerDependencies:
- arc-templates - arc-templates
- atpl - atpl
@ -3005,7 +3022,7 @@ packages:
postcss: ^8.1.0 postcss: ^8.1.0
dependencies: dependencies:
browserslist: 4.21.1 browserslist: 4.21.1
caniuse-lite: 1.0.30001442 caniuse-lite: 1.0.30001502
fraction.js: 4.2.0 fraction.js: 4.2.0
normalize-range: 0.1.2 normalize-range: 0.1.2
picocolors: 1.0.0 picocolors: 1.0.0
@ -3159,7 +3176,7 @@ packages:
'@types/semver': 7.3.10 '@types/semver': 7.3.10
'@types/ua-parser-js': 0.7.36 '@types/ua-parser-js': 0.7.36
browserslist: 4.20.2 browserslist: 4.20.2
caniuse-lite: 1.0.30001442 caniuse-lite: 1.0.30001502
isbot: 3.4.5 isbot: 3.4.5
object-path: 0.11.8 object-path: 0.11.8
semver: 7.3.7 semver: 7.3.7
@ -3171,7 +3188,7 @@ packages:
engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7} engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7}
hasBin: true hasBin: true
dependencies: dependencies:
caniuse-lite: 1.0.30001442 caniuse-lite: 1.0.30001502
electron-to-chromium: 1.4.180 electron-to-chromium: 1.4.180
escalade: 3.1.1 escalade: 3.1.1
node-releases: 2.0.5 node-releases: 2.0.5
@ -3183,7 +3200,7 @@ packages:
engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7} engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7}
hasBin: true hasBin: true
dependencies: dependencies:
caniuse-lite: 1.0.30001442 caniuse-lite: 1.0.30001502
electron-to-chromium: 1.4.180 electron-to-chromium: 1.4.180
node-releases: 2.0.5 node-releases: 2.0.5
update-browserslist-db: 1.0.4(browserslist@4.21.1) update-browserslist-db: 1.0.4(browserslist@4.21.1)
@ -3248,13 +3265,13 @@ packages:
resolution: {integrity: sha512-bsTwuIg/BZZK/vreVTYYbSWoe2F+71P7K5QGEX+pT250DZbfU1MQ5prOKpPR+LL6uWKK3KMwMCAS74QB3Um1uw==} resolution: {integrity: sha512-bsTwuIg/BZZK/vreVTYYbSWoe2F+71P7K5QGEX+pT250DZbfU1MQ5prOKpPR+LL6uWKK3KMwMCAS74QB3Um1uw==}
dependencies: dependencies:
browserslist: 4.21.1 browserslist: 4.21.1
caniuse-lite: 1.0.30001442 caniuse-lite: 1.0.30001502
lodash.memoize: 4.1.2 lodash.memoize: 4.1.2
lodash.uniq: 4.5.0 lodash.uniq: 4.5.0
dev: true dev: true
/caniuse-lite@1.0.30001442: /caniuse-lite@1.0.30001502:
resolution: {integrity: sha512-239m03Pqy0hwxYPYR5JwOIxRJfLTWtle9FV8zosfV5pHg+/51uD4nxcUlM8+mWWGfwKtt8lJNHnD3cWw9VZ6ow==} resolution: {integrity: sha512-AZ+9tFXw1sS0o0jcpJQIXvFTOB/xGiQ4OQ2t98QX3NDn2EZTSRBC801gxrsGgViuq2ak/NLkNgSNEPtCr5lfKg==}
dev: true dev: true
/case-sensitive-paths-webpack-plugin@2.4.0: /case-sensitive-paths-webpack-plugin@2.4.0:
@ -3976,6 +3993,10 @@ packages:
/csstype@3.1.1: /csstype@3.1.1:
resolution: {integrity: sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==} resolution: {integrity: sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==}
/csstype@3.1.2:
resolution: {integrity: sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==}
dev: true
/debug@2.6.9: /debug@2.6.9:
resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==} resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==}
peerDependencies: peerDependencies:
@ -6843,14 +6864,6 @@ packages:
hasBin: true hasBin: true
dev: true dev: true
/prettier@2.8.2:
resolution: {integrity: sha512-BtRV9BcncDyI2tsuS19zzhzoxD8Dh8LiCx7j7tHzrkz8GFXAexeWFdi22mjE1d16dftH2qNaytVxqiRTGlMfpw==}
engines: {node: '>=10.13.0'}
hasBin: true
requiresBuild: true
dev: true
optional: true
/pretty-error@4.0.0: /pretty-error@4.0.0:
resolution: {integrity: sha512-AoJ5YMAcXKYxKhuJGdcvse+Voc6v1RgnsR3nWcYU7q4t6z0Q6T86sv5Zq8VIRbOWWFpvdGE83LtdSMNd+6Y0xw==} resolution: {integrity: sha512-AoJ5YMAcXKYxKhuJGdcvse+Voc6v1RgnsR3nWcYU7q4t6z0Q6T86sv5Zq8VIRbOWWFpvdGE83LtdSMNd+6Y0xw==}
dependencies: dependencies:
@ -7150,7 +7163,7 @@ packages:
rollup: 2.75.7 rollup: 2.75.7
typescript: 4.6.4 typescript: 4.6.4
optionalDependencies: optionalDependencies:
'@babel/code-frame': 7.18.6 '@babel/code-frame': 7.22.5
dev: true dev: true
/rollup-plugin-styles@4.0.0(rollup@2.75.7): /rollup-plugin-styles@4.0.0(rollup@2.75.7):
@ -8160,7 +8173,7 @@ packages:
resolution: {integrity: sha512-b2qkFyOM0kwqWFuQmgd4o+uHGU7T+2z3T+WQp8UBjADfEv2n4FEMffzBmCKNP0IGzOEEfYjvtcC62xaSKeQDrQ==} resolution: {integrity: sha512-b2qkFyOM0kwqWFuQmgd4o+uHGU7T+2z3T+WQp8UBjADfEv2n4FEMffzBmCKNP0IGzOEEfYjvtcC62xaSKeQDrQ==}
dependencies: dependencies:
'@vue/compiler-sfc': 2.7.14 '@vue/compiler-sfc': 2.7.14
csstype: 3.1.1 csstype: 3.1.2
dev: true dev: true
/vue@3.3.4: /vue@3.3.4:

View File

@ -8,7 +8,7 @@ const { readFile, writeFile } = fs.promises;
const CDN_PREFIX = "https://cdn.jsdelivr.net/npm/"; const CDN_PREFIX = "https://cdn.jsdelivr.net/npm/";
const DEP_VERSIONS = { const DEP_VERSIONS = {
"vue@3": "3.2.47", "vue@3": "3.3.4",
"vue@2": "2.7.14", "vue@2": "2.7.14",
echarts: "5.4.2", echarts: "5.4.2",
[name]: version [name]: version

View File

@ -1,26 +1,42 @@
import { Ref, watch } from "vue-demi"; import { watch, type Ref, type PropType } from "vue-demi";
import { throttle } from "echarts/core"; import { throttle } from "echarts/core";
import { addListener, removeListener, ResizeCallback } from "resize-detector"; import {
import { EChartsType } from "../types"; addListener,
removeListener,
type ResizeCallback
} from "resize-detector";
import { type EChartsType } from "../types";
type AutoresizeProp =
| boolean
| {
throttle?: number;
onResize?: () => void;
};
export function useAutoresize( export function useAutoresize(
chart: Ref<EChartsType | undefined>, chart: Ref<EChartsType | undefined>,
autoresize: Ref<boolean>, autoresize: Ref<AutoresizeProp | undefined>,
root: Ref<HTMLElement | undefined> root: Ref<HTMLElement | undefined>
): void { ): void {
let resizeListener: ResizeCallback | null = null; let resizeListener: ResizeCallback | null = null;
watch([root, chart, autoresize], ([root, chart, autoresize], _, cleanup) => { watch([root, chart, autoresize], ([root, chart, autoresize], _, cleanup) => {
if (root && chart && autoresize) { if (root && chart && autoresize) {
resizeListener = throttle(() => { const autoresizeOptions = autoresize === true ? {} : autoresize;
chart.resize(); const { throttle: wait = 100, onResize } = autoresizeOptions;
}, 100);
const callback = () => {
chart.resize();
onResize?.();
};
resizeListener = wait ? throttle(callback, wait) : callback;
addListener(root, resizeListener); addListener(root, resizeListener);
} }
cleanup(() => { cleanup(() => {
if (resizeListener && root) { if (root && resizeListener) {
removeListener(root, resizeListener); removeListener(root, resizeListener);
} }
}); });
@ -28,5 +44,5 @@ export function useAutoresize(
} }
export const autoresizeProps = { export const autoresizeProps = {
autoresize: Boolean autoresize: [Boolean, Object] as PropType<AutoresizeProp>
}; };

View File

@ -4,21 +4,20 @@ import {
computed, computed,
watchEffect, watchEffect,
type Ref, type Ref,
type InjectionKey type InjectionKey,
type PropType
} from "vue-demi"; } from "vue-demi";
import { EChartsType } from "../types"; import type { EChartsType, LoadingOptions } from "../types";
export const LOADING_OPTIONS_KEY = export const LOADING_OPTIONS_KEY =
"ecLoadingOptions" as unknown as InjectionKey< "ecLoadingOptions" as unknown as InjectionKey<
UnknownRecord | Ref<UnknownRecord> LoadingOptions | Ref<LoadingOptions>
>; >;
type UnknownRecord = Record<string, unknown>;
export function useLoading( export function useLoading(
chart: Ref<EChartsType | undefined>, chart: Ref<EChartsType | undefined>,
loading: Ref<boolean>, loading: Ref<boolean>,
loadingOptions: Ref<UnknownRecord | undefined> loadingOptions: Ref<LoadingOptions | undefined>
): void { ): void {
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {}); const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {});
const realLoadingOptions = computed(() => ({ const realLoadingOptions = computed(() => ({
@ -42,5 +41,5 @@ export function useLoading(
export const loadingProps = { export const loadingProps = {
loading: Boolean, loading: Boolean,
loadingOptions: Object loadingOptions: Object as PropType<LoadingOptions>
}; };

View File

@ -25,6 +25,21 @@ export type EventTarget = EChartsType | ZRenderType;
type SetOptionType = EChartsType["setOption"]; type SetOptionType = EChartsType["setOption"];
export type Option = Parameters<SetOptionType>[0]; export type Option = Parameters<SetOptionType>[0];
export type LoadingOptions = {
text?: string;
textColor?: string;
fontSize?: number | string;
fontWeight?: number | string;
fontStyle?: string;
fontFamily?: string;
maskColor?: string;
showSpinner?: boolean;
color?: string;
spinnerRadius?: number;
lineWidth?: number;
zlevel?: number;
};
type MouseEventName = type MouseEventName =
| "click" | "click"
| "dblclick" | "dblclick"