@@ -19,7 +19,7 @@ import type {ECharts, EChartsOption, TooltipComponentOption} from 'echarts';
1919import Loading from '@cosui/cosmic/loading' ;
2020import { isAndroid } from '@cosui/cosmic/util' ;
2121import { colors , customColors } from './constant' ;
22- import { ChartProps , ChartData , Theme } from './interface' ;
22+ import { ChartProps , ChartData , Theme , Type , RegisterMapParams } from './interface' ;
2323import { deepMerge } from './utils' ;
2424
2525/**
@@ -41,6 +41,8 @@ export default class BaseChart extends Component<ChartProps & ChartData> {
4141 'cos-loading' : Loading
4242 } ;
4343
44+ // 是否已设置事件监听,在图表渲染完后设置
45+ hasEventListener : boolean = false ;
4446 // ECharts实例
4547 chartInstance : ECharts | null ;
4648 // 根元素样式表
@@ -51,34 +53,45 @@ export default class BaseChart extends Component<ChartProps & ChartData> {
5153 initData ( ) {
5254 return {
5355 option : { } ,
54- _loading : true
56+ _loading : true ,
57+ async : false
5558 } ;
5659 }
5760
5861 inited ( ) {
5962 this . chartInstance = null ;
6063 this . styleDeclaration = null ;
6164 this . themeObserverCleanup = null ;
65+ // 地图需要异步请求地图配置
66+ if ( this . data . get ( 'type' ) === Type . MAP ) {
67+ this . data . set ( 'async' , true ) ;
68+ }
6269 }
6370
6471 attached ( ) {
6572 this . styleDeclaration = getComputedStyle ( this . el ! ) ;
73+ // 异步无需自动更新配置,需要在外部手动调用 updateChart 方法更新配置
74+ if ( ! this . data . get ( 'async' ) ) {
75+ this . updateChart ( ) ;
76+ }
77+ }
78+ registerMap ( ...args : RegisterMapParams ) : void {
6679 const chartContainer = this . ref ( 'chartContainer' ) as unknown as HTMLDivElement ;
67-
6880 import ( 'echarts' ) . then ( echarts => {
69- const mainChart = echarts . init ( chartContainer ) ;
70-
71- this . initChart ( mainChart ) ;
81+ echarts . registerMap ( ...args ) ;
82+ const chartInstance = echarts . init ( chartContainer ) ;
83+ this . chartInstance = chartInstance ;
84+ this . addChartListener ( ) ;
85+ this . updateChartOption ( ) ;
7286 } ) . catch ( error => {
7387 throw new Error ( `ECharts initialization failed: ${ error . message } ` ) ;
7488 } ) . finally ( ( ) => {
7589 this . data . set ( '_loading' , false ) ;
7690 } ) ;
7791 }
78-
7992 updated ( ) {
80- if ( this . chartInstance && this . data . get ( 'option' ) ) {
81- this . updateChart ( ) ;
93+ if ( this . chartInstance && this . data . get ( 'option' ) && ! this . data . get ( 'async' ) ) {
94+ this . updateChartOption ( ) ;
8295 }
8396 }
8497
@@ -87,31 +100,44 @@ export default class BaseChart extends Component<ChartProps & ChartData> {
87100 this . themeObserverCleanup ?.( ) ;
88101 window . removeEventListener ( 'resize' , this . handleResize ) ;
89102 }
90-
91- // 初始化图表
92- initChart ( chartInstance : ECharts ) {
93- this . chartInstance = chartInstance ;
94- this . handleResize = this . handleResize . bind ( this ) ;
95-
96- // 监听窗口大小变化
97- window . addEventListener ( 'resize' , this . handleResize ) ;
98- // 监听图表渲染完成事件
99- this . chartInstance . on ( 'finished' , this . handleChartRendered . bind ( this ) ) ;
100- // 监听主题模式变化
101- this . themeObserverCleanup = this . onColorSchemeChange ( ( ) => {
102- this . chartInstance ?. setOption ( this . getDarkModeStyle ( ) ) ;
103+ addChartListener ( ) {
104+ if ( ! this . chartInstance ) {
105+ return ;
106+ }
107+ if ( ! this . hasEventListener ) {
108+ this . handleResize = this . handleResize . bind ( this ) ;
109+ // 监听窗口大小变化
110+ window . addEventListener ( 'resize' , this . handleResize ) ;
111+ // 监听图表渲染完成事件
112+ this . chartInstance ?. on ( 'finished' , this . handleChartRendered . bind ( this ) ) ;
113+ // 监听主题模式变化
114+ this . themeObserverCleanup = this . onColorSchemeChange ( ( ) => {
115+ this . chartInstance ?. setOption ( this . getDarkModeStyle ( ) ) ;
116+ } ) ;
117+ this . hasEventListener = true ;
118+ }
119+ }
120+ // 更新图表
121+ updateChart ( ) {
122+ const chartContainer = this . ref ( 'chartContainer' ) as unknown as HTMLDivElement ;
123+ import ( 'echarts' ) . then ( echarts => {
124+ const chartInstance = echarts . init ( chartContainer ) ;
125+ this . chartInstance = chartInstance ;
126+ this . addChartListener ( ) ;
127+ this . updateChartOption ( ) ;
128+ } ) . catch ( error => {
129+ throw new Error ( `ECharts initialization failed: ${ error . message } ` ) ;
130+ } ) . finally ( ( ) => {
131+ this . data . set ( '_loading' , false ) ;
103132 } ) ;
104- this . updateChart ( ) ;
105133 }
106134
107135 // 更新图表
108- updateChart ( ) {
136+ updateChartOption ( ) {
109137 if ( ! this . chartInstance ) {
110138 return ;
111139 }
112-
113140 const option = this . mergeOption ( ) ;
114-
115141 this . chartInstance . setOption ( option , true ) ;
116142 }
117143
0 commit comments