Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: add default config for configuing timezone offset (#301)

islxyqwe34863995c567
diff --git a/packages/graphic-walker/src/App.tsx b/packages/graphic-walker/src/App.tsx
index 96727bb5..3a7524a5 100644
--- a/packages/graphic-walker/src/App.tsx
+++ b/packages/graphic-walker/src/App.tsx
@@ -1,7 +1,7 @@
import React, { useEffect, useMemo, useRef, useCallback } from 'react';
import { observer } from 'mobx-react-lite';
import { useTranslation } from 'react-i18next';
-import { ISegmentKey, IAppI18nProps, IVizProps, IErrorHandlerProps, IVizAppProps, ISpecProps, IComputationContextProps, IRemoteComputationProps, IComputationProps } from './interfaces';
+import { ISegmentKey, IAppI18nProps, IVizProps, IErrorHandlerProps, IVizAppProps, ISpecProps, IComputationContextProps, IComputationProps } from './interfaces';
import type { IReactVegaHandler } from './vis/react-vega';
import VisualSettings from './visualSettings';
import PosFields from './fields/posFields';
@@ -265,7 +265,13 @@ export function VizAppWithContext(props: IVizAppProps & IComputationProps) {
return (
<div className={`${darkMode === 'dark' ? 'dark' : ''} App font-sans bg-white dark:bg-zinc-900 dark:text-white m-0 p-0`}>
- <VizStoreWrapper onMetaChange={onMetaChange} meta={safeMetas} keepAlive={props.keepAlive} storeRef={props.storeRef}>
+ <VizStoreWrapper
+ onMetaChange={onMetaChange}
+ meta={safeMetas}
+ keepAlive={props.keepAlive}
+ storeRef={props.storeRef}
+ defaultConfig={props.defaultConfig}
+ >
<FieldsContextWrapper>
<VizApp
darkMode={darkMode}
diff --git a/packages/graphic-walker/src/Table.tsx b/packages/graphic-walker/src/Table.tsx
index c3138eef..b93cc7c3 100644
--- a/packages/graphic-walker/src/Table.tsx
+++ b/packages/graphic-walker/src/Table.tsx
@@ -59,7 +59,7 @@ export const TableApp = observer(function VizApp(props: BaseTableProps) {
);
const metas = toJS(vizStore.meta);
-
+
return (
<ErrorContext value={{ reportError }}>
<ErrorBoundary fallback={<div>Something went wrong</div>} onError={props.onError}>
@@ -101,7 +101,13 @@ export function TableAppWithContext(props: ITableProps & IComputationProps) {
return (
<div className={`${darkMode === 'dark' ? 'dark' : ''} App font-sans bg-white dark:bg-zinc-900 dark:text-white m-0 p-0`}>
- <VizStoreWrapper onMetaChange={props.onMetaChange} meta={safeMetas} keepAlive={props.keepAlive} storeRef={props.storeRef}>
+ <VizStoreWrapper
+ onMetaChange={props.onMetaChange}
+ meta={safeMetas}
+ keepAlive={props.keepAlive}
+ storeRef={props.storeRef}
+ defaultConfig={props.defaultConfig}
+ >
<TableApp
darkMode={darkMode}
i18nLang={props.i18nLang}
diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.ts
index ff6a2152..1482af55 100644
--- a/packages/graphic-walker/src/interfaces.ts
+++ b/packages/graphic-walker/src/interfaces.ts
@@ -894,11 +894,17 @@ export interface IExperimentalFeatures {
computedField?: boolean;
}
+export interface IDefaultConfig {
+ config?: Partial<IVisualConfigNew>,
+ layout?: Partial<IVisualLayout>
+}
+
export interface IVizStoreProps {
storeRef?: React.MutableRefObject<VizSpecStore | null>;
keepAlive?: boolean | string;
rawFields: IMutField[];
onMetaChange?: (fid: string, meta: Partial<IMutField>) => void;
+ defaultConfig?: IDefaultConfig;
}
export interface ILocalComputationProps {
diff --git a/packages/graphic-walker/src/models/visSpecHistory.ts b/packages/graphic-walker/src/models/visSpecHistory.ts
index 7e6fbe83..405ad706 100644
--- a/packages/graphic-walker/src/models/visSpecHistory.ts
+++ b/packages/graphic-walker/src/models/visSpecHistory.ts
@@ -20,6 +20,7 @@ import {
IFilterField,
IField,
IPaintMapV2,
+ IDefaultConfig,
} from '../interfaces';
import type { FeatureCollection } from 'geojson';
import { createCountField, createVirtualFields, isNotEmpty } from '../utils';
@@ -551,21 +552,21 @@ export function decodeVisSpec(snapshot: Partial<IChartForExport>): PartialChart
};
});
}
-function emptyChart(visId: string, name: string): IChart {
+function emptyChart(visId: string, name: string, defaultConfig?: IDefaultConfig): IChart {
return {
- config: emptyVisualConfig,
+ config: defaultConfig?.config ? { ...emptyVisualConfig, ...defaultConfig.config } : emptyVisualConfig,
encodings: emptyEncodings,
- layout: emptyVisualLayout,
+ layout: defaultConfig?.layout ? { ...emptyVisualLayout, ...defaultConfig.layout } : emptyVisualLayout,
visId: visId,
name,
};
}
-export function newChart(fields: IMutField[], name: string, visId?: string): IChart {
- if (fields.length === 0) return emptyChart(visId || uniqueId(), name);
+export function newChart(fields: IMutField[], name: string, visId?: string, defaultConfig?: IDefaultConfig): IChart {
+ if (fields.length === 0) return emptyChart(visId || uniqueId(), name, defaultConfig);
const extraFields = [createCountField(), ...createVirtualFields()];
const extraDimensions = extraFields.filter((x) => x.analyticType === 'dimension');
const extraMeasures = extraFields.filter((x) => x.analyticType === 'measure');
- return mutPath(emptyChart(visId || uniqueId(), name), 'encodings', (e) => ({
+ return mutPath(emptyChart(visId || uniqueId(), name, defaultConfig), 'encodings', (e) => ({
...e,
dimensions: fields
.filter((f) => f.analyticType === 'dimension')
@@ -577,7 +578,7 @@ export function newChart(fields: IMutField[], name: string, visId?: string): ICh
basename: f.basename || f.name || f.fid,
semanticType: f.semanticType,
analyticType: f.analyticType,
- offset: f.offset
+ offset: f.offset,
})
)
.concat(extraDimensions),
@@ -592,7 +593,7 @@ export function newChart(fields: IMutField[], name: string, visId?: string): ICh
analyticType: f.analyticType,
semanticType: f.semanticType,
aggName: 'sum',
- offset: f.offset
+ offset: f.offset,
})
)
.concat(extraMeasures),
@@ -621,8 +622,8 @@ export function fillChart(chart: PartialChart): IChart {
export function fromSnapshot(snapshot: PartialChart): VisSpecWithHistory {
return create(fillChart(snapshot));
}
-export function fromFields(fields: IMutField[], name: string): VisSpecWithHistory {
- return create(newChart(fields, name));
+export function fromFields(fields: IMutField[], name: string, defaultConfig?: IDefaultConfig): VisSpecWithHistory {
+ return create(newChart(fields, name, undefined, defaultConfig));
}
type VisSpecHistoryInfoForExport = {
base: Partial<IChartForExport>;
diff --git a/packages/graphic-walker/src/store/index.tsx b/packages/graphic-walker/src/store/index.tsx
index 28f49512..aa9d034c 100644
--- a/packages/graphic-walker/src/store/index.tsx
+++ b/packages/graphic-walker/src/store/index.tsx
@@ -1,6 +1,6 @@
import React, { useContext, useMemo, useEffect, createContext, useRef } from 'react';
import { VizSpecStore } from './visualSpecStore';
-import { IComputationFunction, IMutField, IRow } from '../interfaces';
+import { IComputationFunction, IDefaultConfig, IMutField, IRow } from '../interfaces';
function createKeepAliveContext<T, U extends any[]>(create: (...args: U) => T) {
const dict: Record<string, T> = {};
@@ -20,6 +20,7 @@ const getVizStore = createKeepAliveContext(
opts?: {
empty?: boolean;
onMetaChange?: (fid: string, diffMeta: Partial<IMutField>) => void;
+ defaultConfig?: IDefaultConfig;
}
) => new VizSpecStore(meta, opts)
);
@@ -34,11 +35,12 @@ interface VizStoreWrapperProps {
children?: React.ReactNode;
meta: IMutField[];
onMetaChange?: (fid: string, meta: Partial<IMutField>) => void;
+ defaultConfig?: IDefaultConfig;
}
export const VizStoreWrapper = (props: VizStoreWrapperProps) => {
const storeKey = props.keepAlive ? `${props.keepAlive}` : '';
- const store = useMemo(() => getVizStore(storeKey, props.meta, { onMetaChange: props.onMetaChange }), [storeKey]);
+ const store = useMemo(() => getVizStore(storeKey, props.meta, { onMetaChange: props.onMetaChange, defaultConfig: props.defaultConfig }), [storeKey]);
const lastMeta = useRef(props.meta);
useEffect(() => {
if (lastMeta.current !== props.meta) {
@@ -54,6 +56,14 @@ export const VizStoreWrapper = (props: VizStoreWrapperProps) => {
}
}, [props.meta, store]);
+ const lastDefaultConfig = useRef(props.defaultConfig);
+ useEffect(() => {
+ if (lastDefaultConfig.current !== props.defaultConfig) {
+ store.setDefaultConfig(props.defaultConfig);
+ lastDefaultConfig.current = props.defaultConfig;
+ }
+ }, [props.defaultConfig, store]);
+
useEffect(() => {
if (props.storeRef) {
const ref = props.storeRef;
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index ab2d2deb..8ae3bf23 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -39,6 +39,7 @@ import {
IChartForExport,
IPaintMap,
IPaintMapV2,
+ IDefaultConfig,
} from '../interfaces';
import { GLOBAL_CONFIG } from '../config';
import { COUNT_FIELD_ID, DATE_TIME_DRILL_LEVELS, DATE_TIME_FEATURE_LEVELS, PAINT_FIELD_ID, MEA_KEY_ID, MEA_VAL_ID } from '../constants';
@@ -103,6 +104,7 @@ export class VizSpecStore {
showAskvizFeedbackIndex: number | undefined = 0;
lastSpec: string = '';
editingComputedFieldFid: string | undefined = undefined;
+ defaultConfig: IDefaultConfig | undefined;
private onMetaChange?: (fid: string, diffMeta: Partial<IMutField>) => void;
@@ -111,11 +113,13 @@ export class VizSpecStore {
options?: {
empty?: boolean;
onMetaChange?: (fid: string, diffMeta: Partial<IMutField>) => void;
+ defaultConfig?: IDefaultConfig
}
) {
this.meta = meta;
- this.visList = options?.empty ? [] : [fromFields(meta, 'Chart 1')];
+ this.visList = options?.empty ? [] : [fromFields(meta, 'Chart 1', options?.defaultConfig)];
this.createdVis = this.visList.length;
+ this.defaultConfig = options?.defaultConfig;
this.onMetaChange = options?.onMetaChange;
makeAutoObservable(this, {
visList: observable.shallow,
@@ -326,14 +330,18 @@ export class VizSpecStore {
this.onMetaChange = onMetaChange;
}
+ setDefaultConfig(defaultConfig?: IDefaultConfig) {
+ this.defaultConfig = defaultConfig;
+ }
+
resetVisualization(name = 'Chart 1') {
- this.visList = [fromFields(this.meta, name)];
+ this.visList = [fromFields(this.meta, name, this.defaultConfig)];
this.createdVis = 1;
}
addVisualization(defaultName?: string | ((index: number) => string)) {
const name = defaultName ? (typeof defaultName === 'function' ? defaultName(this.createdVis + 1) : defaultName) : 'Chart ' + (this.createdVis + 1);
- this.visList.push(fromFields(this.meta, name));
+ this.visList.push(fromFields(this.meta, name, this.defaultConfig));
this.createdVis += 1;
this.visIndex = this.visList.length - 1;
}
diff --git a/packages/graphic-walker/src/utils/workflow.ts b/packages/graphic-walker/src/utils/workflow.ts
index 9f10a9cf..eb22525d 100644
--- a/packages/graphic-walker/src/utils/workflow.ts
+++ b/packages/graphic-walker/src/utils/workflow.ts
@@ -364,14 +364,14 @@ export const processExpression = (exp: IExpression, allFields: IMutField[], conf
],
};
}
- if (isNotEmpty(config.timezoneDisplayOffset) && (exp.op === 'dateTimeDrill' || exp.op === 'dateTimeFeature')) {
+ if (exp.op === 'dateTimeDrill' || exp.op === 'dateTimeFeature') {
return {
...exp,
params: [
...exp.params,
{
type: 'displayOffset',
- value: config.timezoneDisplayOffset,
+ value: config.timezoneDisplayOffset ?? new Date().getTimezoneOffset(),
},
],
};