Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: add theme selection (#449)

Elwynn Chen2e1492afd368
diff --git a/packages/graphic-walker/src/App.tsx b/packages/graphic-walker/src/App.tsx
index 1cd94d60..b614bf90 100644
--- a/packages/graphic-walker/src/App.tsx
+++ b/packages/graphic-walker/src/App.tsx
@@ -1,7 +1,8 @@
import React, { useEffect, useMemo, useRef, useCallback, useState } from 'react';
import { observer } from 'mobx-react-lite';
import { useTranslation } from 'react-i18next';
-import { ISegmentKey, IAppI18nProps, IVizProps, IErrorHandlerProps, IVizAppProps, ISpecProps, IComputationContextProps, IComputationProps } from './interfaces';
+import { ISegmentKey, IAppI18nProps, IVizProps, IErrorHandlerProps, IVizAppProps, ISpecProps, IComputationContextProps, IComputationProps, IThemeKey } from './interfaces';
+import { GWGlobalConfig } from './vis/theme';
import type { IReactVegaHandler } from './vis/react-vega';
import VisualSettings from './visualSettings';
import PosFields from './fields/posFields';
@@ -136,6 +137,11 @@ export const VizApp = observer(function VizApp(props: BaseVizProps) {
);
const { segmentKey, vizEmbededMenu } = vizStore;
+ const [currentTheme, setCurrentTheme] = useState<IThemeKey | GWGlobalConfig>(
+ (vizThemeConfig ?? themeConfig ?? themeKey) as IThemeKey | GWGlobalConfig
+ );
+ const appliedThemeKey = typeof currentTheme === 'string' ? currentTheme : themeKey;
+ const appliedThemeConfig = typeof currentTheme === 'object' ? currentTheme : themeConfig;
const wrappedComputation = useMemo(
() => (computation ? withErrorReport(withTimeout(computation, computationTimeout), (err) => reportError(parseErrorMessage(err), 501)) : async () => []),
@@ -149,7 +155,7 @@ export const VizApp = observer(function VizApp(props: BaseVizProps) {
<VizAppContext
ComputationContext={wrappedComputation}
themeContext={darkMode}
- vegaThemeContext={{ vizThemeConfig: props.vizThemeConfig ?? props.themeConfig ?? props.themeKey }}
+ vegaThemeContext={{ vizThemeConfig: currentTheme, setVizThemeConfig: setCurrentTheme }}
portalContainerContext={portal}
>
<div className={classNames(`App font-sans bg-background text-foreground m-0 p-0`, darkMode === 'dark' ? 'dark' : '')}>
@@ -206,14 +212,14 @@ export const VizApp = observer(function VizApp(props: BaseVizProps) {
extra={toolbar?.extra}
/>
<CodeExport />
- <ExplainData themeKey={themeKey} />
+ <ExplainData themeKey={appliedThemeKey} />
{vizStore.showDataBoard && <DataBoard />}
<VisualConfig />
<LogPanel />
<BinPanel />
<RenamePanel />
<ComputedFieldDialog />
- <Painter themeConfig={themeConfig} themeKey={themeKey} />
+ <Painter themeConfig={appliedThemeConfig} themeKey={appliedThemeKey} />
{vizStore.showGeoJSONConfigPanel && <GeoConfigPanel geoList={props.geoList} />}
<div className="sm:flex">
<SideResize
@@ -250,7 +256,7 @@ export const VizApp = observer(function VizApp(props: BaseVizProps) {
<ReactiveRenderer
csvRef={downloadCSVRef}
ref={rendererRef}
- vizThemeConfig={vizThemeConfig ?? themeConfig ?? themeKey}
+ vizThemeConfig={currentTheme}
computationFunction={wrappedComputation}
// @TODO remove channelScales
scales={props.scales ?? props.channelScales}
diff --git a/packages/graphic-walker/src/Renderer.tsx b/packages/graphic-walker/src/Renderer.tsx
index 91daf397..ea672c27 100644
--- a/packages/graphic-walker/src/Renderer.tsx
+++ b/packages/graphic-walker/src/Renderer.tsx
@@ -10,10 +10,12 @@ import {
IComputationContextProps,
IComputationProps,
IComputationFunction,
+ IThemeKey,
IFilterRule,
IFilterField,
IVisualLayout,
} from './interfaces';
+import { GWGlobalConfig } from './vis/theme';
import ReactiveRenderer from './renderer/index';
import { ComputationContext, VizStoreWrapper, useCompututaion, useVizStore, withErrorReport, withTimeout } from './store';
import { mergeLocaleRes, setLocaleLanguage } from './locales/i18n';
@@ -79,6 +81,9 @@ export const RendererApp = observer(function VizApp(props: BaseVizProps) {
}, [i18nLang, curLang]);
const vizStore = useVizStore();
+ const [currentTheme, setCurrentTheme] = useState<IThemeKey | GWGlobalConfig>(
+ (vizThemeConfig ?? themeConfig ?? themeKey) as IThemeKey | GWGlobalConfig
+ );
useEffect(() => {
if (geographicData) {
@@ -136,7 +141,7 @@ export const RendererApp = observer(function VizApp(props: BaseVizProps) {
<VizAppContext
ComputationContext={wrappedComputation}
themeContext={darkMode}
- vegaThemeContext={{ vizThemeConfig: vizThemeConfig ?? themeConfig ?? themeKey }}
+ vegaThemeContext={{ vizThemeConfig: currentTheme, setVizThemeConfig: setCurrentTheme }}
portalContainerContext={portal}
>
<div className={`${darkMode === 'dark' ? 'dark' : ''} App font-sans bg-background text-foreground m-0 p-0`}>
@@ -146,7 +151,7 @@ export const RendererApp = observer(function VizApp(props: BaseVizProps) {
<div className={props.containerClassName} style={props.containerStyle}>
{computation && (
<ReactiveRenderer
- vizThemeConfig={vizThemeConfig ?? themeConfig ?? themeKey}
+ vizThemeConfig={currentTheme}
computationFunction={wrappedComputation}
// @TODO remove channelScales
scales={props.scales ?? props.channelScales}
diff --git a/packages/graphic-walker/src/Table.tsx b/packages/graphic-walker/src/Table.tsx
index 5d165abf..3271f94f 100644
--- a/packages/graphic-walker/src/Table.tsx
+++ b/packages/graphic-walker/src/Table.tsx
@@ -2,7 +2,8 @@ import { observer } from 'mobx-react-lite';
import React, { useEffect, useCallback, useMemo, useState } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { useTranslation } from 'react-i18next';
-import { IAppI18nProps, IErrorHandlerProps, IComputationContextProps, ITableProps, ITableSpecProps, IComputationProps, IMutField } from './interfaces';
+import { IAppI18nProps, IErrorHandlerProps, IComputationContextProps, ITableProps, ITableSpecProps, IComputationProps, IMutField, IThemeKey } from './interfaces';
+import { GWGlobalConfig } from './vis/theme';
import { mergeLocaleRes, setLocaleLanguage } from './locales/i18n';
import { useVizStore, withErrorReport, withTimeout, ComputationContext, VizStoreWrapper } from './store';
import { parseErrorMessage } from './utils';
@@ -52,6 +53,9 @@ export const TableApp = observer(function VizApp(props: BaseTableProps) {
}, [i18nLang, curLang]);
const vizStore = useVizStore();
+ const [currentTheme, setCurrentTheme] = useState<IThemeKey | GWGlobalConfig>(
+ (vizThemeConfig ?? themeConfig ?? themeKey) as IThemeKey | GWGlobalConfig
+ );
const reportError = useCallback(
(msg: string, code?: number) => {
@@ -79,7 +83,7 @@ export const TableApp = observer(function VizApp(props: BaseTableProps) {
<VizAppContext
ComputationContext={wrappedComputation}
themeContext={darkMode}
- vegaThemeContext={{ vizThemeConfig: vizThemeConfig ?? themeConfig ?? themeKey }}
+ vegaThemeContext={{ vizThemeConfig: currentTheme, setVizThemeConfig: setCurrentTheme }}
portalContainerContext={portal}
>
<div className={`${darkMode === 'dark' ? 'dark' : ''} App font-sans bg-background text-foreground h-full m-0 p-0`}>
diff --git a/packages/graphic-walker/src/components/visualConfig/index.tsx b/packages/graphic-walker/src/components/visualConfig/index.tsx
index 4c3c19e4..9b9636c7 100644
--- a/packages/graphic-walker/src/components/visualConfig/index.tsx
+++ b/packages/graphic-walker/src/components/visualConfig/index.tsx
@@ -1,11 +1,11 @@
-import React, { useEffect, useState, useMemo, useCallback } from 'react';
+import React, { useEffect, useState, useMemo, useCallback, useContext } from 'react';
import { observer } from 'mobx-react-lite';
import { runInAction, set } from 'mobx';
import { useTranslation } from 'react-i18next';
import { useVizStore } from '../../store';
import { GLOBAL_CONFIG } from '../../config';
-import { IConfigScale, IVisualConfig, IVisualLayout } from '../../interfaces';
+import { IConfigScale, IVisualConfig, IVisualLayout, IThemeKey } from '../../interfaces';
import Toggle from '../toggle';
import { ColorSchemes, extractRGBA } from './colorScheme';
import { DomainScale, RangeScale } from './range-scale';
@@ -18,6 +18,7 @@ import { Button } from '../ui/button';
import { Dialog, DialogContent, DialogFooter, DialogNormalContent } from '../ui/dialog';
import Combobox from '../dropdownSelect/combobox';
import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuTrigger } from '../ui/dropdown-menu';
+import { vegaThemeContext } from '../../store/theme';
import { ColorPickerComponent } from './color-picker';
function useDomainScale() {
@@ -151,6 +152,10 @@ const VisualConfigPanel: React.FC = () => {
const [displayColorPicker, setDisplayColorPicker] = useState(false);
const [displayBackgroundPicker, setDisplayBackgroundPicker] = useState(false);
const [colorPalette, setColorPalette] = useState('');
+ const { vizThemeConfig, setVizThemeConfig } = useContext(vegaThemeContext);
+ const [themeKey, setThemeKey] = useState<IThemeKey>(
+ typeof vizThemeConfig === 'string' ? vizThemeConfig : 'vega'
+ );
const [geoMapTileUrl, setGeoMapTileUrl] = useState<string | undefined>(undefined);
const [displayOffset, setDisplayOffset] = useState<number | undefined>(undefined);
const [displayOffsetEdited, setDisplayOffsetEdited] = useState(false);
@@ -190,6 +195,7 @@ const VisualConfigPanel: React.FC = () => {
normalizedNumberFormat: layout.format.normalizedNumberFormat,
});
setColorPalette(layout.colorPalette ?? '');
+ setThemeKey(typeof vizThemeConfig === 'string' ? vizThemeConfig : 'vega');
const enabledScales = Object.entries(layout.scale ?? {})
.filter(([_k, scale]) => Object.entries(scale).filter(([_k, v]) => !!v).length > 0)
.map(([k]) => k);
@@ -204,7 +210,7 @@ const VisualConfigPanel: React.FC = () => {
setGeoMapTileUrl(layout.geoMapTileUrl);
setDisplayOffset(config.timezoneDisplayOffset);
setDisplayOffsetEdited(false);
- }, [showVisualConfigPanel]);
+ }, [showVisualConfigPanel, vizThemeConfig]);
return (
<Dialog
@@ -250,6 +256,20 @@ const VisualConfigPanel: React.FC = () => {
setDisplayColorPicker={setDisplayBackgroundPicker}
/>
</div>
+ <div>
+ <label className="block text-xs font-medium leading-6">{t('config.theme')}</label>
+ <Combobox
+ className="w-48 h-fit"
+ popClassName="w-48"
+ selectedKey={themeKey}
+ onSelect={(v) => setThemeKey(v as IThemeKey)}
+ options={[
+ { value: 'vega', label: 'vega' },
+ { value: 'g2', label: 'g2' },
+ { value: 'streamlit', label: 'streamlit' },
+ ]}
+ />
+ </div>
<div>
<label className="block text-xs font-medium leading-6">{t('config.color_palette')}</label>
<Combobox
@@ -652,6 +672,7 @@ const VisualConfigPanel: React.FC = () => {
['geoMapTileUrl', geoMapTileUrl]
);
displayOffsetEdited && vizStore.setVisualConfig('timezoneDisplayOffset', displayOffset);
+ setVizThemeConfig?.(themeKey);
vizStore.setShowVisualConfigPanel(false);
});
}}
diff --git a/packages/graphic-walker/src/dataSource/index.tsx b/packages/graphic-walker/src/dataSource/index.tsx
index 918578bd..9ee7375f 100644
--- a/packages/graphic-walker/src/dataSource/index.tsx
+++ b/packages/graphic-walker/src/dataSource/index.tsx
@@ -239,6 +239,9 @@ export function DataSourceSegmentComponent(props: {
}, [selectedId, props.provider]);
const darkMode = useCurrentMediaTheme(props.appearance ?? props.dark);
+ const [currentTheme, setCurrentTheme] = useState<IThemeKey | GWGlobalConfig>(
+ (props.vizThemeConfig ?? props.themeConfig ?? props.themeKey) as IThemeKey | GWGlobalConfig
+ );
const [portal, setPortal] = useState<HTMLDivElement | null>(null);
return (
@@ -246,7 +249,7 @@ export function DataSourceSegmentComponent(props: {
<ShadowDom uiTheme={props.uiTheme ?? props.colorConfig}>
<DataSourceThemeContext
themeContext={darkMode}
- vegaThemeContext={{ vizThemeConfig: props.vizThemeConfig ?? props.themeConfig ?? props.themeKey}}
+ vegaThemeContext={{ vizThemeConfig: currentTheme, setVizThemeConfig: setCurrentTheme }}
portalContainerContext={portal}
>
<div className={`${darkMode === 'dark' ? 'dark' : ''} App`}>
diff --git a/packages/graphic-walker/src/locales/en-US.json b/packages/graphic-walker/src/locales/en-US.json
index fc78924e..a6f4f00f 100644
--- a/packages/graphic-walker/src/locales/en-US.json
+++ b/packages/graphic-walker/src/locales/en-US.json
@@ -3,6 +3,7 @@
"scheme": "Scheme",
"primary_color": "Primary Color",
"color_palette": "Color Palette",
+ "theme": "Theme",
"row": "Row",
"column": "Column",
"radius": "Radius",
diff --git a/packages/graphic-walker/src/locales/ja-JP.json b/packages/graphic-walker/src/locales/ja-JP.json
index b471c24a..d9aca057 100644
--- a/packages/graphic-walker/src/locales/ja-JP.json
+++ b/packages/graphic-walker/src/locales/ja-JP.json
@@ -3,6 +3,7 @@
"scheme": "スキーム",
"primary_color": "プライマリカラー",
"color_palette": "カラーパレット",
+ "theme": "テーマ",
"opacity": "不透明度",
"default_color_palette": "デフォルト",
"size": "サイズ",
diff --git a/packages/graphic-walker/src/locales/zh-CN.json b/packages/graphic-walker/src/locales/zh-CN.json
index 2dcb7b8c..8d0c143f 100644
--- a/packages/graphic-walker/src/locales/zh-CN.json
+++ b/packages/graphic-walker/src/locales/zh-CN.json
@@ -3,6 +3,7 @@
"scheme": "方案",
"primary_color": "主要颜色",
"color_palette": "调色板",
+ "theme": "主题",
"opacity": "不透明度",
"default_color_palette": "默认",
"size": "尺寸",
diff --git a/packages/graphic-walker/src/renderer/pureRenderer.tsx b/packages/graphic-walker/src/renderer/pureRenderer.tsx
index 790b92fe..af09e13e 100644
--- a/packages/graphic-walker/src/renderer/pureRenderer.tsx
+++ b/packages/graphic-walker/src/renderer/pureRenderer.tsx
@@ -162,6 +162,9 @@ const PureRenderer = forwardRef<IReactVegaHandler, IPureRendererProps & (LocalPr
const { coordSystem = 'generic' } = visualConfig;
const isSpatial = coordSystem === 'geographic';
const darkMode = useCurrentMediaTheme(appearance ?? dark);
+ const [currentTheme, setCurrentTheme] = useState<IThemeKey | GWGlobalConfig>(
+ (vizThemeConfig ?? themeConfig ?? themeKey) as IThemeKey | GWGlobalConfig
+ );
const [portal, setPortal] = useState<HTMLDivElement | null>(null);
return (
@@ -169,7 +172,7 @@ const PureRenderer = forwardRef<IReactVegaHandler, IPureRendererProps & (LocalPr
<VizAppContext
ComputationContext={computation}
themeContext={darkMode}
- vegaThemeContext={{ vizThemeConfig: vizThemeConfig ?? themeConfig ?? themeKey }}
+ vegaThemeContext={{ vizThemeConfig: currentTheme, setVizThemeConfig: setCurrentTheme }}
portalContainerContext={portal}
>
{waiting && <LoadingLayer />}
@@ -189,7 +192,7 @@ const PureRenderer = forwardRef<IReactVegaHandler, IPureRendererProps & (LocalPr
layout={visualLayout}
locale={locale ?? 'en-US'}
scales={scales ?? channelScales}
- vizThemeConfig={vizThemeConfig ?? themeConfig ?? themeKey}
+ vizThemeConfig={currentTheme}
disableCollapse={disableCollapse}
/>
)}
diff --git a/packages/graphic-walker/src/store/theme.ts b/packages/graphic-walker/src/store/theme.ts
index ccd0ea44..ea4f0d04 100644
--- a/packages/graphic-walker/src/store/theme.ts
+++ b/packages/graphic-walker/src/store/theme.ts
@@ -7,6 +7,7 @@ export const themeContext = createContext<'light' | 'dark'>('light');
export const vegaThemeContext = createContext<{
vizThemeConfig?: IThemeKey | GWGlobalConfig;
+ setVizThemeConfig?: (cfg: IThemeKey | GWGlobalConfig) => void;
}>({});
export const portalContainerContext = createContext<HTMLDivElement | null>(null);