@@ -60,6 +60,22 @@ export function BaseChart({ isDark, data, chartType, dataConfig, styleConfig }:
6060 } )
6161 } , [ ] )
6262
63+ const renderChart = ( ) => {
64+ if ( ! chartRef . current || ! domRef . current ) return ;
65+
66+ const containerSize = [ domRef . current . clientWidth || 0 , domRef . current . clientHeight || 0 ] ;
67+
68+ const option = generateChartOption ( {
69+ data,
70+ chartType,
71+ dataConfig,
72+ styleConfig,
73+ containerSize
74+ } ) ;
75+
76+ chartRef . current . setOption ( option ) ;
77+ chartRef . current . resize ( ) ;
78+ }
6379 // Initialize and update the chart.
6480 const [ screenFull , setScreenFull ] = useState ( false )
6581 useEffect ( ( ) => {
@@ -83,8 +99,8 @@ export function BaseChart({ isDark, data, chartType, dataConfig, styleConfig }:
8399 echartsLibRef . current . registerTheme ( themeName , themeConfig ) ;
84100 // init echarts
85101 chartRef . current = echartsLibRef . current . init ( domRef . current , themeName )
86- const option = generateChartOption ( { data , chartType , dataConfig , styleConfig } )
87- chartRef . current . setOption ( option , true )
102+
103+ renderChart ( ) ;
88104 } catch ( err ) {
89105 console . error ( 'Failed to initialize chart:' , err )
90106 }
@@ -99,19 +115,17 @@ export function BaseChart({ isDark, data, chartType, dataConfig, styleConfig }:
99115
100116 // resize
101117 useEffect ( ( ) => {
102- if ( ! chartRef . current ) return
118+ if ( ! chartRef . current || ! domRef . current ) return ;
119+
120+ const handleResize = ( ) => {
121+ renderChart ( ) ;
122+ } ;
103123
104124 const resizeObserver = new ResizeObserver ( ( ) => {
105- chartRef . current ?. resize ( )
125+ handleResize ( )
106126 } )
107127
108- if ( domRef . current ) {
109- resizeObserver . observe ( domRef . current )
110- }
111-
112- const handleResize = ( ) => {
113- chartRef . current ?. resize ( )
114- }
128+ resizeObserver . observe ( domRef . current )
115129 window . addEventListener ( 'resize' , handleResize )
116130
117131 return ( ) => {
@@ -155,6 +169,7 @@ export function generateChartOption(props: {
155169 chartType : ChartType ;
156170 dataConfig ?: ComponentConfig ;
157171 styleConfig : ComponentStyleConfig ;
172+ containerSize ?: number [ ] ;
158173} ) : any {
159174 const { chartType } = props ;
160175
@@ -163,7 +178,7 @@ export function generateChartOption(props: {
163178 return getPieChartOption ( props . data , chartType , props . styleConfig ) ;
164179 }
165180
166- return getCartesianChartOption ( props . data , chartType , props . styleConfig , props . dataConfig ) ;
181+ return getCartesianChartOption ( props . data , chartType , props . styleConfig , props . dataConfig , props . containerSize ) ;
167182}
168183
169184
@@ -211,14 +226,20 @@ const getCartesianChartOption = (
211226 data : ChartDataResponse ,
212227 chartType : ChartType ,
213228 styleConfig : ComponentStyleConfig ,
214- dataConfig ?: ComponentConfig
229+ dataConfig ?: ComponentConfig ,
230+ containerSize : number [ ] = [ ]
215231) => {
216232 const { dimensions, series } = data ;
217233 const isHorizontal = chartType . includes ( 'horizontal' ) ;
218234 const isStacked = chartType . includes ( 'stacked' ) ;
219235 const isLineOrArea = chartType . includes ( 'line' ) || chartType . includes ( 'area' ) ;
220236 const isArea = chartType . includes ( 'area' )
221237
238+ // aixs title tyle
239+ const [ containerWidth = 500 , containerHeight = 500 ] = containerSize
240+ const axisWidth = containerWidth - styleConfig . xAxisTitle . length * styleConfig . xAxisFontSize - 50 ;
241+ const axisHeight = containerHeight - styleConfig . yAxisTitle . length * styleConfig . yAxisFontSize - 50 ;
242+
222243 // Tooltip
223244 const tooltipFormatter = ( params : any [ ] ) => {
224245 const originName = params [ 0 ] ?. name || '' ;
@@ -263,8 +284,11 @@ const getCartesianChartOption = (
263284 // ...axisLabelStyle,
264285 } ,
265286 name : styleConfig . xAxisTitle || '' ,
266- nameLocation : styleConfig . xAxisAlign === 'right' ? 'end' : styleConfig . xAxisAlign === 'left' ? 'start' : 'center' ,
267- nameTextStyle : xAxisTitleStyle ,
287+ nameLocation : 'center' ,
288+ nameTextStyle : {
289+ ...xAxisTitleStyle ,
290+ padding : [ 0 , 0 , 0 , styleConfig . xAxisAlign === 'right' ? axisWidth : styleConfig . xAxisAlign === 'left' ? - axisWidth : 0 ]
291+ } ,
268292 inverse : isHorizontal
269293 } ;
270294
@@ -278,9 +302,12 @@ const getCartesianChartOption = (
278302 } ,
279303 splitLine : { show : styleConfig . showGrid ?? true } ,
280304 name : styleConfig . yAxisTitle || '' ,
281- nameLocation : styleConfig . yAxisAlign === 'right' ? 'end' : styleConfig . yAxisAlign === 'left' ? 'start' : 'center' ,
305+ nameLocation : 'center' ,
282306 nameRotate : isHorizontal ? 0 : 90 ,
283- nameTextStyle : yAxisTitleStyle ,
307+ nameTextStyle : {
308+ ...yAxisTitleStyle ,
309+ padding : [ 0 , 0 , 0 , styleConfig . yAxisAlign === 'right' ? axisHeight : styleConfig . yAxisAlign === 'left' ? - axisHeight : 0 ]
310+ } ,
284311 boundaryGap : [ 0 , '20%' ] ,
285312 } ;
286313
0 commit comments