Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: add gog lint (#195)

Observed Observerb64a3cd38577
diff --git a/packages/graphic-walker/src/lib/gog.ts b/packages/graphic-walker/src/lib/gog.ts
new file mode 100644
index 00000000..f5649716
--- /dev/null
+++ b/packages/graphic-walker/src/lib/gog.ts
@@ -0,0 +1,69 @@
+// File name: gog represents the Grammar of Graphics (theory by Wilkinson)
+import { DraggableFieldState } from "../interfaces";
+
+type SpitialEncodings = Pick<DraggableFieldState, 'columns' | 'rows'> | Pick<DraggableFieldState, 'latitude' | 'longitude'>
+type DraggableFieldValues = DraggableFieldState[Exclude<keyof DraggableFieldState, 'filters'>];
+// LR = Lint Rules
+const LR = {
+ /**
+ * measures should appear after dimensions, for we do not apply cross operator on measures.
+ * @param fields
+ * @returns
+ */
+ measureAfterDimension(fields: DraggableFieldValues): DraggableFieldValues {
+ return fields.filter(f => f.analyticType === 'dimension').concat(fields.filter(f => f.analyticType === 'measure'));
+ },
+ // due to renderer, we set a limit of var can be applied to cross operator in GoG.
+ crossLimit(maxLimit: number) {
+ return function (fields: DraggableFieldValues): DraggableFieldValues {
+ let dimCounter = 0;
+ return fields.reduce<DraggableFieldValues>((acc, currField) => {
+ if (currField.analyticType === 'dimension' && dimCounter < maxLimit) {
+ acc.push(currField);
+ dimCounter++;
+ }
+ if (currField.analyticType === 'measure') {
+ acc.push(currField);
+ }
+ return acc;
+ }, [] as DraggableFieldValues);
+ }
+ },
+ forceDimension(fields: DraggableFieldValues): DraggableFieldValues {
+ return fields.map(f => ({
+ ...f,
+ analyticType: 'dimension'
+ }))
+ }
+}
+type Operator<T> = (value: T) => T;
+function applyOperations<T>(initialValue: T, operators: Operator<T>[]): T {
+ return operators.reduce((currentValue, operator) => operator(currentValue), initialValue);
+}
+
+/**
+ * Algebra lint is the lint fix encoding settings in algebra stage in the grammar of graphics.
+ * graphic-walker calculates the algebra on spitial channels (axises) (for now, rows, columns)
+ * This steps mainly decide how the sql is generated.
+ * @param encodings
+ * @returns
+ */
+export function algebraLint <T extends SpitialEncodings | DraggableFieldValues>(encodings: T): T {
+ if (Array.isArray(encodings)) {
+ return LR.measureAfterDimension(encodings) as T
+ }
+ if ('rows' in encodings && 'columns' in encodings && (encodings.rows.length > 0 || encodings.columns.length > 0)) {
+ return {
+ ...encodings,
+ rows: applyOperations(encodings.rows, [LR.measureAfterDimension, LR.crossLimit(2)]),
+ columns: applyOperations(encodings.columns, [LR.measureAfterDimension, LR.crossLimit(2)])
+ };
+ } else if ('latitude' in encodings && 'longitude' in encodings && (encodings.latitude.length > 0 || encodings.longitude.length > 0)) {
+ return {
+ ...encodings,
+ latitude: applyOperations(encodings.latitude, [LR.forceDimension, LR.crossLimit(1)]),
+ longitude: applyOperations(encodings.longitude, [LR.forceDimension, LR.crossLimit(1)])
+ };
+ }
+ return encodings;
+}
\ No newline at end of file
diff --git a/packages/graphic-walker/src/models/visSpecHistory.ts b/packages/graphic-walker/src/models/visSpecHistory.ts
index dbf03dbc..e1de46f6 100644
--- a/packages/graphic-walker/src/models/visSpecHistory.ts
+++ b/packages/graphic-walker/src/models/visSpecHistory.ts
@@ -24,6 +24,7 @@ import { AssertSameKey, KVTuple, insert, mutPath, remove, replace, uniqueId } fr
import { WithHistory, atWith, create, freeze, performWith, redoWith, undoWith } from './withHistory';
import { GLOBAL_CONFIG } from '../config';
import { DATE_TIME_DRILL_LEVELS, DATE_TIME_FEATURE_LEVELS } from '../constants';
+import { algebraLint } from '../lib/gog';
type normalKeys = keyof Omit<DraggableFieldState, 'filters'>;
@@ -84,11 +85,11 @@ const actions: {
[Methods.removeField]: (data, encoding, index) => mutPath(data, `encodings.${encoding}`, (fields) => remove(fields, index)) as IChart,
[Methods.reorderField]: (data, encoding, from, to) =>
mutPath(data, `encodings.${encoding}`, (fields) =>
- fields.map((x, i, a) => {
+ algebraLint(fields.map((x, i, a) => {
if (i === from) return a[to];
if (i === to) return a[from];
return x;
- })
+ }))
),
[Methods.moveField]: (data, from, findex, to, tindex, limit) => {
const oriField = data.encodings[from][findex];
@@ -98,7 +99,7 @@ const actions: {
: to === 'measures'
? mutPath(oriField, 'analyticType', () => 'measure')
: oriField;
- return mutPath(data, 'encodings', (e) => ({
+ return mutPath(data, 'encodings', (e) => algebraLint({
...e,
[from]: remove(data.encodings[from], findex),
[to]: insert(data.encodings[to], field, tindex).slice(0, limit ?? Infinity),
@@ -106,7 +107,7 @@ const actions: {
},
[Methods.cloneField]: (data, from, findex, to, tindex, newVarKey, limit) => {
const field = { ...data.encodings[from][findex], dragId: newVarKey };
- return mutPath(data, 'encodings', (e) => ({
+ return mutPath(data, 'encodings', (e) => algebraLint({
...e,
[to]: insert(data.encodings[to], field, tindex).slice(0, limit ?? Infinity),
}));
@@ -182,7 +183,7 @@ const actions: {
return data;
},
[Methods.transpose]: (data) =>
- mutPath(data, 'encodings', (e) => ({
+ mutPath(data, 'encodings', (e) => algebraLint({
...e,
columns: e.rows,
rows: e.columns,
diff --git a/packages/graphic-walker/src/store/commonStore.ts b/packages/graphic-walker/src/store/commonStore.ts
index 29c210e3..d76675ab 100644
--- a/packages/graphic-walker/src/store/commonStore.ts
+++ b/packages/graphic-walker/src/store/commonStore.ts
@@ -1,8 +1,7 @@
-import { IDataSetInfo, IMutField, IRow } from '../interfaces';
import { makeAutoObservable, observable } from 'mobx';
+import { IDataSetInfo, IMutField, IRow } from '../interfaces';
import { transData } from '../dataSource/utils';
import { DataStore } from './dataStore';
-import { INestNode } from '../components/pivotTable/inteface';
export class CommonStore {
public dataStore: DataStore = new DataStore();
@@ -13,6 +12,7 @@ export class CommonStore {
constructor () {
makeAutoObservable(this, {
tmpDataSource: observable.ref,
+ dataStore: false
});
}
public setShowDSPanel (show: boolean) {
diff --git a/packages/graphic-walker/src/store/dataStore.ts b/packages/graphic-walker/src/store/dataStore.ts
index 103db49d..5a5b8511 100644
--- a/packages/graphic-walker/src/store/dataStore.ts
+++ b/packages/graphic-walker/src/store/dataStore.ts
@@ -1,6 +1,6 @@
import { computed, makeAutoObservable, toJS } from 'mobx';
-import { DataSet, IAnalyticType, IDataSource, IMutField, IRow, ISegmentKey, ISemanticType } from '../interfaces';
import { VizSpecStore } from './visualSpecStore';
+import { DataSet, IAnalyticType, IDataSource, IMutField, IRow, ISemanticType } from '../interfaces';
import { getComputation } from '../computation/clientComputation';
import { IStoInfo, IStoInfoV2, IStoInfoV2SchemaUrl } from '../utils/save';
import { uniqueId } from '../models/utils';