fix: chart not rendering when theme auto

This commit is contained in:
Arman committed 2022-07-04 09:29:06 +02:00
1 parent c2be3c44cb
commit 877b6dd979
1 file changed
+17 -2
+17 -2
View File
@@ -16,6 +16,7 @@
import { app } from '$lib/stores/app';
import light from './echartLight.json';
import dark from './echartDark.json';
import { browser } from '$app/env';
echarts.use([
TitleComponent,
@@ -59,11 +60,25 @@
let timeoutId: unknown;
$: option && setOption();
$: if (myChart && $app.theme) {
$: if (myChart && selectedTheme) {
makeChart();
setOption();
}
$: selectedTheme = $app.theme;
$: if ($app.theme === 'auto') {
if (browser) {
const darkThemeMq = window.matchMedia('(prefers-color-scheme: dark)');
if (darkThemeMq.matches) {
selectedTheme = 'dark';
} else {
selectedTheme = 'light';
}
}
} else {
selectedTheme = $app.theme;
}
onMount(() => {
makeChart();
});
@@ -85,7 +100,7 @@
const makeChart = () => {
destroyChart();
myChart = echarts.init(document.getElementById(`echart-${title}`), $app.theme);
myChart = echarts.init(document.getElementById(`echart-${title}`), selectedTheme);
series.forEach((s: LineSeriesOption, i: number) => {
s.areaStyle = colorToGradient(myChart['_theme'].color[i]);
});