Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: extend count field

ObservedObserver28ef5d54832e
diff --git a/packages/graphic-walker/src/components/dataTable/index.tsx b/packages/graphic-walker/src/components/dataTable/index.tsx
index 28239c10..6a52a6d3 100644
--- a/packages/graphic-walker/src/components/dataTable/index.tsx
+++ b/packages/graphic-walker/src/components/dataTable/index.tsx
@@ -97,7 +97,7 @@ const DataTable: React.FC<DataTableProps> = (props) => {
/>
<table className="min-w-full divide-y">
<thead className="bg-gray-50 dark:bg-gray-900">
- <tr className="divide-x divide-gray-200 dark:divide-gray-600">
+ <tr className="divide-x divide-gray-200 dark:divide-gray-700">
{metas.map((field, fIndex) => (
<th key={field.fid} className={""}>
<div
@@ -152,9 +152,9 @@ const DataTable: React.FC<DataTableProps> = (props) => {
))}
</tr>
</thead>
- <tbody className="divide-y divide-gray-100 dark:divide-gray-600 bg-white dark:bg-zinc-900">
+ <tbody className="divide-y divide-gray-100 dark:divide-gray-700 bg-white dark:bg-zinc-900">
{data.slice(from, to + 1).map((row, index) => (
- <tr className={"divide-x divide-gray-200 dark:divide-gray-600 " + (index % 2 ? "bg-gray-50 dark:bg-gray-800" : "")} key={index}>
+ <tr className={"divide-x divide-gray-200 dark:divide-gray-700 " + (index % 2 ? "bg-gray-50 dark:bg-gray-900" : "")} key={index}>
{metas.map((field) => (
<td
key={field.fid + index}
diff --git a/packages/graphic-walker/src/dataSource/dataSelection/csvData.tsx b/packages/graphic-walker/src/dataSource/dataSelection/csvData.tsx
index 02b5f3d7..1d5cfdcb 100644
--- a/packages/graphic-walker/src/dataSource/dataSelection/csvData.tsx
+++ b/packages/graphic-walker/src/dataSource/dataSelection/csvData.tsx
@@ -74,7 +74,7 @@ const CSVData: React.FC<ICSVData> = (props) => {
d="M9 13h6m-3-3v6m-9 1V7a2 2 0 012-2h6l2 2h6a2 2 0 012 2v8a2 2 0 01-2 2H5a2 2 0 01-2-2z"
/>
</svg>
- <h3 className="mt-2 text-sm font-semibold text-gray-900">{t("choose_file")}</h3>
+ <h3 className="mt-2 text-sm font-semibold text-gray-900 dark:text-gray-50">{t("choose_file")}</h3>
<p className="mt-1 text-sm text-gray-500">{t("get_start_desc")}</p>
</div>
)}
@@ -93,7 +93,7 @@ const CSVData: React.FC<ICSVData> = (props) => {
classNames(
checked
? "bg-indigo-600 text-white hover:bg-indigo-500"
- : "ring-1 ring-inset ring-gray-300 bg-white text-gray-900 hover:bg-gray-50",
+ : "ring-1 ring-inset ring-gray-300 hover:bg-gray-50 dark:hover:bg-gray-800",
"flex cursor-pointer items-center justify-center rounded py-1 px-8 text-sm font-semibold uppercase sm:flex-1"
)
}
diff --git a/packages/graphic-walker/src/dataSource/utils.ts b/packages/graphic-walker/src/dataSource/utils.ts
index 333ddf95..cb7c5eb9 100644
--- a/packages/graphic-walker/src/dataSource/utils.ts
+++ b/packages/graphic-walker/src/dataSource/utils.ts
@@ -2,6 +2,7 @@ import { IRow, IMutField } from "../interfaces";
import { inferMeta } from "../lib/inferMeta";
import { guardDataKeys } from "../utils/dataPrep";
+
export function transData(dataSource: IRow[]): {
dataSource: IRow[];
fields: IMutField[];
@@ -12,7 +13,11 @@ export function transData(dataSource: IRow[]): {
fields: [],
};
}
- const keys = Object.keys(dataSource[0]);
+ const sampleRecord = dataSource[0];
+ // const rawKeys = Object.keys(sampleRecord);
+ // let flatColKeys: string[] = flatNestKeys(sampleRecord);
+
+ const keys = Object.keys(sampleRecord);
const metas = inferMeta({
dataSource,
fields: keys.map((k) => ({
@@ -21,7 +26,7 @@ export function transData(dataSource: IRow[]): {
analyticType: "?",
semanticType: "?",
})),
- });
+ })
const { safeData, safeMetas } = guardDataKeys(dataSource, metas);
const finalData: IRow[] = [];
for (let record of safeData) {
@@ -30,7 +35,7 @@ export function transData(dataSource: IRow[]): {
if (field.semanticType === "quantitative") {
newRecord[field.fid] = Number(record[field.fid]);
} else {
- newRecord[field.fid] = record[field.fid];
+ newRecord[field.fid] = record[field.fid];//getValueByKeyPath(record, field.fid);// record[field.fid];
}
}
finalData.push(newRecord);
diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.ts
index 7ef0ff83..3381be28 100644
--- a/packages/graphic-walker/src/interfaces.ts
+++ b/packages/graphic-walker/src/interfaces.ts
@@ -53,7 +53,7 @@ export type IExpParamter = {
export interface IExpression {
- op: 'bin' | 'log2' | 'log10';
+ op: 'bin' | 'log2' | 'log10' | 'one';
params: IExpParamter[];
as: string;
}
diff --git a/packages/graphic-walker/src/lib/execExp.ts b/packages/graphic-walker/src/lib/execExp.ts
index e99a8fdb..c60a9a41 100644
--- a/packages/graphic-walker/src/lib/execExp.ts
+++ b/packages/graphic-walker/src/lib/execExp.ts
@@ -28,6 +28,8 @@ export function execExpression (exp: IExpression, dataFrame: IDataFrame, columns
}
}
switch (op) {
+ case 'one':
+ return one(exp.as, params, subFrame);
case 'bin':
return bin(exp.as, params, subFrame);
case 'log2':
@@ -73,9 +75,20 @@ function log2(resKey: string, params: IExpParamter[], data: IDataFrame): IDataFr
}
function log10(resKey: string, params: IExpParamter[], data: IDataFrame): IDataFrame {
- const { value } = params[0];
- const field = data[value];
- const newField = field.map((v: number) => Math.log10(v));
+ const { value: fieldKey } = params[0];
+ const fieldValues = data[fieldKey];
+ const newField = fieldValues.map((v: number) => Math.log10(v));
+ return {
+ ...data,
+ [resKey]: newField,
+ }
+}
+
+function one(resKey: string, params: IExpParamter[], data: IDataFrame): IDataFrame {
+ // const { value: fieldKey } = params[0];
+ if (Object.keys(data).length === 0) return data;
+ const len = data[Object.keys(data)[0]].length;
+ const newField = new Array(len).fill(1);
return {
...data,
[resKey]: newField,
diff --git a/packages/graphic-walker/src/main.tsx b/packages/graphic-walker/src/main.tsx
index 62c9c4c3..c44bc13e 100644
--- a/packages/graphic-walker/src/main.tsx
+++ b/packages/graphic-walker/src/main.tsx
@@ -1,4 +1,4 @@
-import React from "react";
+import React, { useEffect } from "react";
import ReactDOM from "react-dom";
import { GraphicWalker } from "./index";
diff --git a/packages/graphic-walker/src/store/commonStore.ts b/packages/graphic-walker/src/store/commonStore.ts
index 2af178db..12045026 100644
--- a/packages/graphic-walker/src/store/commonStore.ts
+++ b/packages/graphic-walker/src/store/commonStore.ts
@@ -1,7 +1,7 @@
import { DataSet, Filters, IDataSet, IDataSetInfo, IDataSource, IMutField, IRow, ISegmentKey } from '../interfaces';
import { makeAutoObservable, observable, toJS } from 'mobx';
import { transData } from '../dataSource/utils';
-import { extendCountField } from '../utils';
+import { createCountField, extendCountField } from '../utils';
export class CommonStore {
public datasets: IDataSet[] = [];
@@ -31,12 +31,12 @@ export class CommonStore {
if (this.datasets.length > 0) {
const dataSourceId = this.datasets[datasetIndex].dsId;
const dataSource = this.dataSources.find(d => d.id === dataSourceId);
- const rawFields = toJS(this.datasets[datasetIndex].rawFields)
- const base = extendCountField((dataSource ? dataSource.data : []), rawFields)
+ const rawFields = toJS(this.datasets[datasetIndex].rawFields)//.concat(createCountField())
+ // const base = extendCountField((dataSource ? dataSource.data : []), rawFields)
return {
...this.datasets[datasetIndex],
- dataSource: base.dataSource,
- rawFields: base.fields
+ dataSource: dataSource?.data ?? [],
+ rawFields
}
}
return {
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index 9aca3bc2..ceda14a8 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -8,6 +8,7 @@ import { makeBinField, makeLogField } from "../utils/normalization";
import { VisSpecWithHistory } from "../models/visSpecHistory";
import { IStoInfo, dumpsGWPureSpec, parseGWContent, parseGWPureSpec, stringifyGWContent } from "../utils/save";
import { CommonStore } from "./commonStore";
+import { createCountField } from "../utils";
function getChannelSizeLimit(channel: string): number {
if (typeof CHANNEL_LIMIT[channel] === "undefined") return Infinity;
@@ -322,6 +323,7 @@ export class VizSpecStore {
});
}
public initMetaState(dataset: DataSet) {
+ const countField = createCountField();
this.useMutable(({ encodings }) => {
encodings.fields = dataset.rawFields.map((f) => ({
dragId: uuidv4(),
@@ -330,7 +332,8 @@ export class VizSpecStore {
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) => ({
@@ -350,17 +353,10 @@ export class VizSpecStore {
semanticType: f.semanticType,
aggName: "sum",
}));
+ encodings.measures.push(countField);
});
this.freezeHistory();
- // this.draggableFieldState.measures.push({
- // dragId: uuidv4(),
- // fid: COUNT_FIELD_ID,
- // name: '记录数',
- // analyticType: 'measure',
- // semanticType: 'quantitative',
- // aggName: 'count'
- // })
}
public clearState() {
this.useMutable(({ encodings }) => {
diff --git a/packages/graphic-walker/src/utils/dataPrep.ts b/packages/graphic-walker/src/utils/dataPrep.ts
index 15f5953d..ab6c6113 100644
--- a/packages/graphic-walker/src/utils/dataPrep.ts
+++ b/packages/graphic-walker/src/utils/dataPrep.ts
@@ -41,4 +41,28 @@ export function guardDataKeys (data: IRow[], metas: IMutField[]): {
safeData,
safeMetas
}
+}
+
+const SPLITOR = '__'
+export function flatNestKeys (object: any): string[] {
+ const keys = Object.keys(object);
+ let flatColKeys: string[] = [];
+ for (let key of keys) {
+ if (typeof object[key] === 'object') {
+ const subKeys = flatNestKeys(object[key]);
+ flatColKeys = flatColKeys.concat(subKeys.map(k => `${key}${SPLITOR}${k}`));
+ } else {
+ flatColKeys.push(key)
+ }
+ }
+ return flatColKeys;
+}
+
+export function getValueByKeyPath (object: any, keyPath: string): any {
+ const keys = keyPath.split(SPLITOR);
+ let value = object;
+ for (let key of keys) {
+ value = value[key];
+ }
+ return value;
}
\ No newline at end of file
diff --git a/packages/graphic-walker/src/utils/index.ts b/packages/graphic-walker/src/utils/index.ts
index face5a72..fe29abdc 100644
--- a/packages/graphic-walker/src/utils/index.ts
+++ b/packages/graphic-walker/src/utils/index.ts
@@ -1,6 +1,6 @@
import i18next from "i18next";
import { COUNT_FIELD_ID } from "../constants";
-import { IRow, Filters, IMutField } from "../interfaces";
+import { IRow, Filters, IMutField, IViewField } from "../interfaces";
interface NRReturns {
normalizedData: IRow[];
maxMeasures: IRow;
@@ -259,6 +259,24 @@ export function extendCountField(
};
}
+export function createCountField(): IViewField {
+ return {
+ // viewId: "",
+ dragId: COUNT_FIELD_ID,
+ fid: COUNT_FIELD_ID,
+ name: i18next.t("constant.row_count"),
+ analyticType: "measure",
+ semanticType: "quantitative",
+ aggName: 'sum',
+ computed: true,
+ expressoion: {
+ op: 'one',
+ params: [],
+ as: COUNT_FIELD_ID,
+ }
+ };
+}
+
export function getRange (nums: number[]): [number, number] {
let _min = Infinity;
let _max = -Infinity;
diff --git a/packages/graphic-walker/src/vis/theme.ts b/packages/graphic-walker/src/vis/theme.ts
index a3809ab5..2269e4cd 100644
--- a/packages/graphic-walker/src/vis/theme.ts
+++ b/packages/graphic-walker/src/vis/theme.ts
@@ -56,6 +56,18 @@ export const AntVTheme = {
],
diverging: ["#7b3294", "#c2a5cf", "#f7f7f7", "#a6dba0", "#008837"],
heatmap: ["#000000", "#7b3294", "#c2a5cf", "#f7f7f7", "#a6dba0", "#008837"],
+ // ordinal: [
+ // '#B8E1FF',
+ // // '#9AC5FF',
+ // // '#7DAAFF',
+ // // '#5B8FF9',
+ // // '#3D76DD',
+ // // '#085EC0',
+ // // '#0047A5',
+ // // '#00318A',
+ // '#001D70'
+ // ],
+ // ordinal: 'blues',
ramp: [
"#EBCCFF",
"#CCB0FF",