Skip to content

Commit 379ca35

Browse files
prakhar10ebyhr
authored andcommitted
Fix chart legend font color issue after theme change
1 parent bfe6c02 commit 379ca35

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

webapp/src/components/dashboard.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,28 @@ export function Dashboard() {
117117
);
118118
}
119119

120+
function updateLegendColor() {
121+
const [color, setColor] = useState(getCSSVar('--semi-color-text-0'));
122+
123+
useEffect(() => {
124+
const handleThemeChange = () => {
125+
setColor(getCSSVar('--semi-color-text-0'));
126+
};
127+
128+
const observer = new MutationObserver(handleThemeChange);
129+
observer.observe(document.body, { attributes: true, attributeFilter: ["theme-mode"] });
130+
return () => observer.disconnect();
131+
132+
}, []);
133+
134+
return color;
135+
}
136+
120137
function LineChart(props: {
121138
data: Record<string, LineChartData[]>
122139
}) {
123140
const chartRef = useRef(null);
141+
const legendColor = updateLegendColor();
124142

125143
useEffect(() => {
126144
const chartInstance = echarts.init(chartRef.current);
@@ -150,7 +168,7 @@ function LineChart(props: {
150168
const option = {
151169
legend: {
152170
textStyle: {
153-
color: getCSSVar('--semi-color-text-0')
171+
color: legendColor
154172
}
155173
},
156174
xAxis: {
@@ -180,7 +198,7 @@ function LineChart(props: {
180198
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
181199
// @ts-ignore
182200
chartInstance.setOption(option);
183-
}, [props.data]);
201+
}, [props.data, legendColor]);
184202

185203
return (
186204
<div style={{ textAlign: "center" }}>
@@ -193,6 +211,7 @@ function DistributionChart(props: {
193211
data: DistributionChartData[]
194212
}) {
195213
const chartRef = useRef(null);
214+
const legendColor = updateLegendColor();
196215

197216
useEffect(() => {
198217
const chartInstance = echarts.init(chartRef.current);
@@ -202,7 +221,7 @@ function DistributionChart(props: {
202221
},
203222
legend: {
204223
textStyle: {
205-
color: getCSSVar('--semi-color-text-0')
224+
color: legendColor
206225
}
207226
},
208227
series: [
@@ -239,7 +258,7 @@ function DistributionChart(props: {
239258
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
240259
// @ts-ignore
241260
chartInstance.setOption(option);
242-
}, [props.data]);
261+
}, [props.data, legendColor]);
243262

244263
return (
245264
<div style={{ textAlign: "center" }}>

0 commit comments

Comments
 (0)