@@ -2,6 +2,34 @@ import React, { useEffect, useRef, useState } from 'react';
22// 核心修复:直接使用 auto 全自动注册,彻底解决漏引组件导致的致命闪退
33import Chart from 'chart.js/auto' ;
44
5+ const isDark = ( ) => typeof document !== 'undefined' && document . documentElement . classList . contains ( 'dark' ) ;
6+
7+ // canvas 无法读取 CSS 变量,按当前主题返回具体色值
8+ const getChartColors = ( ) => {
9+ if ( isDark ( ) ) {
10+ return {
11+ bar : 'rgba(139, 92, 246, 0.85)' ,
12+ barBorder : 'rgb(139, 92, 246)' ,
13+ tooltipBg : 'rgba(24, 24, 27, 0.96)' ,
14+ tooltipTitle : '#f4f4f5' ,
15+ tooltipBody : '#a1a1aa' ,
16+ tooltipBorder : 'rgba(63, 63, 70, 1)' ,
17+ grid : 'rgba(255, 255, 255, 0.05)' ,
18+ ticks : '#a1a1aa' ,
19+ } ;
20+ }
21+ return {
22+ bar : 'rgba(139, 92, 246, 0.85)' ,
23+ barBorder : 'rgb(139, 92, 246)' ,
24+ tooltipBg : 'rgba(255, 255, 255, 0.96)' ,
25+ tooltipTitle : '#18181b' ,
26+ tooltipBody : '#52525b' ,
27+ tooltipBorder : 'rgba(228, 228, 231, 1)' ,
28+ grid : 'rgba(0, 0, 0, 0.06)' ,
29+ ticks : '#52525b' ,
30+ } ;
31+ } ;
32+
533export default function AuthorActivityChart ( { data = [ ] } ) {
634 const canvasRef = useRef ( null ) ;
735 const chartInstance = useRef ( null ) ;
@@ -55,6 +83,8 @@ export default function AuthorActivityChart({ data = [] }) {
5583
5684 const ctx = canvasRef . current . getContext ( '2d' ) ;
5785
86+ const colors = getChartColors ( ) ;
87+
5888 chartInstance . current = new Chart ( ctx , {
5989 type : 'bar' ,
6090 data : {
@@ -63,8 +93,8 @@ export default function AuthorActivityChart({ data = [] }) {
6393 {
6494 label : '发布页面数' ,
6595 data : pagesData ,
66- backgroundColor : 'rgba(99, 102, 241, 0.85)' ,
67- borderColor : 'rgb(99, 102, 241)' ,
96+ backgroundColor : colors . bar ,
97+ borderColor : colors . barBorder ,
6898 borderWidth : 1 ,
6999 barPercentage : 0.9 ,
70100 categoryPercentage : 1.0 ,
@@ -77,10 +107,10 @@ export default function AuthorActivityChart({ data = [] }) {
77107 plugins : {
78108 legend : { display : false } ,
79109 tooltip : {
80- backgroundColor : 'rgba(23,23,23,0.96)' ,
81- titleColor : '#fff' ,
82- bodyColor : 'rgb(200,200,200)' ,
83- borderColor : 'rgba(255,255,255,0.1)' ,
110+ backgroundColor : colors . tooltipBg ,
111+ titleColor : colors . tooltipTitle ,
112+ bodyColor : colors . tooltipBody ,
113+ borderColor : colors . tooltipBorder ,
84114 borderWidth : 1 ,
85115 callbacks : {
86116 label : function ( context ) {
@@ -98,16 +128,38 @@ export default function AuthorActivityChart({ data = [] }) {
98128 scales : {
99129 x : {
100130 grid : { display : false } ,
101- ticks : { color : 'rgb(110, 118, 129)' , maxRotation : 45 }
131+ ticks : { color : colors . ticks , maxRotation : 45 }
102132 } ,
103133 y : {
104134 beginAtZero : true ,
105- grid : { color : 'rgba(255, 255, 255, 0.05)' } ,
106- ticks : { color : 'rgb(110, 118, 129)' , stepSize : 1 }
135+ grid : { color : colors . grid } ,
136+ ticks : { color : colors . ticks , stepSize : 1 }
107137 }
108138 }
109139 }
110140 } ) ;
141+
142+ // 亮暗主题切换时重设 canvas 配色(canvas 不支持 CSS 变量,需 JS 侧感知)
143+ const applyThemeColors = ( ) => {
144+ const chart = chartInstance . current ;
145+ if ( ! chart ) return ;
146+ const c = getChartColors ( ) ;
147+ chart . data . datasets [ 0 ] . backgroundColor = c . bar ;
148+ chart . data . datasets [ 0 ] . borderColor = c . barBorder ;
149+ chart . options . plugins . tooltip . backgroundColor = c . tooltipBg ;
150+ chart . options . plugins . tooltip . titleColor = c . tooltipTitle ;
151+ chart . options . plugins . tooltip . bodyColor = c . tooltipBody ;
152+ chart . options . plugins . tooltip . borderColor = c . tooltipBorder ;
153+ chart . options . scales . x . ticks . color = c . ticks ;
154+ chart . options . scales . y . ticks . color = c . ticks ;
155+ chart . options . scales . y . grid . color = c . grid ;
156+ chart . update ( 'none' ) ;
157+ } ;
158+
159+ const themeObserver = new MutationObserver ( applyThemeColors ) ;
160+ themeObserver . observe ( document . documentElement , { attributes : true , attributeFilter : [ 'class' ] } ) ;
161+
162+ return ( ) => themeObserver . disconnect ( ) ;
111163 } catch ( err ) {
112164 console . error ( "图表引擎渲染异常:" , err ) ;
113165 setChartError ( err . message ) ;
@@ -118,7 +170,7 @@ export default function AuthorActivityChart({ data = [] }) {
118170 return (
119171 < div className = "w-full h-full relative min-h-[260px]" >
120172 { chartError && (
121- < div className = "absolute inset-0 flex items-center justify-center text-sm text-red-400 bg-gray-900/50 rounded-lg" >
173+ < div className = "absolute inset-0 flex items-center justify-center text-sm text-red-400 bg-sunken rounded-lg" >
122174 图表渲染失败: { chartError }
123175 </ div >
124176 ) }
0 commit comments