Skip to content

Commit e869738

Browse files
committed
feat: add options for autoresize
* add `throttle: number` to specify throttle delay (#419 #562) * add `onResize: () => void` to specify a resize callback (#714)
1 parent 47f7885 commit e869738

File tree

8 files changed

+98
-55
lines changed

8 files changed

+98
-55
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ Drop `<script>` inside your HTML file and access the component via `window.VueEC
225225

226226
<!-- vue3Scripts:start -->
227227
```html
228-
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.47"></script>
228+
<script src="https://cdn.jsdelivr.net/npm/vue@3.3.4"></script>
229229
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
230-
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.5.5"></script>
230+
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.6.0"></script>
231231
```
232232
<!-- vue3Scripts:end -->
233233

@@ -247,7 +247,7 @@ app.component('v-chart', VueECharts)
247247
```html
248248
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
249249
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
250-
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.5.5"></script>
250+
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.6.0"></script>
251251
```
252252
<!-- vue2Scripts:end -->
253253

@@ -290,9 +290,9 @@ See more examples [here](https://github.com/ecomfe/vue-echarts/tree/main/src/dem
290290

291291
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)
292292

293-
- `autoresize: boolean` (default: `false`)
293+
- `autoresize: boolean | { throttle?: number, onResize?: () => void }` (default: `false`)
294294

295-
Whether the chart should be resized automatically whenever its root is resized.
295+
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.
296296

297297
- `loading: boolean` (default: `false`)
298298

README.zh-Hans.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ import "echarts";
225225

226226
<!-- vue3Scripts:start -->
227227
```html
228-
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.47"></script>
228+
<script src="https://cdn.jsdelivr.net/npm/vue@3.3.4"></script>
229229
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
230-
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.5.5"></script>
230+
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.6.0"></script>
231231
```
232232
<!-- vue3Scripts:end -->
233233

@@ -247,7 +247,7 @@ app.component('v-chart', VueECharts)
247247
```html
248248
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
249249
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
250-
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.5.5"></script>
250+
<script src="https://cdn.jsdelivr.net/npm/vue-echarts@6.6.0"></script>
251251
```
252252
<!-- vue2Scripts:end -->
253253

@@ -290,9 +290,9 @@ Vue.component("v-chart", VueECharts);
290290

291291
图表的分组,用于[联动](https://echarts.apache.org/zh/api.html#echarts.connect)。请参考 `echartsInstance.group`[前往 →](https://echarts.apache.org/zh/api.html#echartsInstance.group)
292292

293-
- `autoresize: boolean`(默认值`false`
293+
- `autoresize: boolean | { throttle?: number, onResize?: () => void }`(默认值`false`
294294

295-
图表在组件根元素尺寸变化时是否需要自动进行重绘。
295+
图表在组件根元素尺寸变化时是否需要自动进行重绘。也可以传入一个选项对象来指定自定义的节流延迟和尺寸变化时的额外回调函数。
296296

297297
- `loading: boolean`(默认值:`false`
298298

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-echarts",
3-
"version": "6.5.5",
3+
"version": "6.6.0",
44
"description": "Vue.js component for Apache ECharts.",
55
"author": "GU Yiling <[email protected]>",
66
"scripts": {

pnpm-lock.yaml

Lines changed: 41 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/docs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { readFile, writeFile } = fs.promises;
88
const CDN_PREFIX = "https://cdn.jsdelivr.net/npm/";
99

1010
const DEP_VERSIONS = {
11-
"vue@3": "3.2.47",
11+
"vue@3": "3.3.4",
1212
"vue@2": "2.7.14",
1313
echarts: "5.4.2",
1414
[name]: version

src/composables/autoresize.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
1-
import { Ref, watch } from "vue-demi";
1+
import { watch, type Ref, type PropType } from "vue-demi";
22
import { throttle } from "echarts/core";
3-
import { addListener, removeListener, ResizeCallback } from "resize-detector";
4-
import { EChartsType } from "../types";
3+
import {
4+
addListener,
5+
removeListener,
6+
type ResizeCallback
7+
} from "resize-detector";
8+
import { type EChartsType } from "../types";
9+
10+
type AutoresizeProp =
11+
| boolean
12+
| {
13+
throttle?: number;
14+
onResize?: () => void;
15+
};
516

617
export function useAutoresize(
718
chart: Ref<EChartsType | undefined>,
8-
autoresize: Ref<boolean>,
19+
autoresize: Ref<AutoresizeProp | undefined>,
920
root: Ref<HTMLElement | undefined>
1021
): void {
1122
let resizeListener: ResizeCallback | null = null;
1223

1324
watch([root, chart, autoresize], ([root, chart, autoresize], _, cleanup) => {
1425
if (root && chart && autoresize) {
15-
resizeListener = throttle(() => {
26+
const autoresizeOptions = autoresize === true ? {} : autoresize;
27+
const { throttle: wait = 100, onResize } = autoresizeOptions;
28+
29+
const callback = () => {
1630
chart.resize();
17-
}, 100);
31+
onResize?.();
32+
};
1833

34+
resizeListener = wait ? throttle(callback, wait) : callback;
1935
addListener(root, resizeListener);
2036
}
2137

2238
cleanup(() => {
23-
if (resizeListener && root) {
39+
if (root && resizeListener) {
2440
removeListener(root, resizeListener);
2541
}
2642
});
2743
});
2844
}
2945

3046
export const autoresizeProps = {
31-
autoresize: Boolean
47+
autoresize: [Boolean, Object] as PropType<AutoresizeProp>
3248
};

src/composables/loading.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ import {
44
computed,
55
watchEffect,
66
type Ref,
7-
type InjectionKey
7+
type InjectionKey,
8+
type PropType
89
} from "vue-demi";
9-
import { EChartsType } from "../types";
10+
import type { EChartsType, LoadingOptions } from "../types";
1011

1112
export const LOADING_OPTIONS_KEY =
1213
"ecLoadingOptions" as unknown as InjectionKey<
13-
UnknownRecord | Ref<UnknownRecord>
14+
LoadingOptions | Ref<LoadingOptions>
1415
>;
1516

16-
type UnknownRecord = Record<string, unknown>;
17-
1817
export function useLoading(
1918
chart: Ref<EChartsType | undefined>,
2019
loading: Ref<boolean>,
21-
loadingOptions: Ref<UnknownRecord | undefined>
20+
loadingOptions: Ref<LoadingOptions | undefined>
2221
): void {
2322
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {});
2423
const realLoadingOptions = computed(() => ({
@@ -42,5 +41,5 @@ export function useLoading(
4241

4342
export const loadingProps = {
4443
loading: Boolean,
45-
loadingOptions: Object
44+
loadingOptions: Object as PropType<LoadingOptions>
4645
};

src/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ export type EventTarget = EChartsType | ZRenderType;
2525
type SetOptionType = EChartsType["setOption"];
2626
export type Option = Parameters<SetOptionType>[0];
2727

28+
export type LoadingOptions = {
29+
text?: string;
30+
textColor?: string;
31+
fontSize?: number | string;
32+
fontWeight?: number | string;
33+
fontStyle?: string;
34+
fontFamily?: string;
35+
maskColor?: string;
36+
showSpinner?: boolean;
37+
color?: string;
38+
spinnerRadius?: number;
39+
lineWidth?: number;
40+
zlevel?: number;
41+
};
42+
2843
type MouseEventName =
2944
| "click"
3045
| "dblclick"

0 commit comments

Comments
 (0)