Skip to content

Commit ac46184

Browse files
authored
fix: render Chart with transparent background in both modes (#562)
1 parent f957dbc commit ac46184

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@cloudflare/kumo": patch
3+
---
4+
5+
Fix `Chart` (and `SankeyChart`) rendering in dark mode. The chart canvas
6+
now stays transparent so the surrounding `bg-kumo-*` surface shows through
7+
symmetrically in both modes, and ECharts' built-in `"dark"` theme is
8+
applied when `isDarkMode` is true so the tooltip card, axes, splitLines,
9+
and legend text are themed correctly.

packages/kumo/src/components/chart/EChart.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,26 @@ const transformTooltip = (tooltipObj: SafeTooltipOption) => {
158158
};
159159
};
160160

161-
const prepareChartOptions = (options: KumoChartOption): EChartsOption => {
162-
if (!options.tooltip) return options;
161+
const prepareChartOptions = ({
162+
options,
163+
isDarkMode,
164+
}: {
165+
options: KumoChartOption;
166+
isDarkMode?: boolean;
167+
}): EChartsOption => {
168+
const withDefaults: EChartsOption = {
169+
backgroundColor: "transparent",
170+
color: isDarkMode ? CHART_DARK_COLORS : CHART_LIGHT_COLORS,
171+
...options,
172+
};
173+
174+
if (!withDefaults.tooltip) return withDefaults;
163175

164176
return {
165-
...options,
166-
tooltip: Array.isArray(options.tooltip)
167-
? options.tooltip.map(transformTooltip)
168-
: transformTooltip(options.tooltip),
177+
...withDefaults,
178+
tooltip: Array.isArray(withDefaults.tooltip)
179+
? withDefaults.tooltip.map(transformTooltip)
180+
: transformTooltip(withDefaults.tooltip as SafeTooltipOption),
169181
};
170182
};
171183

@@ -221,14 +233,7 @@ export const Chart = forwardRef<echarts.ECharts, ChartProps>(function Chart(
221233
useEffect(() => {
222234
if (!elRef.current) return;
223235

224-
const chart = echarts.init(
225-
elRef.current,
226-
isDarkMode
227-
? "dark"
228-
: {
229-
color: isDarkMode ? CHART_DARK_COLORS : CHART_LIGHT_COLORS,
230-
},
231-
);
236+
const chart = echarts.init(elRef.current, isDarkMode ? "dark" : undefined);
232237
chartRef.current = chart;
233238

234239
if (typeof ref === "function") ref(chart);
@@ -252,7 +257,7 @@ export const Chart = forwardRef<echarts.ECharts, ChartProps>(function Chart(
252257
const chart = chartRef.current;
253258
if (!chart) return;
254259

255-
chart.setOption(prepareChartOptions(options), {
260+
chart.setOption(prepareChartOptions({ options, isDarkMode }), {
256261
notMerge: false,
257262
lazyUpdate: true,
258263
...optionUpdateBehavior,

0 commit comments

Comments
 (0)