An open source alternative to Tableau. Embeddable visual analytic
TypeScript bidatadata-analysisdata-miningdata-visualizationedak6skanarieslow-codepivot-tablereacttableautableau-alternativetypescriptvegavega-litevisualization
feat: import json data with nested structure (#118)
islxyqwee24c1bf8391b
diff --git a/packages/graphic-walker/src/components/dataTable/index.tsx b/packages/graphic-walker/src/components/dataTable/index.tsxindex 6a52a6d3..bc060df1 100644--- a/packages/graphic-walker/src/components/dataTable/index.tsx+++ b/packages/graphic-walker/src/components/dataTable/index.tsx@@ -1,10 +1,10 @@-import React, { useMemo, useState } from "react";-import styled from "styled-components";-import { IMutField, IRow } from "../../interfaces";-import { useTranslation } from "react-i18next";-import Pagination from "./pagination";-import { ChevronUpDownIcon } from "@heroicons/react/24/outline";-import DropdownContext from "../dropdownContext";+import React, { useMemo, useState } from 'react';+import styled from 'styled-components';+import { IMutField, IRow } from '../../interfaces';+import { useTranslation } from 'react-i18next';+import Pagination from './pagination';+import { ChevronUpDownIcon } from '@heroicons/react/24/outline';+import DropdownContext from '../dropdownContext';interface DataTableProps {size?: number;@@ -32,34 +32,83 @@ const Container = styled.div`}}`;-const ANALYTIC_TYPE_LIST = ["dimension", "measure"];-const SEMANTIC_TYPE_LIST = ["nominal", "ordinal", "quantitative", "temporal"];+const ANALYTIC_TYPE_LIST = ['dimension', 'measure'];+const SEMANTIC_TYPE_LIST = ['nominal', 'ordinal', 'quantitative', 'temporal'];// function getCellType(field: IMutField): 'number' | 'text' {// return field.dataType === 'number' || field.dataType === 'integer' ? 'number' : 'text';// }-function getHeaderType(field: IMutField): "number" | "text" {- return field.analyticType === "dimension" ? "text" : "number";+function getHeaderType(field: IMutField): 'number' | 'text' {+ return field.analyticType === 'dimension' ? 'text' : 'number';}function getHeaderClassNames(field: IMutField) {- return field.analyticType === "dimension" ? "border-t-4 border-blue-400" : "border-t-4 border-purple-400";+ return field.analyticType === 'dimension' ? 'border-t-4 border-blue-400' : 'border-t-4 border-purple-400';}function getSemanticColors(field: IMutField): string {switch (field.semanticType) {- case "nominal":- return "border border-transparent bg-sky-100 text-sky-800 dark:bg-sky-900 dark:text-sky-100 dark:border-sky-600";- case "ordinal":- return "border border-transparent bg-indigo-100 text-indigo-800 dark:bg-indigo-900 dark:text-indigo-100 dark:border-indigo-600";- case "quantitative":- return "border border-transparent bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-100 dark:border-purple-600";- case "temporal":- return "border border-transparent bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-100 dark:border-yellow-600";+ case 'nominal':+ return 'border border-transparent bg-sky-100 text-sky-800 dark:bg-sky-900 dark:text-sky-100 dark:border-sky-600';+ case 'ordinal':+ return 'border border-transparent bg-indigo-100 text-indigo-800 dark:bg-indigo-900 dark:text-indigo-100 dark:border-indigo-600';+ case 'quantitative':+ return 'border border-transparent bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-100 dark:border-purple-600';+ case 'temporal':+ return 'border border-transparent bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-100 dark:border-yellow-600';default:- return "border border-transparent bg-gray-400";+ return 'border border-transparent bg-gray-400';}}+type wrapMutField = {+ colSpan: number;+ rowSpan: number;+} & (+ | { type: 'field'; value: IMutField; fIndex: number }+ | {+ type: 'name';+ value: string;+ }+);++const getHeaders = (metas: IMutField[]): wrapMutField[][] => {+ const height = metas.map((x) => x.path?.length ?? 1).reduce((a, b) => Math.max(a, b), 0);+ const result: wrapMutField[][] = [...Array(height)].map(() => []);+ let now = 1;+ metas.forEach((x, fIndex) => {+ const path = x.path ?? [x.name ?? x.fid];+ if (path.length > now) {+ for (let i = now - 1; i < path.length - 1; i++) {+ result[i].push({+ colSpan: 0,+ rowSpan: 1,+ type: 'name',+ value: path[i],+ });+ }+ }+ now = path.length;+ for (let i = 0; i < path.length - 1; i++) {+ result[i][result[i].length - 1].colSpan++;+ }+ result[path.length - 1].push({+ type: 'field',+ value: x,+ colSpan: 1,+ rowSpan: height - path.length + 1,+ fIndex,+ });+ });+ return result;+};++const getHeaderKey = (f: wrapMutField) => {+ if (f.type === 'name') {+ return f.value;+ }+ return f.value.name ?? f.value.fid;+};+const DataTable: React.FC<DataTableProps> = (props) => {const { size = 10, data, metas, onMetaChange } = props;const [pageIndex, setPageIndex] = useState(0);@@ -82,6 +131,8 @@ const DataTable: React.FC<DataTableProps> = (props) => {const from = pageIndex * size;const to = Math.min((pageIndex + 1) * size, data.length - 1);+ const headers = useMemo(() => getHeaders(metas), [metas]);+return (<Container className="rounded border-gray-200 dark:border-gray-700 border"><Pagination@@ -97,70 +148,99 @@ 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-700">- {metas.map((field, fIndex) => (- <th key={field.fid} className={""}>- <div- className={- getHeaderClassNames(field) +- " whitespace-nowrap py-3.5 px-6 text-left text-xs font-semibold text-gray-900 dark:text-gray-50 sm:pl-6"- }+ {headers.map((row) => (+ <tr+ className="divide-x divide-gray-200 dark:divide-gray-700"+ key={`row_${getHeaderKey(row[0])}`}+ >+ {row.map((f) => (+ <th+ colSpan={f.colSpan}+ rowSpan={f.rowSpan}+ key={getHeaderKey(f)}+ className={'align-top'}>- <b>{field.name || field.fid}</b>- <div>- <DropdownContext- options={analyticTypeList}- onSelect={(value) => {- onMetaChange(field.fid, fIndex, {- analyticType: value as IMutField["analyticType"],- });- }}+ {f.type === 'name' && (+ <div+ className={+ 'border-t-4 border-yellow-400 whitespace-nowrap py-3.5 text-left text-xs font-semibold text-gray-900 dark:text-gray-50'+ }>- <span- className={- "cursor-pointer inline-flex px-2.5 py-0.5 text-xs font-medium mt-1 rounded-full text-xs text-white " +- (field.analyticType === "dimension" ? "bg-blue-500" : "bg-purple-500")- }- >- {field.analyticType}- <ChevronUpDownIcon className="ml-2 w-3" />- </span>- </DropdownContext>- </div>- <div>- <DropdownContext- options={semanticTypeList}- onSelect={(value) => {- onMetaChange(field.fid, fIndex, {- semanticType: value as IMutField["semanticType"],- });- }}+ <b className="sticky inset-x-0 w-fit px-6 sm:pl-6">{f.value}</b>+ </div>+ )}+ {f.type === 'field' && (+ <div+ className={+ getHeaderClassNames(f.value) ++ ' whitespace-nowrap py-3.5 px-6 text-left text-xs font-semibold text-gray-900 dark:text-gray-50 sm:pl-6'+ }>- <span- className={- "cursor-pointer inline-flex px-2.5 py-0.5 text-xs font-medium mt-1 rounded-full text-xs " +- getSemanticColors(field)- }- >- {field.semanticType}- <ChevronUpDownIcon className="ml-2 w-3" />- </span>- </DropdownContext>- </div>- </div>- </th>- ))}- </tr>+ <b>{f.value.basename || f.value.name || f.value.fid}</b>+ <div>+ <DropdownContext+ options={analyticTypeList}+ onSelect={(value) => {+ onMetaChange(f.value.fid, f.fIndex, {+ analyticType: value as IMutField['analyticType'],+ });+ }}+ >+ <span+ className={+ 'cursor-pointer inline-flex px-2.5 py-0.5 text-xs font-medium mt-1 rounded-full text-xs text-white ' ++ (f.value.analyticType === 'dimension'+ ? 'bg-blue-500'+ : 'bg-purple-500')+ }+ >+ {f.value.analyticType}+ <ChevronUpDownIcon className="ml-2 w-3" />+ </span>+ </DropdownContext>+ </div>+ <div>+ <DropdownContext+ options={semanticTypeList}+ onSelect={(value) => {+ onMetaChange(f.value.fid, f.fIndex, {+ semanticType: value as IMutField['semanticType'],+ });+ }}+ >+ <span+ className={+ 'cursor-pointer inline-flex px-2.5 py-0.5 text-xs font-medium mt-1 rounded-full text-xs ' ++ getSemanticColors(f.value)+ }+ >+ {f.value.semanticType}+ <ChevronUpDownIcon className="ml-2 w-3" />+ </span>+ </DropdownContext>+ </div>+ </div>+ )}+ </th>+ ))}+ </tr>+ ))}</thead><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-700 " + (index % 2 ? "bg-gray-50 dark:bg-gray-900" : "")} 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) => (<tdkey={field.fid + index}className={getHeaderType(field) +- " whitespace-nowrap py-2 pl-4 pr-3 text-xs text-gray-500 dark:text-gray-300 sm:pl-6"+ ' whitespace-nowrap py-2 pl-4 pr-3 text-xs text-gray-500 dark:text-gray-300 sm:pl-6'}>{`${row[field.fid]}`}diff --git a/packages/graphic-walker/src/components/dataTable/pagination.tsx b/packages/graphic-walker/src/components/dataTable/pagination.tsxindex 314e8161..4431ecf6 100644--- a/packages/graphic-walker/src/components/dataTable/pagination.tsx+++ b/packages/graphic-walker/src/components/dataTable/pagination.tsx@@ -12,7 +12,7 @@ export default function Pagination(props: PaginationProps) {const { t } = useTranslation();return (<nav- className="flex items-center justify-between border-t border-gray-200 dark:border-gray-700 bg-white dark:bg-zinc-900 px-4 py-3 sm:px-6"+ className="sticky inset-x-0 flex items-center justify-between border-t border-gray-200 dark:border-gray-700 bg-white dark:bg-zinc-900 px-4 py-3 sm:px-6"aria-label="Pagination"><div className="hidden sm:block">diff --git a/packages/graphic-walker/src/dataSource/utils.ts b/packages/graphic-walker/src/dataSource/utils.tsindex cb7c5eb9..388526c7 100644--- a/packages/graphic-walker/src/dataSource/utils.ts+++ b/packages/graphic-walker/src/dataSource/utils.ts@@ -1,7 +1,6 @@-import { IRow, IMutField } from "../interfaces";-import { inferMeta } from "../lib/inferMeta";-import { guardDataKeys } from "../utils/dataPrep";-+import { IRow, IMutField } from '../interfaces';+import { inferMeta } from '../lib/inferMeta';+import { flatKeys, guardDataKeys } from '../utils/dataPrep';export function transData(dataSource: IRow[]): {dataSource: IRow[];@@ -17,31 +16,34 @@ export function transData(dataSource: IRow[]): {// const rawKeys = Object.keys(sampleRecord);// let flatColKeys: string[] = flatNestKeys(sampleRecord);- const keys = Object.keys(sampleRecord);+ const keys = flatKeys(sampleRecord);const metas = inferMeta({dataSource,fields: keys.map((k) => ({- fid: k,- key: k,- analyticType: "?",- semanticType: "?",+ fid: k[k.length - 1],+ key: k[k.length - 1],+ analyticType: '?',+ semanticType: '?',+ path: k,+ basename: k[k.length - 1],+ name: k.join('.'),})),- })+ });const { safeData, safeMetas } = guardDataKeys(dataSource, metas);const finalData: IRow[] = [];for (let record of safeData) {const newRecord: IRow = {};for (let field of safeMetas) {- if (field.semanticType === "quantitative") {+ if (field.semanticType === 'quantitative') {newRecord[field.fid] = Number(record[field.fid]);} else {- newRecord[field.fid] = record[field.fid];//getValueByKeyPath(record, field.fid);// record[field.fid];+ newRecord[field.fid] = record[field.fid]; //getValueByKeyPath(record, field.fid);// record[field.fid];}}finalData.push(newRecord);}return {dataSource: finalData,- fields: safeMetas+ fields: safeMetas,};}diff --git a/packages/graphic-walker/src/interfaces.ts b/packages/graphic-walker/src/interfaces.tsindex 466a6712..3f6d8870 100644--- a/packages/graphic-walker/src/interfaces.ts+++ b/packages/graphic-walker/src/interfaces.ts@@ -34,18 +34,22 @@ export interface IMutField {fid: string;key?: string;name?: string;+ basename?: string;disable?: boolean;semanticType: ISemanticType;analyticType: IAnalyticType;+ path?: string[];}export interface IUncertainMutField {fid: string;key?: string;name?: string;+ basename?: string;disable?: boolean;semanticType: ISemanticType | '?';analyticType: IAnalyticType | '?';+ path: string[];}export type IExpParamter =@@ -90,6 +94,8 @@ export interface IField {cmp?: (a: any, b: any) => number;computed?: boolean;expression?: IExpression;+ basename?: string;+ path?: [],}export interface IViewField extends IField {diff --git a/packages/graphic-walker/src/lib/inferMeta.ts b/packages/graphic-walker/src/lib/inferMeta.tsindex ef49affd..32362f53 100644--- a/packages/graphic-walker/src/lib/inferMeta.ts+++ b/packages/graphic-walker/src/lib/inferMeta.ts@@ -1,4 +1,5 @@import { IAnalyticType, IMutField, IRow, ISemanticType, IUncertainMutField } from '../interfaces';+import { getValueByKeyPath } from '../utils/dataPrep';const COMMON_TIME_FORMAT: RegExp[] = [/^\d{4}-\d{2}-\d{2}$/, // YYYY-MM-DD@@ -55,12 +56,12 @@ function inferAnalyticTypeFromSemanticType(semanticType: ISemanticType): IAnalyt}}-export function inferSemanticType(data: IRow[], fid: string): ISemanticType {- const values = data.map((row) => row[fid]);+export function inferSemanticType(data: IRow[], path: string[]): ISemanticType {+ const values = data.map((row) => getValueByKeyPath(row, path));let st: ISemanticType = isNumericArray(values) ? 'quantitative' : 'nominal';if (st === 'nominal') {- if (isDateTimeArray(data.map((row) => `${row[fid]}`))) st = 'temporal';+ if (isDateTimeArray(data.map((row) => `${getValueByKeyPath(row, path)}`))) st = 'temporal';}return st;}@@ -70,7 +71,7 @@ export function inferMeta(props: { dataSource: IRow[]; fields: IUncertainMutFielconst finalFieldMetas: IMutField[] = [];for (let field of fields) {let semanticType: ISemanticType =- field.semanticType === '?' ? inferSemanticType(dataSource, field.fid) : field.semanticType;+ field.semanticType === '?' ? inferSemanticType(dataSource, field.path) : field.semanticType;let analyticType: IAnalyticType = inferAnalyticTypeFromSemanticType(semanticType);finalFieldMetas.push({@@ -79,6 +80,8 @@ export function inferMeta(props: { dataSource: IRow[]; fields: IUncertainMutFielname: field.name ? field.name : field.fid,analyticType,semanticType,+ basename: field.basename || field.name || field.fid,+ path: field.path,});}return finalFieldMetas;diff --git a/packages/graphic-walker/src/lib/viewQuery.ts b/packages/graphic-walker/src/lib/viewQuery.tsindex d39722a0..34d9f77d 100644--- a/packages/graphic-walker/src/lib/viewQuery.ts+++ b/packages/graphic-walker/src/lib/viewQuery.ts@@ -1,4 +1,4 @@-import { IMutField, IRow } from "../interfaces";+import { IField, IRow } from "../interfaces";import { aggregate } from "./op/aggregate";import { fold } from "./op/fold";import { IAggQuery, IBinQuery, IFoldQuery, IRawQuery } from "./interfaces";@@ -6,7 +6,7 @@ import { bin } from "./op/bin";export type IViewQuery = IAggQuery | IFoldQuery | IBinQuery | IRawQuery;-export function queryView (rawData: IRow[], metas: IMutField[], query: IViewQuery) {+export function queryView (rawData: IRow[], metas: IField[], query: IViewQuery) {switch (query.op) {case 'aggregate':return aggregate(rawData, query);diff --git a/packages/graphic-walker/src/services.ts b/packages/graphic-walker/src/services.tsindex 4e2695d8..83ce0916 100644--- a/packages/graphic-walker/src/services.ts+++ b/packages/graphic-walker/src/services.ts@@ -1,5 +1,5 @@import { toJS } from 'mobx';-import { IRow, IMutField, IFilterField, Specification } from './interfaces';+import { IRow, IMutField, IField, IFilterField, Specification } from './interfaces';/* eslint import/no-webpack-loader-syntax:0 */// @ts-ignore// eslint-disable-next-line@@ -140,7 +140,7 @@ export const applyFilter = async (data: IRow[], filters: readonly IFilterField[]}};-export const transformDataService = async (data: IRow[], columns: IMutField[]): Promise<IRow[]> => {+export const transformDataService = async (data: IRow[], columns: IField[]): Promise<IRow[]> => {if (columns.length === 0 || data.length === 0) return data;const worker = new TransformDataWorker();try {@@ -156,7 +156,7 @@ export const transformDataService = async (data: IRow[], columns: IMutField[]):}}-export const applyViewQuery = async (data: IRow[], metas: IMutField[], query: IViewQuery): Promise<IRow[]> => {+export const applyViewQuery = async (data: IRow[], metas: IField[], query: IViewQuery): Promise<IRow[]> => {const worker = new ViewQueryWorker();try {const res: IRow[] = await workerService(worker, {diff --git a/packages/graphic-walker/src/store/visualSpecStore.ts b/packages/graphic-walker/src/store/visualSpecStore.tsindex 3b6cf176..792a90d8 100644--- a/packages/graphic-walker/src/store/visualSpecStore.ts+++ b/packages/graphic-walker/src/store/visualSpecStore.ts@@ -336,6 +336,7 @@ export class VizSpecStore {dragId: uniqueId(),fid: f.fid,name: f.name || f.fid,+ basename: f.basename || f.name || f.fid,semanticType: f.semanticType,analyticType: f.analyticType,}));@@ -345,6 +346,7 @@ export class VizSpecStore {dragId: uniqueId(),fid: f.fid,name: f.name || f.fid,+ basename: f.basename || f.name || f.fid,analyticType: f.analyticType,semanticType: f.semanticType,aggName: "sum",diff --git a/packages/graphic-walker/src/utils/dataPrep.ts b/packages/graphic-walker/src/utils/dataPrep.tsindex d6cdedcf..691ac288 100644--- a/packages/graphic-walker/src/utils/dataPrep.ts+++ b/packages/graphic-walker/src/utils/dataPrep.ts@@ -1,11 +1,12 @@import { IMutField, IRow } from "../interfaces";+import { isPlainObject } from "./is-plain-object";-function updateRowKeys(data: IRow[], keyEncodeList: {from: string; to: string}[]): IRow[] {+function updateRowKeys(data: IRow[], keyEncodeList: { from: string[]; to: string }[]): IRow[] {return data.map((row) => {const newRow: IRow = {};for (let k in keyEncodeList) {const { from, to } = keyEncodeList[k];- newRow[to] = row[from];+ newRow[to] = getValueByKeyPath(row, from);}return newRow;});@@ -13,9 +14,9 @@ function updateRowKeys(data: IRow[], keyEncodeList: {from: string; to: string}[]/*** parse column id(key) to a safe string- * @param metas+ * @param metas*/-function parseColumnMetas (metas: IMutField[]) {+function parseColumnMetas(metas: IMutField[]) {return metas.map((meta, i) => {const safeKey = `gwc_${i}`;return {@@ -26,42 +27,34 @@ function parseColumnMetas (metas: IMutField[]) {});}-export function guardDataKeys (data: IRow[], metas: IMutField[]): {+export function guardDataKeys(+ data: IRow[],+ metas: IMutField[]+): {safeData: IRow[];safeMetas: IMutField[];} {- const safeMetas = parseColumnMetas(metas)+ const safeMetas = parseColumnMetas(metas);const keyEncodeList = safeMetas.map((f, i) => ({- from: metas[i].fid,- to: f.fid+ from: metas[i].path ?? [metas[i].fid],+ to: f.fid,}));const safeData = updateRowKeys(data, keyEncodeList);return {safeData,- safeMetas- }+ 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 flatKeys(obj: Object, prefixKeys: string[] = []): string[][] {+ return Object.keys(obj).flatMap((k) =>+ isPlainObject(obj[k]) ? flatKeys(obj[k], prefixKeys.concat(k)) : [prefixKeys.concat(k)]+ );}-export function getValueByKeyPath (object: any, keyPath: string): any {- const keys = keyPath.split(SPLITOR);+export function getValueByKeyPath(object: any, keyPath: string[]): any {let value = object;- for (let key of keys) {+ for (let key of keyPath) {value = value[key];}return value;-}\ No newline at end of file+}diff --git a/packages/graphic-walker/src/utils/is-plain-object.ts b/packages/graphic-walker/src/utils/is-plain-object.tsnew file mode 100644index 00000000..39e230fa--- /dev/null+++ b/packages/graphic-walker/src/utils/is-plain-object.ts@@ -0,0 +1,33 @@+/*!+ * is-plain-object <https://github.com/jonschlinkert/is-plain-object>+ *+ * Copyright (c) 2014-2017, Jon Schlinkert.+ * Released under the MIT License.+ */+function isObject(o: any) {+ return Object.prototype.toString.call(o) === '[object Object]';+}++function isPlainObject(o: any) {+ var ctor, prot;++ if (isObject(o) === false) return false;++ // If has modified constructor+ ctor = o.constructor;+ if (ctor === undefined) return true;++ // If has modified prototype+ prot = ctor.prototype;+ if (isObject(prot) === false) return false;++ // If constructor does not have an Object-specific method+ if (prot.hasOwnProperty('isPrototypeOf') === false) {+ return false;+ }++ // Most likely a plain Object+ return true;+}++export { isPlainObject };