Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: add binCount

jojocys0cbd444a6aa8
diff --git a/packages/graphic-walker/src/fields/datasetFields/meaFields.tsx b/packages/graphic-walker/src/fields/datasetFields/meaFields.tsx
index bf31b6b2..451e6190 100644
--- a/packages/graphic-walker/src/fields/datasetFields/meaFields.tsx
+++ b/packages/graphic-walker/src/fields/datasetFields/meaFields.tsx
@@ -20,18 +20,26 @@ const MeaFields: React.FC<Props> = (props) => {
value: "bin",
label: "Bin",
},
+ {
+ value: 'binCount',
+ label: 'Bin Count'
+ },
{
value: "log10",
label: "Log10",
},
+ {
+ value: "log2",
+ label: "Log2",
+ },
];
}, []);
const fieldActionHandler = useCallback((selectedValue: any, opIndex: number, meaIndex: number) => {
- if (selectedValue === "bin") {
- vizStore.createBinField("measures", meaIndex);
- } else if (selectedValue === "log10") {
- vizStore.createLogField("measures", meaIndex);
+ if (selectedValue === "bin" || selectedValue === 'binCount') {
+ vizStore.createBinField("measures", meaIndex, selectedValue);
+ } else if (selectedValue === "log10" || selectedValue === "log2") {
+ vizStore.createLogField("measures", meaIndex, selectedValue);
}
}, []);
return (
diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.ts
index 3381be28..70fa664e 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' | 'one';
+ op: 'bin' | 'log2' | 'log10' | 'one' | 'binCount';
params: IExpParamter[];
as: string;
}
diff --git a/packages/graphic-walker/src/lib/execExp.ts b/packages/graphic-walker/src/lib/execExp.ts
index c60a9a41..93c16757 100644
--- a/packages/graphic-walker/src/lib/execExp.ts
+++ b/packages/graphic-walker/src/lib/execExp.ts
@@ -36,6 +36,8 @@ export function execExpression (exp: IExpression, dataFrame: IDataFrame, columns
return log2(exp.as, params, subFrame);
case 'log10':
return log10(exp.as, params, subFrame);
+ case 'binCount':
+ return binCount(exp.as, params, subFrame);
default:
return subFrame;
}
@@ -64,6 +66,33 @@ function bin(resKey: string, params: IExpParamter[], data: IDataFrame, binSize:
}
}
+function binCount(resKey: string, params: IExpParamter[], data: IDataFrame, binSize: number | undefined = 10): IDataFrame {
+ const { value: fieldKey } = params[0];
+ const fieldValues = data[fieldKey] as number[];
+
+ const valueWithIndices: {val: number; index: number; orderIndex: number }[] = fieldValues.map((v, i) => ({
+ val: v,
+ index: i
+ })).sort((a, b) => a.val - b.val)
+ .map((item, i) => ({
+ val: item.val,
+ index: item.index,
+ orderIndex: i
+ }))
+
+ const groupSize = valueWithIndices.length / binSize;
+
+ const newValues = valueWithIndices.map(item => {
+ let bIndex = Math.floor(item.orderIndex / groupSize);
+ if (bIndex === binSize) bIndex = binSize - 1;
+ return bIndex + 1
+ })
+ return {
+ ...data,
+ [resKey]: newValues,
+ }
+}
+
function log2(resKey: string, params: IExpParamter[], data: IDataFrame): IDataFrame {
const { value } = params[0];
const field = data[value];
diff --git a/packages/graphic-walker/src/store/commonStore.ts b/packages/graphic-walker/src/store/commonStore.ts
index 12045026..dc87459a 100644
--- a/packages/graphic-walker/src/store/commonStore.ts
+++ b/packages/graphic-walker/src/store/commonStore.ts
@@ -1,7 +1,6 @@
import { DataSet, Filters, IDataSet, IDataSetInfo, IDataSource, IMutField, IRow, ISegmentKey } from '../interfaces';
import { makeAutoObservable, observable, toJS } from 'mobx';
import { transData } from '../dataSource/utils';
-import { createCountField, extendCountField } from '../utils';
export class CommonStore {
public datasets: IDataSet[] = [];
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index d493f47a..26c66967 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -2,13 +2,14 @@ import { IReactionDisposer, makeAutoObservable, observable, reaction, toJS } fro
import produce from "immer";
import { v4 as uuidv4 } from "uuid";
import { Specification } from "visual-insights";
-import { DataSet, DraggableFieldState, IFilterRule, IViewField, IVisSpec, IVisualConfig } from "../interfaces";
+import { DataSet, DraggableFieldState, IExpression, IFilterRule, IViewField, IVisSpec, IVisualConfig } from "../interfaces";
import { CHANNEL_LIMIT, GEMO_TYPES, MetaFieldKeys } from "../config";
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";
+import { IViewQuery } from "../lib/viewQuery";
function getChannelSizeLimit(channel: string): number {
if (typeof CHANNEL_LIMIT[channel] === "undefined") return Infinity;
@@ -506,19 +507,19 @@ export class VizSpecStore {
encodings.rows = fieldsInCup as typeof encodings.rows; // assume this as writable
});
}
- public createBinField(stateKey: keyof DraggableFieldState, index: number) {
+ public createBinField(stateKey: keyof DraggableFieldState, index: number, binType: 'bin' | 'binCount') {
this.useMutable(({ encodings }) => {
const originField = encodings[stateKey][index];
const newVarKey = uuidv4();
const binField: IViewField = {
fid: newVarKey,
dragId: newVarKey,
- name: `bin(${originField.name})`,
+ name: `${binType}(${originField.name})`,
semanticType: "ordinal",
analyticType: "dimension",
computed: true,
expressoion: {
- op: 'bin',
+ op: binType,
as: newVarKey,
params: [
{
@@ -531,7 +532,7 @@ export class VizSpecStore {
encodings.dimensions.push(binField);
});
}
- public createLogField(stateKey: keyof DraggableFieldState, index: number) {
+ public createLogField(stateKey: keyof DraggableFieldState, index: number, scaleType: 'log10' | 'log2') {
if (stateKey === "filters") {
return;
}
@@ -542,13 +543,13 @@ export class VizSpecStore {
const logField: IViewField = {
fid: newVarKey,
dragId: newVarKey,
- name: `log10(${originField.name})`,
+ name: `${scaleType}(${originField.name})`,
semanticType: "quantitative",
analyticType: originField.analyticType,
aggName: 'sum',
computed: true,
expressoion: {
- op: 'log10',
+ op: scaleType,
as: newVarKey,
params: [
{
diff --git a/packages/graphic-walker/src/utils/index.ts b/packages/graphic-walker/src/utils/index.ts
index fe29abdc..3cb1fabc 100644
--- a/packages/graphic-walker/src/utils/index.ts
+++ b/packages/graphic-walker/src/utils/index.ts
@@ -236,29 +236,6 @@ export function applyFilters(dataSource: IRow[], filters: Filters): IRow[] {
});
}
-export function extendCountField(
- dataSource: IRow[],
- fields: IMutField[]
-): {
- dataSource: IRow[];
- fields: IMutField[];
-} {
- const nextData = dataSource.map((r) => ({
- ...r,
- [COUNT_FIELD_ID]: 1,
- }));
- const nextFields = fields.concat({
- fid: COUNT_FIELD_ID,
- name: i18next.t("constant.row_count"),
- analyticType: "measure",
- semanticType: "quantitative",
- });
- return {
- dataSource: nextData,
- fields: nextFields,
- };
-}
-
export function createCountField(): IViewField {
return {
// viewId: "",
diff --git a/packages/graphic-walker/src/vis/spec/aggregate.ts b/packages/graphic-walker/src/vis/spec/aggregate.ts
index 8c46142c..d9ac7222 100644
--- a/packages/graphic-walker/src/vis/spec/aggregate.ts
+++ b/packages/graphic-walker/src/vis/spec/aggregate.ts
@@ -5,12 +5,9 @@ export function channelAggregate(encoding: { [key: string]: any }, fields: IView
Object.values(encoding).forEach((c) => {
const targetField = fields.find((f) => f.fid === c.field && !('aggregate' in c));
if (targetField && targetField.fid === COUNT_FIELD_ID) {
- c.field = undefined;
- // c.aggregate = 'count';
c.title = 'Count';
} else if (targetField && targetField.analyticType === 'measure') {
c.title = `${targetField.aggName}(${targetField.name})`;
- // c.aggregate = targetField.aggName;
}
});
}
diff --git a/packages/graphic-walker/src/vis/spec/encode.ts b/packages/graphic-walker/src/vis/spec/encode.ts
index 268b3c84..5abc617c 100644
--- a/packages/graphic-walker/src/vis/spec/encode.ts
+++ b/packages/graphic-walker/src/vis/spec/encode.ts
@@ -44,6 +44,12 @@ export function channelEncode(props: IEncodeProps) {
}
});
// FIXME: temporal fix, only for x and y relative order
+ if (encoding.x) {
+ encoding.x.axis = { labelOverlap: true }
+ }
+ if (encoding && encoding.y) {
+ encoding.y.axis = { labelOverlap: true }
+ }
if (encoding.x && encoding.y) {
if ((props.x.sort && props.x.sort) || (props.y && props.y.sort)) {
if (props.x.sort !== 'none' && (props.y.sort === 'none' || !Boolean(props.y.sort))) {