Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: fold fields optimize (#269)

islxyqwe01bbe7cea626
diff --git a/packages/graphic-walker/src/fields/encodeFields/singleEncodeEditor.tsx b/packages/graphic-walker/src/fields/encodeFields/singleEncodeEditor.tsx
index 177cb1d3..ce98fa8a 100644
--- a/packages/graphic-walker/src/fields/encodeFields/singleEncodeEditor.tsx
+++ b/packages/graphic-walker/src/fields/encodeFields/singleEncodeEditor.tsx
@@ -83,7 +83,7 @@ const SingleEncodeEditor: React.FC<SingleEncodeEditorProps> = (props) => {
onSelect={(keys) => {
vizStore.setVisualConfig('folds', keys);
}}
- className="flex-1 truncate"
+ className="flex-1"
>
<span className="flex-1 truncate" title={channelItem.name}>{channelItem.name}</span>
</SelectContext>
diff --git a/packages/graphic-walker/src/models/visSpecHistory.ts b/packages/graphic-walker/src/models/visSpecHistory.ts
index 3dc401dd..e61b70e9 100644
--- a/packages/graphic-walker/src/models/visSpecHistory.ts
+++ b/packages/graphic-walker/src/models/visSpecHistory.ts
@@ -50,6 +50,7 @@ export enum Methods {
createDateFeatureField,
changeSemanticType,
setFilterAggregator,
+ addFoldField,
}
type PropsMap = {
[Methods.setConfig]: KVTuple<IVisualConfigNew>;
@@ -72,6 +73,7 @@ type PropsMap = {
[Methods.createDateFeatureField]: [normalKeys, number, (typeof DATE_TIME_FEATURE_LEVELS)[number], string, string, string | undefined, number | undefined];
[Methods.changeSemanticType]: [normalKeys, number, ISemanticType];
[Methods.setFilterAggregator]: [number, IAggregator | ''];
+ [Methods.addFoldField]: [normalKeys, number, normalKeys, number, string, number | null];
};
// ensure propsMap has all keys of methods
type assertPropsMap = AssertSameKey<PropsMap, { [a in Methods]: any }>;
@@ -295,6 +297,30 @@ const actions: {
replace(f, index, (x) => ({ ...x, aggName: aggName || 'sum', enableAgg: aggName ? true : false, rule: null }))
);
},
+ [Methods.addFoldField]: (originalData, from, findex, to, tindex, newVarKey, limit) => {
+ let data = originalData;
+ const originalField = data.encodings[from][findex];
+ if (!data.config.folds) {
+ const validFoldBy = [...data.encodings.measures, ...data.encodings.details].filter((f) => f.analyticType === 'measure' && f.fid !== MEA_VAL_ID);
+ data = actions[Methods.setConfig](
+ data,
+ 'folds',
+ validFoldBy.map((x) => x.fid)
+ );
+ }
+ if (originalField.fid === MEA_VAL_ID) {
+ const meaKeyIndexes = Object.keys(data.encodings)
+ .filter((x): x is normalKeys => x !== 'filters')
+ .map((k) => [k, data.encodings[k].findIndex((f) => f.fid === MEA_KEY_ID)] as const)
+ .filter(([k, i]) => i >= 0);
+ if (meaKeyIndexes.length === 1) {
+ // there is no Measure Name in Chart, add it in Details channel (which has no limit)
+ const [fromKey, fromIndex] = meaKeyIndexes[0];
+ data = actions[Methods.cloneField](data, fromKey, fromIndex, 'details', 0, `${newVarKey}_auto`, Infinity);
+ }
+ }
+ return actions[Methods.cloneField](data, from, findex, to, tindex, newVarKey, limit);
+ },
};
const diffChangedEncodings = (prev: IChart, next: IChart) => {
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index 2a7e4d26..3f9261d8 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -349,6 +349,19 @@ export class VizSpecStore {
} else if (destMeta) {
this.visList[this.visIndex] = performers.removeField(this.visList[this.visIndex], sourceKey, sourceIndex);
} else {
+ // add an encoding
+ if (oriF.fid === MEA_KEY_ID || oriF.fid === MEA_VAL_ID) {
+ this.visList[this.visIndex] = performers.addFoldField(
+ this.visList[this.visIndex],
+ sourceKey,
+ sourceIndex,
+ destinationKey,
+ destinationIndex,
+ uniqueId(),
+ limit
+ );
+ return;
+ }
this.visList[this.visIndex] = performers.cloneField(
this.visList[this.visIndex],
sourceKey,