Skip to content

An open source alternative to Tableau. Embeddable visual analytic

fix: make eaiser to import old format data (#185)

islxyqwe81009e980793
diff --git a/packages/graphic-walker/src/index.tsx b/packages/graphic-walker/src/index.tsx
index 33673b05..e8809b47 100644
--- a/packages/graphic-walker/src/index.tsx
+++ b/packages/graphic-walker/src/index.tsx
@@ -65,7 +65,6 @@ export { embedGraphicWalker } from './vanilla';
export type { IGWProps };
export { ISegmentKey, ColorSchemes } from './interfaces';
export { resolveChart, convertChart } from './models/visSpecHistory';
-export { resolveVisSpec } from './utils/save';
export { getGlobalConfig } from './config';
export { getComputation } from './computation/clientComputation';
diff --git a/packages/graphic-walker/src/renderer/pureRenderer.tsx b/packages/graphic-walker/src/renderer/pureRenderer.tsx
index 9d7419c8..4fa80625 100644
--- a/packages/graphic-walker/src/renderer/pureRenderer.tsx
+++ b/packages/graphic-walker/src/renderer/pureRenderer.tsx
@@ -4,7 +4,7 @@ import { observer } from 'mobx-react-lite';
import { ShadowDom } from '../shadow-dom';
import LeafletRenderer, { LEAFLET_DEFAULT_HEIGHT } from '../components/leafletRenderer';
import { withAppRoot } from '../components/appRoot';
-import type { IDarkMode, IViewField, IRow, IThemeKey, DraggableFieldState, IVisualConfigNew, IComputationFunction, IVisualLayout, IChannelScales } from '../interfaces';
+import type { IDarkMode, IViewField, IRow, IThemeKey, DraggableFieldState, IVisualConfig, IVisualConfigNew, IComputationFunction, IVisualLayout, IChannelScales } from '../interfaces';
import type { IReactVegaHandler } from '../vis/react-vega';
import SpecRenderer from './specRenderer';
import { useRenderer } from './hooks';
@@ -19,8 +19,8 @@ type IPureRendererProps =
themeConfig?: GWGlobalConfig;
dark?: IDarkMode;
visualState: DraggableFieldState;
- visualConfig: IVisualConfigNew;
- visualLayout: IVisualLayout;
+ visualConfig: IVisualConfigNew | IVisualConfig;
+ visualLayout?: IVisualLayout;
locale?: string;
channelScales?: IChannelScales;
} & (
@@ -39,7 +39,7 @@ type IPureRendererProps =
* This is a pure component, which means it will not depend on any global state.
*/
const PureRenderer = forwardRef<IReactVegaHandler, IPureRendererProps>(function PureRenderer(props, ref) {
- const { name, themeKey, dark, visualState, visualConfig, visualLayout, locale, type, themeConfig, channelScales } = props;
+ const { name, themeKey, dark, visualState, visualConfig, visualLayout: layout, locale, type, themeConfig, channelScales } = props;
const computation = useMemo(() => {
if (props.type === 'remote') {
return props.computation;
@@ -47,6 +47,8 @@ const PureRenderer = forwardRef<IReactVegaHandler, IPureRendererProps>(function
return getComputation(props.rawData);
}, [type, type === 'remote' ? props.computation : props.rawData]);
+ const visualLayout = layout ?? (visualConfig as IVisualConfig);
+
const sort = getSort(visualState);
const limit = visualConfig.limit ?? -1;
const defaultAggregated = visualConfig?.defaultAggregated ?? false;
diff --git a/packages/graphic-walker/src/store/dataStore.ts b/packages/graphic-walker/src/store/dataStore.ts
index a44d8b8b..228baaab 100644
--- a/packages/graphic-walker/src/store/dataStore.ts
+++ b/packages/graphic-walker/src/store/dataStore.ts
@@ -2,7 +2,7 @@ import { computed, makeAutoObservable, toJS } from 'mobx';
import { DataSet, IAnalyticType, IDataSource, IMutField, IRow, ISemanticType } from '../interfaces';
import { VizSpecStore } from './visualSpecStore';
import { getComputation } from '../computation/clientComputation';
-import { IStoInfo, IStoInfoV2, IStoInfoV2SchemaUrl, forwardVisualConfigs, visSpecDecoder } from '../utils/save';
+import { IStoInfo, IStoInfoV2, IStoInfoV2SchemaUrl } from '../utils/save';
import { uniqueId } from '../models/utils';
const emptyMeta: IMutField[] = [];
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index 07755577..277010d0 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -36,7 +36,7 @@ import {
IGeoUrl,
ICreateField,
ISemanticType,
- IChartForExport
+ IChartForExport,
} from '../interfaces';
import { GLOBAL_CONFIG } from '../config';
import { DATE_TIME_DRILL_LEVELS, DATE_TIME_FEATURE_LEVELS } from '../constants';
@@ -170,9 +170,11 @@ export class VizSpecStore {
get layout() {
return {
...this.currentVis.layout,
- ...(this.localGeoJSON ? {
- geoJson: this.localGeoJSON
- }: {})
+ ...(this.localGeoJSON
+ ? {
+ geoJson: this.localGeoJSON,
+ }
+ : {}),
};
}
@@ -359,7 +361,12 @@ export class VizSpecStore {
const newVarKey = uniqueId();
const state = this.currentEncodings;
const existedRelatedBinField = state.dimensions.find(
- (f) => f.computed && f.expression && f.expression.op === binType && f.expression.params[0].value === state[stateKey][index].fid && f.expression.num === binNumber
+ (f) =>
+ f.computed &&
+ f.expression &&
+ f.expression.op === binType &&
+ f.expression.params[0].value === state[stateKey][index].fid &&
+ f.expression.num === binNumber
);
if (existedRelatedBinField) {
return existedRelatedBinField.fid;
@@ -409,11 +416,17 @@ export class VizSpecStore {
}
exportCode() {
- return this.visList.map(x => exportNow(x));
+ return this.visList.map((x) => exportNow(x));
}
- importCode(data: IChartForExport[]) {
- this.visList = data.map(x => importNow(x));
+ importCode(data: IChartForExport[] | IVisSpecForExport[]) {
+ this.visList = data.map((x: IChartForExport | IVisSpecForExport) => {
+ if ('layout' in x) {
+ return importNow(x);
+ } else {
+ return fromSnapshot(convertChart(visSpecDecoder(forwardVisualConfigs(x))));
+ }
+ });
this.visIndex = 0;
}
@@ -428,13 +441,8 @@ export class VizSpecStore {
this.visIndex = 0;
}
- importOld(data: IVisSpecForExport[]) {
- this.visList = visSpecDecoder(forwardVisualConfigs(data)).map(convertChart).map(fromSnapshot);
- this.visIndex = 0;
- }
-
appendFromOld(data: IVisSpecForExport) {
- const newChart = fromSnapshot(convertChart(visSpecDecoder(forwardVisualConfigs([data]))[0]));
+ const newChart = fromSnapshot(convertChart(visSpecDecoder(forwardVisualConfigs(data))));
this.visList.push(newChart);
this.visIndex = this.visList.length - 1;
}
diff --git a/packages/graphic-walker/src/utils/save.ts b/packages/graphic-walker/src/utils/save.ts
index de4513e8..69087075 100644
--- a/packages/graphic-walker/src/utils/save.ts
+++ b/packages/graphic-walker/src/utils/save.ts
@@ -1,4 +1,14 @@
-import { DraggableFieldState, IDataSet, IDataSource, IMutField, IVisSpec, IVisSpecForExport, IVisualConfig, IVisualConfigNew, IVisualLayout } from '../interfaces';
+import {
+ DraggableFieldState,
+ IDataSet,
+ IDataSource,
+ IMutField,
+ IVisSpec,
+ IVisSpecForExport,
+ IVisualConfig,
+ IVisualConfigNew,
+ IVisualLayout,
+} from '../interfaces';
import { GLOBAL_CONFIG } from '../config';
export function initEncoding(): DraggableFieldState {
@@ -15,7 +25,7 @@ export function initEncoding(): DraggableFieldState {
theta: [],
longitude: [],
latitude: [],
- geoId: [],
+ geoId: [],
details: [],
filters: [],
text: [],
@@ -23,7 +33,7 @@ export function initEncoding(): DraggableFieldState {
}
export function initVisualConfig(): IVisualConfig {
- const [ geom ] = GLOBAL_CONFIG.GEOM_TYPES.generic;
+ const [geom] = GLOBAL_CONFIG.GEOM_TYPES.generic;
return {
defaultAggregated: true,
geoms: [geom],
@@ -113,46 +123,38 @@ export const emptyEncodings: DraggableFieldState = {
text: [],
};
-export function visSpecDecoder(visList: IVisSpecForExport[]): IVisSpec[] {
- const updatedVisList = visList.map((visSpec) => {
- const updatedFilters = visSpec.encodings.filters.map((filter) => {
- if (filter.rule?.type === 'one of' && Array.isArray(filter.rule.value)) {
- return {
- ...filter,
- rule: {
- ...filter.rule,
- value: new Set(filter.rule.value),
- },
- };
- }
- return filter;
- });
- return {
- ...visSpec,
- encodings: {
- ...initEncoding(),
- ...visSpec.encodings,
- filters: updatedFilters,
- },
- } as IVisSpec;
+export function visSpecDecoder(visSpec: IVisSpecForExport): IVisSpec {
+ const updatedFilters = visSpec.encodings.filters.map((filter) => {
+ if (filter.rule?.type === 'one of' && Array.isArray(filter.rule.value)) {
+ return {
+ ...filter,
+ rule: {
+ ...filter.rule,
+ value: new Set(filter.rule.value),
+ },
+ };
+ }
+ return filter;
});
- return updatedVisList;
+ return {
+ ...visSpec,
+ encodings: {
+ ...initEncoding(),
+ ...visSpec.encodings,
+ filters: updatedFilters,
+ },
+ } as IVisSpec;
}
-export const forwardVisualConfigs = (backwards: IStoInfoOld['specList']): IVisSpecForExport[] => {
- return backwards.map((content) => ({
+export const forwardVisualConfigs = (content: IStoInfoOld['specList'][number]): IVisSpecForExport => {
+ return {
...content,
config: {
...initVisualConfig(),
...content.config,
},
- }));
+ };
};
-
-export const resolveVisSpec = (a: IVisSpecForExport[]): IVisSpec[] => {
- return visSpecDecoder(forwardVisualConfigs(a))
-}
-
export interface IStoInfoOld {
$schema: undefined;
datasets: IDataSet[];
@@ -163,11 +165,11 @@ export interface IStoInfoOld {
export const IStoInfoV2SchemaUrl = 'https://graphic-walker.kanaries.net/stoinfo_v2.json';
export interface IStoInfoV2 {
- $schema: typeof IStoInfoV2SchemaUrl,
- metaDict: Record<string, IMutField[]>,
- datasets: Required<IDataSource>[],
- specDict: Record<string, string[]>,
-};
+ $schema: typeof IStoInfoV2SchemaUrl;
+ metaDict: Record<string, IMutField[]>;
+ datasets: Required<IDataSource>[];
+ specDict: Record<string, string[]>;
+}
export type IStoInfo = IStoInfoOld | IStoInfoV2;