An open source alternative to Tableau. Embeddable visual analytic
TypeScript bidatadata-analysisdata-miningdata-visualizationedak6skanarieslow-codepivot-tablereacttableautableau-alternativetypescriptvegavega-litevisualization
feat: datetime feature
islxyqwe2cf4e4834f92
diff --git a/packages/graphic-walker/src/constants.ts b/packages/graphic-walker/src/constants.tsindex 78495908..4ca208c8 100644--- a/packages/graphic-walker/src/constants.ts+++ b/packages/graphic-walker/src/constants.ts@@ -2,3 +2,6 @@ export const COUNT_FIELD_ID = 'gw_count_fid';export const DATE_TIME_DRILL_LEVELS = ["year", "quarter", "month", "week", "day", "hour", "minute", "second"] as const;+export const DATE_TIME_FEATURE_LEVELS = [+ "year", "quarter", "month", "week", "weekday", "day", "hour", "minute", "second"+] as const;diff --git a/packages/graphic-walker/src/fields/datasetFields/utils.ts b/packages/graphic-walker/src/fields/datasetFields/utils.tsindex ab0c48a4..c4673406 100644--- a/packages/graphic-walker/src/fields/datasetFields/utils.ts+++ b/packages/graphic-walker/src/fields/datasetFields/utils.ts@@ -2,7 +2,7 @@ import { useMemo } from "react";import { useTranslation } from "react-i18next";import { useGlobalStore } from "../../store";import type { IActionMenuItem } from "../../components/actionMenu/list";-import { COUNT_FIELD_ID, DATE_TIME_DRILL_LEVELS } from "../../constants";+import { COUNT_FIELD_ID, DATE_TIME_DRILL_LEVELS, DATE_TIME_FEATURE_LEVELS } from "../../constants";import { useComputationFunc } from "../../renderer/hooks";import { getSample } from "../../computation/serverComputation";import { getTimeFormat } from "../../lib/inferMeta";@@ -95,10 +95,24 @@ export const useMenuActions = (channel: "dimensions" | "measures"): IActionMenuIconst originField = (isDateTimeDrilled ? vizStore.allFields.find(field => field.fid === f.expression?.params.find(p => p.type === 'field')?.value) : null) ?? f;const originChannel = originField.analyticType === 'dimension' ? 'dimensions' : 'measures';const originIndex = vizStore.allFields.findIndex(x => x.fid === originField.fid);- getSample(computation, originField.fid).then(getTimeFormat).then(format => vizStore.createDateTimeDrilledField(originChannel, originIndex, level, `${t(`drill.levels.${level}`)} (${originField.name || originField.fid})`, format));+ getSample(computation, originField.fid).then(getTimeFormat).then(format => vizStore.createDateTimeDrilledField(originChannel, originIndex, 'dateTimeDrill',level, `${t(`drill.levels.${level}`)} (${originField.name || originField.fid})`, format));},})),},+ (f.semanticType === 'temporal' || isDateTimeDrilled) && {+ label: t('drill.feature_name'),+ children: DATE_TIME_FEATURE_LEVELS.map(level => ({+ label: t(`drill.levels.${level}`),+ disabled: isDateTimeDrilled && f.expression.params.find(p => p.type === 'value')?.value === level,+ onPress() {+ const originField = (isDateTimeDrilled ? vizStore.allFields.find(field => field.fid === f.expression?.params.find(p => p.type === 'field')?.value) : null) ?? f;+ const originChannel = originField.analyticType === 'dimension' ? 'dimensions' : 'measures';+ const originIndex = vizStore.allFields.findIndex(x => x.fid === originField.fid);+ getSample(computation, originField.fid).then(getTimeFormat).then(format => vizStore.createDateTimeDrilledField(originChannel, originIndex, 'dateTimeFeature', level,`${t(`drill.levels.${level}`)} [${originField.name || originField.fid}]`, format));+ },+ })),+ },+]);});}, [channel, fields, vizStore, t, computation]);diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.tsindex b6befa8c..3d0530f0 100644--- a/packages/graphic-walker/src/interfaces.ts+++ b/packages/graphic-walker/src/interfaces.ts@@ -93,7 +93,7 @@ export type IExpParamter =};export interface IExpression {- op: 'bin' | 'log2' | 'log10' | 'one' | 'binCount' | 'dateTimeDrill' | 'log';+ op: 'bin' | 'log2' | 'log10' | 'one' | 'binCount' | 'dateTimeDrill' | 'dateTimeFeature' | 'log';params: IExpParamter[];as: string;num?: number;diff --git a/packages/graphic-walker/src/lib/execExp.ts b/packages/graphic-walker/src/lib/execExp.tsindex d3ed71b1..e4c850de 100644--- a/packages/graphic-walker/src/lib/execExp.ts+++ b/packages/graphic-walker/src/lib/execExp.ts@@ -1,5 +1,6 @@import { IExpParamter, IExpression, IField, IRow } from "../interfaces";import dateTimeDrill from "./op/dateTimeDrill";+import dateTimeFeature from "./op/dateTimeFeature";export interface IDataFrame {[key: string]: any[];@@ -43,6 +44,8 @@ export function execExpression (exp: IExpression, dataFrame: IDataFrame): IDataFreturn bin(exp.as, params, subFrame, num);case 'dateTimeDrill':return dateTimeDrill(exp.as, params, subFrame);+ case 'dateTimeFeature':+ return dateTimeFeature(exp.as, params, subFrame);default:return subFrame;}diff --git a/packages/graphic-walker/src/lib/op/dateTimeFeature.ts b/packages/graphic-walker/src/lib/op/dateTimeFeature.tsnew file mode 100644index 00000000..d33b308a--- /dev/null+++ b/packages/graphic-walker/src/lib/op/dateTimeFeature.ts@@ -0,0 +1,123 @@+import { DATE_TIME_FEATURE_LEVELS } from "../../constants";+import type { IExpParamter } from "../../interfaces";+import type { IDataFrame } from "../execExp";+++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_FEATURE_LEVELS[number] | undefined;+ if (!fieldKey || !drillLevel) {+ return data;+ }+ const fieldValues = data[fieldKey];+ switch (drillLevel) {+ case 'year': {+ const newValues = fieldValues.map(v => {+ const date = new Date(v);+ return date.getFullYear();+ });+ return {+ ...data,+ [resKey]: newValues,+ };+ }+ case 'quarter': {+ const newValues = fieldValues.map(v => {+ const date = new Date(v);+ const Q = Math.floor(date.getMonth() / 3) + 1;+ return Q;+ });+ return {+ ...data,+ [resKey]: newValues,+ };+ }+ case 'month': {+ const newValues = fieldValues.map(v => {+ const date = new Date(v);+ return date.getMonth() + 1;+ });+ return {+ ...data,+ [resKey]: newValues,+ };+ }+ case 'week': {+ 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 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;+ return W;+ });+ return {+ ...data,+ [resKey]: newValues,+ };+ }+ case 'weekday': {+ const newValues = fieldValues.map(v => {+ const date = new Date(v);+ return date.getDay();+ });+ return {+ ...data,+ [resKey]: newValues,+ };+ }+ case 'day': {+ const newValues = fieldValues.map(v => {+ const date = new Date(v);+ return date.getDate();+ });+ return {+ ...data,+ [resKey]: newValues,+ };+ }+ case 'hour': {+ const newValues = fieldValues.map(v => {+ const date = new Date(v);+ return date.getHours();+ });+ return {+ ...data,+ [resKey]: newValues,+ };+ }+ case 'minute': {+ const newValues = fieldValues.map(v => {+ const date = new Date(v);+ return date.getMinutes();+ });+ return {+ ...data,+ [resKey]: newValues,+ };+ }+ case 'second': {+ const newValues = fieldValues.map(v => {+ const date = new Date(v);+ return date.getSeconds();+ });+ return {+ ...data,+ [resKey]: newValues,+ };+ }+ default: {+ return data;+ }+ }+}+++export default dateTimeDrill;diff --git a/packages/graphic-walker/src/locales/en-US.json b/packages/graphic-walker/src/locales/en-US.jsonindex fae160d8..85447c0b 100644--- a/packages/graphic-walker/src/locales/en-US.json+++ b/packages/graphic-walker/src/locales/en-US.json@@ -266,11 +266,13 @@"logCustom": "Log(customize)","drill": {"name": "Time Unit",+ "feature_name": "Time feature","levels": {"year": "Year","quarter": "Quarter","month": "Month","week": "Week",+ "weekday": "Weekday","day": "Day","hour": "Hour","minute": "Minute",diff --git a/packages/graphic-walker/src/locales/ja-JP.json b/packages/graphic-walker/src/locales/ja-JP.jsonindex 54a0871f..c40b3530 100644--- a/packages/graphic-walker/src/locales/ja-JP.json+++ b/packages/graphic-walker/src/locales/ja-JP.json@@ -260,11 +260,13 @@"logCustom": "対数(カスタム)","drill": {"name": "時間単位",+ "feature_name": "時間特性","levels": {"year": "年","quarter": "四半期","month": "月","week": "週",+ "weekday": "曜日","day": "日","hour": "時","minute": "分",diff --git a/packages/graphic-walker/src/locales/zh-CN.json b/packages/graphic-walker/src/locales/zh-CN.jsonindex 0363ea61..d185a6fe 100644--- a/packages/graphic-walker/src/locales/zh-CN.json+++ b/packages/graphic-walker/src/locales/zh-CN.json@@ -266,11 +266,13 @@"logCustom": "自定义对数","drill": {"name": "时间粒度",+ "feature_name": "时间特征","levels": {"year": "年","quarter": "季度","month": "月","week": "周",+ "weekdat": "星期几","day": "日","hour": "时","minute": "分",diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.tsindex f065ff04..7f8567b8 100644--- a/packages/graphic-walker/src/store/visualSpecStore.ts+++ b/packages/graphic-walker/src/store/visualSpecStore.ts@@ -19,7 +19,7 @@ import {IGeoUrl,} from '../interfaces';import { CHANNEL_LIMIT, MetaFieldKeys } from '../config';-import { DATE_TIME_DRILL_LEVELS } from "../constants";+import { DATE_TIME_DRILL_LEVELS, DATE_TIME_FEATURE_LEVELS } from "../constants";import { VisSpecWithHistory } from '../models/visSpecHistory';import {IStoInfo,@@ -582,7 +582,9 @@ export class VizSpecStore {encodings[stateKey].push(logField);});}- public createDateTimeDrilledField(stateKey: keyof DraggableFieldState, index: number, drillLevel: typeof DATE_TIME_DRILL_LEVELS[number], name: string | ((originFieldName: string) => string), format: string) {+ public createDateTimeDrilledField(stateKey: keyof DraggableFieldState, index: number, op: 'dateTimeDrill', drillLevel: typeof DATE_TIME_DRILL_LEVELS[number], name: string | ((originFieldName: string) => string), format: string)+ public createDateTimeDrilledField(stateKey: keyof DraggableFieldState, index: number, op: 'dateTimeFeature', drillLevel: typeof DATE_TIME_FEATURE_LEVELS[number], name: string | ((originFieldName: string) => string), format: string)+ public createDateTimeDrilledField(stateKey, index, op, drillLevel, name, format) {if (stateKey === "filters") {return;}@@ -594,13 +596,13 @@ export class VizSpecStore {fid: newVarKey,dragId: newVarKey,name: typeof name === 'function' ? name(originField.name) : name,- semanticType: "temporal",+ semanticType: op === 'dateTimeDrill' ? "temporal" : "ordinal",analyticType: originField.analyticType,aggName: 'sum',computed: true,- timeUnit: drillLevel,+ timeUnit: op === 'dateTimeDrill' ? drillLevel : undefined,expression: {- op: "dateTimeDrill",+ op,as: newVarKey,params: [{