@@ -4,10 +4,14 @@ import { unref, Ref, nextTick } from 'vue';
4
4
5
5
import ApexCharts from 'apexcharts' ;
6
6
7
+ interface CallBackFn {
8
+ ( instance : Nullable < ApexCharts > ) : void ;
9
+ }
10
+
7
11
export function useApexCharts ( elRef : Ref < HTMLDivElement > ) {
8
12
let chartInstance : Nullable < ApexCharts > = null ;
9
13
10
- function setOptions ( options : any , callback ) {
14
+ function setOptions ( options : any , callback ?: CallBackFn ) {
11
15
nextTick ( ( ) => {
12
16
useTimeoutFn ( ( ) => {
13
17
const el = unref ( elRef ) ;
@@ -16,37 +20,29 @@ export function useApexCharts(elRef: Ref<HTMLDivElement>) {
16
20
chartInstance = new ApexCharts ( el , options ) ;
17
21
18
22
chartInstance && chartInstance . render ( ) ;
19
-
23
+
20
24
// setOptions增加callback方法,返回chartInstance,以便于对图表进行再操作,例如调用updateOptions方法更新图表
21
25
callback && callback ( chartInstance ) ;
22
-
23
26
} , 30 ) ;
24
27
} ) ;
25
28
}
26
-
29
+
27
30
// 新增调用ApexCharts的updateOptions方法更新图表
28
31
function updateOptions (
29
- chartInstance : Nullable < ApexCharts > ,
30
- options ,
31
- redraw = false ,
32
- animate = true ,
33
- updateSyncedCharts = true ,
34
- overwriteInitialConfig = true ,
35
- callback ) {
32
+ chartInstance : Nullable < ApexCharts > ,
33
+ options : any ,
34
+ redraw = false ,
35
+ animate = true ,
36
+ updateSyncedCharts = true ,
37
+ callback : CallBackFn
38
+ ) {
36
39
nextTick ( ( ) => {
37
40
useTimeoutFn ( ( ) => {
41
+ chartInstance && chartInstance . updateOptions ( options , redraw , animate , updateSyncedCharts ) ;
38
42
39
- chartInstance && chartInstance . updateOptions (
40
- options ,
41
- redraw ,
42
- animate ,
43
- updateSyncedCharts ,
44
- overwriteInitialConfig ) ;
45
-
46
43
callback && callback ( chartInstance ) ;
47
-
48
44
} , 30 ) ;
49
- } ) ;
45
+ } ) ;
50
46
}
51
47
52
48
tryOnUnmounted ( ( ) => {
0 commit comments