Skip to content

An open source alternative to Tableau. Embeddable visual analytic

feat: pivot-table

jojocysf003b97f5301
diff --git a/packages/graphic-walker/package.json b/packages/graphic-walker/package.json
index 00976215..f8eb1d57 100644
--- a/packages/graphic-walker/package.json
+++ b/packages/graphic-walker/package.json
@@ -1,6 +1,6 @@
{
"name": "@kanaries/graphic-walker",
- "version": "0.3.2",
+ "version": "0.3.3-a",
"scripts": {
"dev:front_end": "vite --host",
"dev": "npm run dev:front_end",
diff --git a/packages/graphic-walker/src/components/pivotTable/index.tsx b/packages/graphic-walker/src/components/pivotTable/index.tsx
index 8a08254a..51ca6467 100644
--- a/packages/graphic-walker/src/components/pivotTable/index.tsx
+++ b/packages/graphic-walker/src/components/pivotTable/index.tsx
@@ -17,6 +17,21 @@ import { INestNode } from './inteface';
import { buildMetricTableFromNestTree, buildNestTree } from './utils';
import { unstable_batchedUpdates } from 'react-dom';
import MetricTable from './metricTable';
+import { toJS } from 'mobx';
+
+// const PTStateConnector = observer(function StateWrapper (props: PivotTableProps) {
+// const store = usePivotTableStore();
+// const { vizStore } = useGlobalStore();
+// const { draggableFieldState } = vizStore;
+// const { rows, columns } = draggableFieldState;
+// return (
+// <PivotTable
+// {...props}
+// draggableFieldState={draggableFieldState}
+// visualConfig={visualConfig}
+// />
+// );
+// })
interface PivotTableProps {
themeKey?: IThemeKey;
@@ -26,11 +41,11 @@ interface PivotTableProps {
draggableFieldState: DeepReadonly<DraggableFieldState>;
visualConfig: IVisualConfig;
}
-const PivotTable: React.FC<PivotTableProps> = observer((props) => {
- const { data } = props;
+const PivotTable: React.FC<PivotTableProps> = (props) => {
+ const { data, draggableFieldState } = props;
// const store = usePivotTableStore();
- const { vizStore } = useGlobalStore();
- const { draggableFieldState } = vizStore;
+ // const { vizStore } = useGlobalStore();
+ // const { draggableFieldState } = vizStore;
const { rows, columns } = draggableFieldState;
const [leftTree, setLeftTree] = useState<INestNode | null>(null);
const [topTree, setTopTree] = useState<INestNode | null>(null);
@@ -52,10 +67,6 @@ const PivotTable: React.FC<PivotTableProps> = observer((props) => {
return columns.filter((f) => f.analyticType === 'measure');
}, [columns]);
- const measures = useMemo(() => {
- return [...measInRow, ...measInColumn];
- }, [measInRow, measInColumn]);
-
useEffect(() => {
if ((dimsInRow.length > 0 || dimsInColumn.length > 0) && data.length > 0) {
const lt = buildNestTree(
@@ -67,11 +78,6 @@ const PivotTable: React.FC<PivotTableProps> = observer((props) => {
data
);
const metric = buildMetricTableFromNestTree(lt, tt, data);
- // console.log({
- // lt,
- // tt,
- // metric,
- // });
// debugger
unstable_batchedUpdates(() => {
setLeftTree(lt);
@@ -81,26 +87,26 @@ const PivotTable: React.FC<PivotTableProps> = observer((props) => {
}
}, [dimsInRow, dimsInColumn, data]);
- // console.log('render');
-
// const { leftTree, topTree, metricTable } = store;
return (
- <div>
- <div>
- <h1>left</h1>
- {leftTree && <LeftTree data={leftTree} dimsInRow={dimsInRow} measInRow={measInRow} />}
- </div>
- <div>
- <h1>top</h1>
+ <div className="flex">
+ <table className="border border-gray-300 border-collapse">
+ <thead className="border border-gray-300">
+ {new Array(dimsInColumn.length + (measInColumn.length > 0 ? 1 : 0)).fill(0).map((_, i) => (
+ <tr className="" key={i}>
+ <td className="p-2 m-1 text-xs text-white border border-gray-300" colSpan={dimsInRow.length + (measInRow.length > 0 ? 1 : 0)}>_</td>
+ </tr>
+ ))}
+ </thead>
+ {leftTree && <LeftTree data={leftTree} dimsInRow={dimsInRow} measInRow={measInRow} />}
+ </table>
+ <table className="border border-gray-300 border-collapse">
{topTree && <TopTree data={topTree} dimsInCol={dimsInColumn} measInCol={measInColumn} />}
- </div>
- <div>
- <h1>metric</h1>
- <MetricTable matrix={metricTable} measures={measures} />
- </div>
+ {metricTable && <MetricTable matrix={metricTable} meaInColumns={measInColumn} meaInRows={measInRow} />}
+ </table>
</div>
);
-});
+};
export default PivotTable;
diff --git a/packages/graphic-walker/src/components/pivotTable/leftTree.tsx b/packages/graphic-walker/src/components/pivotTable/leftTree.tsx
index a198434f..8d28269c 100644
--- a/packages/graphic-walker/src/components/pivotTable/leftTree.tsx
+++ b/packages/graphic-walker/src/components/pivotTable/leftTree.tsx
@@ -27,7 +27,7 @@ function renderTree(node: INestNode, dimsInRow: IField[], depth: number, cellRow
cellRows[cellRows.length - 1].push(
<td
key={nanoid()}
- className="whitespace-nowrap p-2 text-xs text-gray-500 bg-gray-200 m-1 border border-red-500"
+ className="whitespace-nowrap p-2 text-xs text-gray-500 m-1 border border-gray-300"
rowSpan={childrenSize * Math.max(meaNumber, 1)}
>
{node.value}
@@ -52,6 +52,7 @@ const LeftTree: React.FC<TreeProps> = (props) => {
const nodeCells: ReactNode[] = useMemo(() => {
const cellRows: ReactNode[][] = [[]];
renderTree(data, dimsInRow, 0, cellRows, measInRow.length);
+ cellRows[0].shift();
if (measInRow.length > 0) {
const ans: ReactNode[][] = [];
for (let row of cellRows) {
@@ -59,7 +60,7 @@ const LeftTree: React.FC<TreeProps> = (props) => {
...row,
<td
key={nanoid()}
- className="whitespace-nowrap p-2 text-xs text-gray-500 bg-gray-200 m-1 border border-red-500"
+ className="whitespace-nowrap p-2 text-xs text-gray-500 m-1 border border-gray-300"
>
{measInRow[0].name}
</td>,
@@ -68,7 +69,7 @@ const LeftTree: React.FC<TreeProps> = (props) => {
ans.push([
<td
key={nanoid()}
- className="whitespace-nowrap p-2 text-xs text-gray-500 bg-gray-200 m-1 border border-red-500"
+ className="whitespace-nowrap p-2 text-xs text-gray-500 m-1 border border-gray-300"
>
{measInRow[j].name}
</td>,
@@ -80,13 +81,13 @@ const LeftTree: React.FC<TreeProps> = (props) => {
return cellRows;
}, [data, dimsInRow, measInRow]);
return (
- <div>
- <thead>
- {nodeCells.map((row, rIndex) => (
- <tr key={rIndex}>{row}</tr>
- ))}
- </thead>
- </div>
+ <thead className="bg-gray-50 border border-gray-300 border border-gray-300">
+ {nodeCells.map((row, rIndex) => (
+ <tr className="border border-gray-300" key={rIndex}>
+ {row}
+ </tr>
+ ))}
+ </thead>
);
};
diff --git a/packages/graphic-walker/src/components/pivotTable/metricTable.tsx b/packages/graphic-walker/src/components/pivotTable/metricTable.tsx
index f9d1cdef..25186c73 100644
--- a/packages/graphic-walker/src/components/pivotTable/metricTable.tsx
+++ b/packages/graphic-walker/src/components/pivotTable/metricTable.tsx
@@ -3,20 +3,88 @@ import { IField } from '../../interfaces';
interface MetricTableProps {
matrix: any[][];
- measures: IField[];
+ meaInRows: IField[];
+ meaInColumns: IField[];
}
const MetricTable: React.FC<MetricTableProps> = (props) => {
- const { matrix, measures } = props;
- // console.log(props)
+ const { matrix, meaInRows, meaInColumns } = props;
+
return (
- <tbody className="divide-y divide-gray-200 bg-white ">
+ <tbody className="bg-white border-r border-b border-gray-300">
{matrix.map((row, rIndex) => {
+ if (meaInRows.length !== 0) {
+ return meaInRows.map((rowMea, rmIndex) => {
+ return (
+ <tr className="divide-x divide-gray-200" key={`${rIndex}-${rowMea.fid}`}>
+ {
+ row.flatMap((cell, cIndex) => {
+ cell = cell ?? {};
+ if (meaInColumns.length !== 0) {
+ return meaInColumns.map((colMea, cmIndex) => (
+ <td
+ className="whitespace-nowrap p-2 text-xs text-gray-500"
+ key={`${rIndex}-${cIndex}-${rowMea.fid}-${colMea.fid}-${colMea.fid}`}
+ >
+ {cell[rowMea.fid] ?? '--'} , {cell[colMea.fid] ?? '--'}
+ </td>
+ ));
+ }
+ return (
+ <td
+ className="whitespace-nowrap p-2 text-xs text-gray-500"
+ key={`${rIndex}-${cIndex}-${rowMea.fid}`}
+ >
+ {cell[rowMea.fid] ?? '--'}
+ </td>
+ );
+ })
+ }
+ </tr>
+ );
+ });
+ }
return (
<tr className="divide-x divide-gray-200" key={rIndex}>
{row.flatMap((cell, cIndex) => {
- return measures.map((mea) => (
- <td className='whitespace-nowrap p-2 text-xs text-gray-500' key={`${rIndex}-${cIndex}-${mea.fid}`}>{cell[mea.fid] ?? '--'}</td>
- ));
+ cell = cell ?? {};
+ if (meaInRows.length === 0 && meaInColumns.length !== 0) {
+ return meaInColumns.map((colMea, cmIndex) => (
+ <td
+ className="whitespace-nowrap p-2 text-xs text-gray-500"
+ key={`${rIndex}-${cIndex}-${cmIndex}-${colMea.fid}`}
+ >
+ {cell[meaInColumns[cmIndex].fid] ?? '--'}
+ </td>
+ ));
+ }else if (meaInRows.length === 0 && meaInColumns.length === 0) {
+ return (
+ <td
+ className="whitespace-nowrap p-2 text-xs text-gray-500"
+ key={`${rIndex}-${cIndex}`}
+ >
+ {`${cell}`}
+ </td>
+ );
+ } else {
+ return meaInRows.flatMap((rowMea, rmIndex) => (
+ <td
+ className="whitespace-nowrap p-2 text-xs text-gray-500"
+ key={`${rIndex}-${cIndex}-${rmIndex}-${rowMea.fid}`}
+ >
+ {meaInColumns.flatMap((colMea, cmIndex) => (
+ <td
+ className="whitespace-nowrap p-2 text-xs text-gray-500"
+ key={`${rIndex}-${cIndex}-${rmIndex}-${cmIndex}-${colMea.fid}`}
+ >
+ {cell[rowMea.fid] ?? '--'} , {cell[colMea.fid] ?? '--'}
+ </td>
+ ))}
+ </td>
+ ));
+ }
+ // return measures.map((mea) => (
+ // <td className='whitespace-nowrap p-2 text-xs text-gray-500' key={`${rIndex}-${cIndex}-${mea.fid}`}>{cell[mea.fid] ?? '--'}</td>
+ // ));
})}
</tr>
);
diff --git a/packages/graphic-walker/src/components/pivotTable/topTree.tsx b/packages/graphic-walker/src/components/pivotTable/topTree.tsx
index e9b62add..26375dba 100644
--- a/packages/graphic-walker/src/components/pivotTable/topTree.tsx
+++ b/packages/graphic-walker/src/components/pivotTable/topTree.tsx
@@ -24,16 +24,13 @@ function renderTree(node: INestNode, dimsInCol: IField[], depth: number, cellRow
return;
}
cellRows[depth].push(
- <td key={nanoid()} className="whitespace-nowrap p-2 text-xs text-gray-500 bg-gray-200 m-1 border border-red-500" colSpan={childrenSize * Math.max(meaNumber, 1)}>
+ <td key={nanoid()} className="whitespace-nowrap p-2 text-xs text-gray-500 m-1 border border-gray-300" colSpan={childrenSize * Math.max(meaNumber, 1)}>
{node.value}
</td>
);
for (let i = 0; i < node.children.length; i++) {
const child = node.children[i];
renderTree(child, dimsInCol, depth + 1, cellRows, meaNumber);
- // if (i < node.children.length - 1) {
- // cellRows.push([])
- // }
}
}
@@ -44,26 +41,20 @@ export interface TreeProps {
}
const TopTree: React.FC<TreeProps> = (props) => {
const { data, dimsInCol, measInCol } = props;
- console.log({
- data,
- dimsInCol,
- measInCol,
- })
const nodeCells: ReactNode[] = useMemo(() => {
const cellRows: ReactNode[][] = new Array(dimsInCol.length + 1).fill(0).map(() => []);
renderTree(data, dimsInCol, 0, cellRows, measInCol.length);
const totalChildrenSize = cellRows[cellRows.length - 1].length;
- cellRows.push(new Array(totalChildrenSize).fill(0).flatMap(() => measInCol.map((m) => <td key={nanoid()} className="whitespace-nowrap p-2 text-xs text-gray-500 bg-gray-200 m-1 border border-red-500">{m.name}</td>)));
+ cellRows.push(new Array(totalChildrenSize).fill(0).flatMap(() => measInCol.map((m) => <td key={nanoid()} className="whitespace-nowrap p-2 text-xs text-gray-500 m-1 border border-gray-300">{m.name}</td>)));
+ cellRows.shift();
return cellRows;
}, [data, dimsInCol, measInCol]);
return (
- <div>
- <thead>
+ <thead className='border border-gray-300 bg-gray-50 border border-gray-300'>
{nodeCells.map((row, rIndex) => (
- <tr key={rIndex}>{row}</tr>
+ <tr className="border border-gray-300" key={rIndex}>{row}</tr>
))}
</thead>
- </div>
);
};
diff --git a/packages/graphic-walker/src/components/pivotTable/utils.ts b/packages/graphic-walker/src/components/pivotTable/utils.ts
index 00e2185f..d1b18428 100644
--- a/packages/graphic-walker/src/components/pivotTable/utils.ts
+++ b/packages/graphic-walker/src/components/pivotTable/utils.ts
@@ -118,7 +118,6 @@ export function buildMetricTableFromNestTree (leftTree: INestNode, topTree: INes
iteLeft.first();
// return mat;
while (iteLeft.current !== null) {
- mat.push([])
const vec: any[] = [];
iteTop.first();
while (iteTop.current !== null) {