Commit 5eecec03126d131bd1210d4fcac3acfe3d5aeb40
Committed by
GitHub
1 parent
877311f9
perf: Update useApexCharts.ts (#139)
1、setOptions增加callback方法,返回chartInstance,以便于对图表进行再操作,例如调用updateOptions方法更新图表; 2、新增调用ApexCharts的updateOptions方法更新图表
Showing
1 changed file
with
31 additions
and
1 deletions
src/hooks/web/useApexCharts.ts
... | ... | @@ -7,7 +7,7 @@ import ApexCharts from 'apexcharts'; |
7 | 7 | export function useApexCharts(elRef: Ref<HTMLDivElement>) { |
8 | 8 | let chartInstance: Nullable<ApexCharts> = null; |
9 | 9 | |
10 | - function setOptions(options: any) { | |
10 | + function setOptions(options: any, callback) { | |
11 | 11 | nextTick(() => { |
12 | 12 | useTimeoutFn(() => { |
13 | 13 | const el = unref(elRef); |
... | ... | @@ -16,9 +16,38 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) { |
16 | 16 | chartInstance = new ApexCharts(el, options); |
17 | 17 | |
18 | 18 | chartInstance && chartInstance.render(); |
19 | + | |
20 | + // setOptions增加callback方法,返回chartInstance,以便于对图表进行再操作,例如调用updateOptions方法更新图表 | |
21 | + callback && callback(chartInstance); | |
22 | + | |
19 | 23 | }, 30); |
20 | 24 | }); |
21 | 25 | } |
26 | + | |
27 | + // 新增调用ApexCharts的updateOptions方法更新图表 | |
28 | + function updateOptions( | |
29 | + chartInstance: Nullable<ApexCharts>, | |
30 | + options, | |
31 | + redraw = false, | |
32 | + animate = true, | |
33 | + updateSyncedCharts = true, | |
34 | + overwriteInitialConfig = true, | |
35 | + callback) { | |
36 | + nextTick(() => { | |
37 | + useTimeoutFn(() => { | |
38 | + | |
39 | + chartInstance && chartInstance.updateOptions( | |
40 | + options, | |
41 | + redraw, | |
42 | + animate, | |
43 | + updateSyncedCharts, | |
44 | + overwriteInitialConfig); | |
45 | + | |
46 | + callback && callback(chartInstance); | |
47 | + | |
48 | + }, 30); | |
49 | + }); | |
50 | + } | |
22 | 51 | |
23 | 52 | tryOnUnmounted(() => { |
24 | 53 | if (!chartInstance) return; |
... | ... | @@ -28,5 +57,6 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) { |
28 | 57 | |
29 | 58 | return { |
30 | 59 | setOptions, |
60 | + updateOptions, | |
31 | 61 | }; |
32 | 62 | } | ... | ... |