Skip to content

Commit

Permalink
feat: add options for autoresize
Browse files Browse the repository at this point in the history
* add `throttle: number` to specify throttle delay (#419 #562)
* add `onResize: () => void` to specify a resize callback (#714)
  • Loading branch information
Justineo committed Jun 13, 2023
1 parent 47f7885 commit e869738
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 55 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ Drop `<script>` inside your HTML file and access the component via `window.VueEC

<!-- vue3Scripts:start -->
```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/[email protected]"></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 -->

Expand All @@ -247,7 +247,7 @@ app.component('v-chart', VueECharts)
```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></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 -->

Expand Down Expand Up @@ -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)

- `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`)

Expand Down
10 changes: 5 additions & 5 deletions README.zh-Hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ import "echarts";

<!-- vue3Scripts:start -->
```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/[email protected]"></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 -->

Expand All @@ -247,7 +247,7 @@ app.component('v-chart', VueECharts)
```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></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 -->

Expand Down Expand Up @@ -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)

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-echarts",
"version": "6.5.5",
"version": "6.6.0",
"description": "Vue.js component for Apache ECharts.",
"author": "GU Yiling <[email protected]>",
"scripts": {
Expand Down
69 changes: 41 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { readFile, writeFile } = fs.promises;
const CDN_PREFIX = "https://cdn.jsdelivr.net/npm/";

const DEP_VERSIONS = {
"vue@3": "3.2.47",
"vue@3": "3.3.4",
"vue@2": "2.7.14",
echarts: "5.4.2",
[name]: version
Expand Down
32 changes: 24 additions & 8 deletions src/composables/autoresize.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
import { Ref, watch } from "vue-demi";
import { watch, type Ref, type PropType } from "vue-demi";
import { throttle } from "echarts/core";
import { addListener, removeListener, ResizeCallback } from "resize-detector";
import { EChartsType } from "../types";
import {
addListener,
removeListener,
type ResizeCallback
} from "resize-detector";
import { type EChartsType } from "../types";

type AutoresizeProp =
| boolean
| {
throttle?: number;
onResize?: () => void;
};

export function useAutoresize(
chart: Ref<EChartsType | undefined>,
autoresize: Ref<boolean>,
autoresize: Ref<AutoresizeProp | undefined>,
root: Ref<HTMLElement | undefined>
): void {
let resizeListener: ResizeCallback | null = null;

watch([root, chart, autoresize], ([root, chart, autoresize], _, cleanup) => {
if (root && chart && autoresize) {
resizeListener = throttle(() => {
const autoresizeOptions = autoresize === true ? {} : autoresize;
const { throttle: wait = 100, onResize } = autoresizeOptions;

const callback = () => {
chart.resize();
}, 100);
onResize?.();
};

resizeListener = wait ? throttle(callback, wait) : callback;
addListener(root, resizeListener);
}

cleanup(() => {
if (resizeListener && root) {
if (root && resizeListener) {
removeListener(root, resizeListener);
}
});
});
}

export const autoresizeProps = {
autoresize: Boolean
autoresize: [Boolean, Object] as PropType<AutoresizeProp>
};
13 changes: 6 additions & 7 deletions src/composables/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import {
computed,
watchEffect,
type Ref,
type InjectionKey
type InjectionKey,
type PropType
} from "vue-demi";
import { EChartsType } from "../types";
import type { EChartsType, LoadingOptions } from "../types";

export const LOADING_OPTIONS_KEY =
"ecLoadingOptions" as unknown as InjectionKey<
UnknownRecord | Ref<UnknownRecord>
LoadingOptions | Ref<LoadingOptions>
>;

type UnknownRecord = Record<string, unknown>;

export function useLoading(
chart: Ref<EChartsType | undefined>,
loading: Ref<boolean>,
loadingOptions: Ref<UnknownRecord | undefined>
loadingOptions: Ref<LoadingOptions | undefined>
): void {
const defaultLoadingOptions = inject(LOADING_OPTIONS_KEY, {});
const realLoadingOptions = computed(() => ({
Expand All @@ -42,5 +41,5 @@ export function useLoading(

export const loadingProps = {
loading: Boolean,
loadingOptions: Object
loadingOptions: Object as PropType<LoadingOptions>
};
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ export type EventTarget = EChartsType | ZRenderType;
type SetOptionType = EChartsType["setOption"];
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 =
| "click"
| "dblclick"
Expand Down

1 comment on commit e869738

@vercel
Copy link

@vercel vercel bot commented on e869738 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.