Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: remove useless 'fields' key form encoding

jojocyscd4229bb9126
diff --git a/packages/graphic-walker/package.json b/packages/graphic-walker/package.json
index a0779ce2..bf102bd4 100644
--- a/packages/graphic-walker/package.json
+++ b/packages/graphic-walker/package.json
@@ -1,6 +1,6 @@
{
"name": "@kanaries/graphic-walker",
- "version": "0.2.18",
+ "version": "0.2.19",
"scripts": {
"dev:front_end": "vite --host",
"dev": "npm run dev:front_end",
@@ -43,6 +43,7 @@
"immer": "^9.0.15",
"mobx": "^6.3.3",
"mobx-react-lite": "^3.2.1",
+ "nanoid": "^4.0.2",
"postcss": "^8.3.7",
"postinstall-postinstall": "^2.1.0",
"re-resizable": "^6.9.8",
diff --git a/packages/graphic-walker/src/components/codeExport/index.tsx b/packages/graphic-walker/src/components/codeExport/index.tsx
index d4567a2a..ac75000d 100644
--- a/packages/graphic-walker/src/components/codeExport/index.tsx
+++ b/packages/graphic-walker/src/components/codeExport/index.tsx
@@ -17,7 +17,7 @@ const syntaxHighlight = (json: any) => {
.replace(/>/g, ">")
.replace(/\n/g, "<br>")
.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;")
- .replace(/\s/g, "&nbsp;&nbsp;");
+ .replace(/\s/g, "&nbsp;");
return json.replace(
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
function (match) {
@@ -84,8 +84,8 @@ const CodeExport: React.FC = observer((props) => {
}}
/>
{tabKey === "graphic-walker" && (
- <div className="text-sm px-6 max-h-56 overflow-auto">
- <div dangerouslySetInnerHTML={{ __html: syntaxHighlight(code) }} />
+ <div className="text-sm px-6 max-h-96 overflow-auto">
+ <code dangerouslySetInnerHTML={{ __html: syntaxHighlight(code) }} />
</div>
)}
<div className="mt-4 flex justify-start">
diff --git a/packages/graphic-walker/src/config.ts b/packages/graphic-walker/src/config.ts
index b6591aae..4ede6254 100644
--- a/packages/graphic-walker/src/config.ts
+++ b/packages/graphic-walker/src/config.ts
@@ -53,5 +53,4 @@ export const CHANNEL_LIMIT = {
export const MetaFieldKeys: Array<keyof DraggableFieldState> = [
'dimensions',
'measures',
- 'fields'
]
\ No newline at end of file
diff --git a/packages/graphic-walker/src/fields/fieldsContext.tsx b/packages/graphic-walker/src/fields/fieldsContext.tsx
index 68e8b33d..f9c30ec3 100644
--- a/packages/graphic-walker/src/fields/fieldsContext.tsx
+++ b/packages/graphic-walker/src/fields/fieldsContext.tsx
@@ -40,7 +40,6 @@ export const FieldsContextWrapper: React.FC = props => {
export default FieldsContextWrapper;
export const DRAGGABLE_STATE_KEYS: Readonly<IDraggableStateKey[]> = [
- { id: 'fields', mode: 0 },
{ id: 'columns', mode: 0 },
{ id: 'rows', mode: 0 },
{ id: 'color', mode: 1 },
diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.ts
index a3bd8c20..b65ea246 100644
--- a/packages/graphic-walker/src/interfaces.ts
+++ b/packages/graphic-walker/src/interfaces.ts
@@ -149,7 +149,6 @@ export interface IFilterField extends IViewField {
}
export interface DraggableFieldState {
- fields: IViewField[];
dimensions: IViewField[];
measures: IViewField[];
rows: IViewField[];
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index 46435f2c..05b10568 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -1,18 +1,22 @@
import { IReactionDisposer, makeAutoObservable, observable, reaction, toJS } from "mobx";
import produce from "immer";
-import { v4 as uuidv4 } from "uuid";
import { DataSet, DraggableFieldState, IFilterRule, IViewField, IVisSpec, IVisualConfig, Specification } from "../interfaces";
import { CHANNEL_LIMIT, GEMO_TYPES, MetaFieldKeys } from "../config";
import { VisSpecWithHistory } from "../models/visSpecHistory";
import { IStoInfo, dumpsGWPureSpec, parseGWContent, parseGWPureSpec, stringifyGWContent } from "../utils/save";
import { CommonStore } from "./commonStore";
import { createCountField } from "../utils";
+import { nanoid } from "nanoid";
function getChannelSizeLimit(channel: string): number {
if (typeof CHANNEL_LIMIT[channel] === "undefined") return Infinity;
return CHANNEL_LIMIT[channel];
}
+function uniqueId(): string {
+ return "gw_" + nanoid(4);
+}
+
function geomAdapter(geom: string) {
switch (geom) {
case "interval":
@@ -44,7 +48,6 @@ export function initEncoding(): DraggableFieldState {
return {
dimensions: [],
measures: [],
- fields: [],
rows: [],
columns: [],
color: [],
@@ -144,7 +147,7 @@ export class VizSpecStore {
this.visList.push(
new VisSpecWithHistory({
name: 'Chart 1',
- visId: uuidv4(),
+ visId: uniqueId(),
config: this.visualConfig,
encodings: this.draggableFieldState,
})
@@ -299,7 +302,7 @@ export class VizSpecStore {
this.visList.push(
new VisSpecWithHistory({
name,
- visId: uuidv4(),
+ visId: uniqueId(),
config: initVisualConfig(),
encodings: initEncoding(),
})
@@ -324,19 +327,10 @@ export class VizSpecStore {
public initMetaState(dataset: DataSet) {
const countField = createCountField();
this.useMutable(({ encodings }) => {
- encodings.fields = dataset.rawFields.map((f) => ({
- dragId: uuidv4(),
- fid: f.fid,
- name: f.name || f.fid,
- aggName: f.analyticType === "measure" ? "sum" : undefined,
- analyticType: f.analyticType,
- semanticType: f.semanticType,
- }))//.concat(countField);
- encodings.fields.push(countField);
encodings.dimensions = dataset.rawFields
.filter((f) => f.analyticType === "dimension")
.map((f) => ({
- dragId: uuidv4(),
+ dragId: uniqueId(),
fid: f.fid,
name: f.name || f.fid,
semanticType: f.semanticType,
@@ -345,7 +339,7 @@ export class VizSpecStore {
encodings.measures = dataset.rawFields
.filter((f) => f.analyticType === "measure")
.map((f) => ({
- dragId: uuidv4(),
+ dragId: uniqueId(),
fid: f.fid,
name: f.name || f.fid,
analyticType: f.analyticType,
@@ -436,7 +430,7 @@ export class VizSpecStore {
// use a different dragId
movingField = {
...toJS(encodings[sourceKey][sourceIndex]), // toJS will NOT shallow copy a object here
- dragId: uuidv4(),
+ dragId: uniqueId(),
};
} else {
[movingField] = encodings[sourceKey].splice(sourceIndex, 1);
@@ -480,7 +474,7 @@ export class VizSpecStore {
this.useMutable(({ encodings }) => {
encodings.filters.splice(index, 0, {
...toJS(data),
- dragId: uuidv4(),
+ dragId: uniqueId(),
rule: null,
});
this.editingFilterIdx = index;
@@ -508,7 +502,7 @@ export class VizSpecStore {
public createBinField(stateKey: keyof DraggableFieldState, index: number, binType: 'bin' | 'binCount') {
this.useMutable(({ encodings }) => {
const originField = encodings[stateKey][index];
- const newVarKey = uuidv4();
+ const newVarKey = uniqueId();
const binField: IViewField = {
fid: newVarKey,
dragId: newVarKey,
@@ -537,7 +531,7 @@ export class VizSpecStore {
this.useMutable(({ encodings }) => {
const originField = encodings[stateKey][index];
- const newVarKey = uuidv4();
+ const newVarKey = uniqueId();
const logField: IViewField = {
fid: newVarKey,
dragId: newVarKey,
@@ -635,7 +629,7 @@ export class VizSpecStore {
this.useMutable(({ encodings }) => {
const cloneField = { ...toJS(field) };
- cloneField.dragId = uuidv4();
+ cloneField.dragId = uniqueId();
encodings[destinationKey].push(cloneField);
});
}
@@ -643,7 +637,7 @@ export class VizSpecStore {
const tab = this.visList[this.visIndex];
if (tab) {
- const fields = tab.encodings.fields;
+ const fields = tab.encodings.dimensions.concat(tab.encodings.measures);
// thi
// const [xField, yField, ] = spec.position;
this.clearState();
diff --git a/yarn.lock b/yarn.lock
index bf981373..1972705b 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1646,6 +1646,11 @@ nanoid@^3.3.4:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab"
integrity sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==
+nanoid@^4.0.2:
+ version "4.0.2"
+ resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-4.0.2.tgz#140b3c5003959adbebf521c170f282c5e7f9fb9e"
+ integrity sha512-7ZtY5KTCNheRGfEFxnedV5zFiORN1+Y1N6zvPTnHQd8ENUvfaDBeuJDZb2bN/oXwXxu3qkTXDzy57W5vAmDTBw==
+
node-fetch@^2.6.7:
version "2.6.9"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.9.tgz#7c7f744b5cc6eb5fd404e0c7a9fec630a55657e6"