Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: new date drill level

islxyqwe882e93bf3246
diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.ts
index d6984194..6c235ba9 100644
--- a/packages/graphic-walker/src/interfaces.ts
+++ b/packages/graphic-walker/src/interfaces.ts
@@ -3,6 +3,7 @@ import {Config as VlConfig} from 'vega-lite';
import type { FeatureCollection } from 'geojson';
import type { feature } from 'topojson-client';
import type {IViewQuery} from "./lib/viewQuery";
+import { DATE_TIME_DRILL_LEVELS } from './constants';
export type DeepReadonly<T extends Record<keyof any, any>> = {
readonly [K in keyof T]: T[K] extends Record<keyof any, any> ? DeepReadonly<T[K]> : T[K];
@@ -115,6 +116,7 @@ export interface IField {
cmp?: (a: any, b: any) => number;
computed?: boolean;
expression?: IExpression;
+ timeUnit?: (typeof DATE_TIME_DRILL_LEVELS)[number];
basename?: string;
path?: string[],
}
diff --git a/packages/graphic-walker/src/lib/op/dateTimeDrill.ts b/packages/graphic-walker/src/lib/op/dateTimeDrill.ts
index 6eacf22e..0b1dcbf9 100644
--- a/packages/graphic-walker/src/lib/op/dateTimeDrill.ts
+++ b/packages/graphic-walker/src/lib/op/dateTimeDrill.ts
@@ -1,21 +1,30 @@
-import { DATE_TIME_DRILL_LEVELS } from "../../constants";
-import type { IExpParamter } from "../../interfaces";
-import type { IDataFrame } from "../execExp";
+import { DATE_TIME_DRILL_LEVELS } from '../../constants';
+import type { IExpParamter } from '../../interfaces';
+import type { IDataFrame } from '../execExp';
+const formatDate = (date: Date) => {
+ const Y = date.getFullYear();
+ const M = date.getMonth() + 1;
+ const D = date.getDate();
+ const H = date.getHours();
+ const m = date.getMinutes();
+ const s = date.getSeconds();
+ return `${Y}-${M}-${D} ${`${H}`.padStart(2, ' ')}:${`${m}`.padStart(2, '0')}:${`${s}`.padStart(2, '0')}`;
+};
function dateTimeDrill(resKey: string, params: IExpParamter[], data: IDataFrame): IDataFrame {
- const fieldKey = params.find(p => p.type === 'field')?.value;
- const drillLevel = params.find(p => p.type === 'value')?.value as typeof DATE_TIME_DRILL_LEVELS[number] | undefined;
+ const fieldKey = params.find((p) => p.type === 'field')?.value;
+ const drillLevel = params.find((p) => p.type === 'value')?.value as (typeof DATE_TIME_DRILL_LEVELS)[number] | undefined;
if (!fieldKey || !drillLevel) {
return data;
}
const fieldValues = data[fieldKey];
switch (drillLevel) {
case 'year': {
- const newValues = fieldValues.map(v => {
+ const newValues = fieldValues.map((v) => {
const date = new Date(v);
const Y = date.getFullYear();
- return Y;
+ return formatDate(new Date(Y, 0, 1));
});
return {
...data,
@@ -23,11 +32,11 @@ function dateTimeDrill(resKey: string, params: IExpParamter[], data: IDataFrame)
};
}
case 'quarter': {
- const newValues = fieldValues.map(v => {
+ const newValues = fieldValues.map((v) => {
const date = new Date(v);
const Y = date.getFullYear();
- const Q = Math.floor(date.getMonth() / 3) + 1;
- return `${Y} Q${Q}`;
+ const Q = Math.floor(date.getMonth() / 3);
+ return formatDate(new Date(Y, Q * 3, 1));
});
return {
...data,
@@ -35,11 +44,11 @@ function dateTimeDrill(resKey: string, params: IExpParamter[], data: IDataFrame)
};
}
case 'month': {
- const newValues = fieldValues.map(v => {
+ const newValues = fieldValues.map((v) => {
const date = new Date(v);
const Y = date.getFullYear();
- const M = date.getMonth() + 1;
- return `${Y}-${M}`;
+ const M = date.getMonth();
+ return formatDate(new Date(Y, M, 1));
});
return {
...data,
@@ -47,28 +56,14 @@ function dateTimeDrill(resKey: string, params: IExpParamter[], data: IDataFrame)
};
}
case 'week': {
- const newValues = fieldValues.map(v => {
+ const newValues = fieldValues.map((v) => {
const date = new Date(v);
const _Y = date.getFullYear();
const _firstDayOfYear = new Date(_Y, 0, 1);
- const _SundayOfFirstWeek = new Date(
- new Date(_firstDayOfYear).setDate(_firstDayOfYear.getDate() - _firstDayOfYear.getDay())
- );
+ const _SundayOfFirstWeek = new Date(new Date(_firstDayOfYear).setDate(_firstDayOfYear.getDate() - _firstDayOfYear.getDay()));
const Y = date.getTime() - _SundayOfFirstWeek.getTime() > 1_000 * 60 * 60 * 24 * 7 ? _Y : _SundayOfFirstWeek.getFullYear();
- const firstDayOfYear = new Date(Y, 0, 1);
- const SundayOfFirstWeek = new Date(
- new Date(firstDayOfYear).setDate(firstDayOfYear.getDate() - firstDayOfYear.getDay())
- );
- const W = Math.floor((date.getTime() - SundayOfFirstWeek.getTime()) / (7 * 24 * 60 * 60 * 1_000)) + 1;
- const SundayOfThisWeek = new Date(
- new Date(date).setDate(date.getDate() - date.getDay())
- );
- const SaturdayOfThisWeek = new Date(
- SundayOfThisWeek.getTime() + 6 * 24 * 60 * 60 * 1_000
- );
- const beginDay = SundayOfThisWeek.toLocaleDateString();
- const endDay = SaturdayOfThisWeek.toLocaleDateString();
- return `${Y} W${W < 10 ? ' ' : ''}${W} (${beginDay} ~ ${endDay})`;
+ const SundayOfThisWeek = new Date(new Date(date).setDate(date.getDate() - date.getDay()));
+ return formatDate(SundayOfThisWeek);
});
return {
...data,
@@ -76,12 +71,12 @@ function dateTimeDrill(resKey: string, params: IExpParamter[], data: IDataFrame)
};
}
case 'day': {
- const newValues = fieldValues.map(v => {
+ const newValues = fieldValues.map((v) => {
const date = new Date(v);
const Y = date.getFullYear();
- const M = date.getMonth() + 1;
+ const M = date.getMonth();
const D = date.getDate();
- return `${Y}-${M}-${D}`;
+ return formatDate(new Date(Y, M, D));
});
return {
...data,
@@ -89,13 +84,13 @@ function dateTimeDrill(resKey: string, params: IExpParamter[], data: IDataFrame)
};
}
case 'hour': {
- const newValues = fieldValues.map(v => {
+ const newValues = fieldValues.map((v) => {
const date = new Date(v);
const Y = date.getFullYear();
- const M = date.getMonth() + 1;
+ const M = date.getMonth();
const D = date.getDate();
const H = date.getHours();
- return `${Y}-${M}-${D} ${H < 10 ? ' ' : ''}${H} o'clock`;
+ return formatDate(new Date(Y, M, D, H));
});
return {
...data,
@@ -103,14 +98,14 @@ function dateTimeDrill(resKey: string, params: IExpParamter[], data: IDataFrame)
};
}
case 'minute': {
- const newValues = fieldValues.map(v => {
+ const newValues = fieldValues.map((v) => {
const date = new Date(v);
const Y = date.getFullYear();
- const M = date.getMonth() + 1;
+ const M = date.getMonth();
const D = date.getDate();
const H = date.getHours();
const m = date.getMinutes();
- return `${Y}-${M}-${D} ${H < 10 ? ' ' : ''}${H}:${m < 10 ? '0' : ''}${m}`;
+ return formatDate(new Date(Y, M, D, H, m));
});
return {
...data,
@@ -118,15 +113,15 @@ function dateTimeDrill(resKey: string, params: IExpParamter[], data: IDataFrame)
};
}
case 'second': {
- const newValues = fieldValues.map(v => {
+ const newValues = fieldValues.map((v) => {
const date = new Date(v);
const Y = date.getFullYear();
- const M = date.getMonth() + 1;
+ const M = date.getMonth();
const D = date.getDate();
const H = date.getHours();
const m = date.getMinutes();
const s = date.getSeconds();
- return `${Y}-${M}-${D} ${H < 10 ? ' ' : ''}${H}:${m < 10 ? '0' : ''}${m}:${s < 10 ? '0' : ''}${s}`;
+ return formatDate(new Date(Y, M, D, H, m, s));
});
return {
...data,
@@ -139,5 +134,4 @@ function dateTimeDrill(resKey: string, params: IExpParamter[], data: IDataFrame)
}
}
-
export default dateTimeDrill;
diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.ts
index 1609fc80..1decade3 100644
--- a/packages/graphic-walker/src/store/visualSpecStore.ts
+++ b/packages/graphic-walker/src/store/visualSpecStore.ts
@@ -593,10 +593,11 @@ export class VizSpecStore {
fid: newVarKey,
dragId: newVarKey,
name: typeof name === 'function' ? name(originField.name) : name,
- semanticType: "ordinal",
- analyticType: "dimension",
+ semanticType: "temporal",
+ analyticType: originField.analyticType,
aggName: 'sum',
computed: true,
+ timeUnit: drillLevel,
expression: {
op: "dateTimeDrill",
as: newVarKey,
diff --git a/packages/graphic-walker/src/vis/spec/encode.ts b/packages/graphic-walker/src/vis/spec/encode.ts
index 69af8803..4890a4a8 100644
--- a/packages/graphic-walker/src/vis/spec/encode.ts
+++ b/packages/graphic-walker/src/vis/spec/encode.ts
@@ -1,3 +1,4 @@
+import { DATE_TIME_DRILL_LEVELS } from '../../constants';
import { IViewField } from '../../interfaces';
import { NULL_FIELD } from './field';
export interface IEncodeProps {
@@ -26,6 +27,26 @@ export function availableChannels(geomType: string): Set<string> {
}
return new Set(['column', 'opacity', 'color', 'row', 'size', 'x', 'y', 'xOffset', 'yOffset', 'shape']);
}
+function encodeTimeunit(unit: (typeof DATE_TIME_DRILL_LEVELS)[number]) {
+ switch(unit) {
+ case 'quarter':
+ return 'yearquarter';
+ case 'month':
+ return 'yearmonth';
+ case 'week':
+ return 'yearweek';
+ case 'day':
+ return 'yearmonthdate';
+ case 'hour':
+ return 'yearmonthdatehours';
+ case 'minute':
+ return 'yearmonthdatehoursminutes';
+ case 'second':
+ return 'yearmonthdatehoursminutesseconds';
+ }
+ return unit;
+}
+
export function channelEncode(props: IEncodeProps) {
const avcs = availableChannels(props.geomType);
const encoding: { [key: string]: any } = {};
@@ -46,6 +67,9 @@ export function channelEncode(props: IEncodeProps) {
if (props[c].analyticType === 'measure') {
encoding[c].type = 'quantitative';
}
+ if (props[c].semanticType === 'temporal' && props[c].timeUnit) {
+ encoding[c].timeUnit = encodeTimeunit(props[c].timeUnit)
+ }
}
});
// FIXME: temporal fix, only for x and y relative order
diff --git a/packages/graphic-walker/src/vis/spec/tooltip.ts b/packages/graphic-walker/src/vis/spec/tooltip.ts
index b6237664..ed30b3fa 100644
--- a/packages/graphic-walker/src/vis/spec/tooltip.ts
+++ b/packages/graphic-walker/src/vis/spec/tooltip.ts
@@ -1,17 +1,27 @@
-import { IViewField } from "../../interfaces";
-import { getMeaAggKey } from "../../utils";
+import { IViewField } from '../../interfaces';
+import { getMeaAggKey } from '../../utils';
-export function addTooltipEncode (encoding: {[key: string]: any}, details: Readonly<IViewField[]> = [], defaultAggregated = false) {
- const encs = Object.keys(encoding).filter(ck => ck !== 'tooltip').map(ck => {
- return {
- field: encoding[ck].field,
- type: encoding[ck].type,
- title: encoding[ck].title
- }
- }).concat(details.map(f => ({
- field: defaultAggregated ? getMeaAggKey(f.fid, f.aggName) : f.fid,
- title: defaultAggregated && f.aggName ? `${f.aggName}(${f.name})` : f.name,
- type: f.semanticType
- })))
- encoding.tooltip = encs
-}
\ No newline at end of file
+export function addTooltipEncode(encoding: { [key: string]: any }, details: Readonly<IViewField[]> = [], defaultAggregated = false) {
+ const encs = Object.keys(encoding)
+ .filter((ck) => ck !== 'tooltip')
+ .map((ck) => {
+ return {
+ field: encoding[ck].field,
+ type: encoding[ck].type,
+ title: encoding[ck].title,
+ ...(encoding[ck].timeUnit
+ ? {
+ timeUnit: encoding[ck].timeUnit,
+ }
+ : {}),
+ };
+ })
+ .concat(
+ details.map((f) => ({
+ field: defaultAggregated ? getMeaAggKey(f.fid, f.aggName) : f.fid,
+ title: defaultAggregated && f.aggName ? `${f.aggName}(${f.name})` : f.name,
+ type: f.semanticType,
+ }))
+ );
+ encoding.tooltip = encs;
+}