Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add title in pivot table #166

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 62 additions & 67 deletions packages/graphic-walker/src/components/pivotTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ import { useAppRootContext } from '../../components/appRoot';
import { observer } from 'mobx-react-lite';
import LeftTree from './leftTree';
import TopTree from './topTree';
import {
DeepReadonly,
DraggableFieldState,
IComputationFunction,
IDarkMode,
IRow,
IThemeKey,
IViewField,
IVisualConfig,
} from '../../interfaces';
import { DeepReadonly, DraggableFieldState, IComputationFunction, IDarkMode, IRow, IThemeKey, IViewField, IVisualConfig } from '../../interfaces';
import { INestNode } from './inteface';
import { unstable_batchedUpdates } from 'react-dom';
import MetricTable from './metricTable';
Expand All @@ -30,10 +21,10 @@ interface PivotTableProps {
loading: boolean;
draggableFieldState: DeepReadonly<DraggableFieldState>;
visualConfig: DeepReadonly<IVisualConfig>;
computationFunction: IComputationFunction
computationFunction: IComputationFunction;
}

const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableComponent (props) {
const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableComponent(props) {
const { data, visualConfig, loading, computationFunction } = props;
const appRef = useAppRootContext();
const [leftTree, setLeftTree] = useState<INestNode | null>(null);
Expand All @@ -47,7 +38,7 @@ const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableCompon
const { showTableSummary, defaultAggregated } = visualConfig;
const { tableCollapsedHeaderMap } = commonStore;
const aggData = useRef<IRow[]>([]);
const [ topTreeHeaderRowNum, setTopTreeHeaderRowNum ] = useState<number>(0);
const [topTreeHeaderRowNum, setTopTreeHeaderRowNum] = useState<number>(0);

const dimsInRow = useMemo(() => {
return toJS(rows).filter((f) => f.analyticType === 'dimension');
Expand Down Expand Up @@ -88,24 +79,17 @@ const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableCompon
}
}, [tableCollapsedHeaderMap]);

const aggregateThenGenerate = async() => {
const aggregateThenGenerate = async () => {
await aggregateGroupbyData();
generateNewTable();
};

const generateNewTable = () => {
appRef.current?.updateRenderStatus('rendering');
setIsLoading(true);
buildPivotTableService(
dimsInRow,
dimsInColumn,
data,
aggData.current,
Array.from(tableCollapsedHeaderMap.keys()),
showTableSummary
)
buildPivotTableService(dimsInRow, dimsInColumn, data, aggData.current, Array.from(tableCollapsedHeaderMap.keys()), showTableSummary)
.then((data) => {
const {lt, tt, metric} = data;
const { lt, tt, metric } = data;
unstable_batchedUpdates(() => {
setLeftTree(lt);
setTopTree(tt);
Expand All @@ -118,14 +102,14 @@ const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableCompon
appRef.current?.updateRenderStatus('error');
console.log(err);
setIsLoading(false);
})
});
};

const aggregateGroupbyData = () => {
if (dimsInRow.length === 0 && dimsInColumn.length === 0) return;
if (data.length === 0) return;
let groupbyCombListInRow:IViewField[][] = [];
let groupbyCombListInCol:IViewField[][] = [];
let groupbyCombListInRow: IViewField[][] = [];
let groupbyCombListInCol: IViewField[][] = [];
if (showTableSummary) {
groupbyCombListInRow = dimsInRow.map((dim, idx) => dimsInRow.slice(0, idx));
groupbyCombListInCol = dimsInColumn.map((dim, idx) => dimsInColumn.slice(0, idx));
Expand All @@ -138,26 +122,17 @@ const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableCompon
}
groupbyCombListInRow.push(dimsInRow);
groupbyCombListInCol.push(dimsInColumn);
const groupbyCombList:IViewField[][] = groupbyCombListInCol.flatMap(combInCol =>
groupbyCombListInRow.map(combInRow => [...combInCol, ...combInRow])
).slice(0, -1);
const groupbyCombList: IViewField[][] = groupbyCombListInCol
.flatMap((combInCol) => groupbyCombListInRow.map((combInRow) => [...combInCol, ...combInRow]))
.slice(0, -1);
setIsLoading(true);
appRef.current?.updateRenderStatus('computing');
const groupbyPromises: Promise<IRow[]>[] = groupbyCombList.map((dimComb) => {
const workflow = toWorkflow(
viewFilters,
allFields,
dimComb,
viewMeasures,
defaultAggregated,
sort,
limit > 0 ? limit : undefined
);
return dataQueryServer(computationFunction, workflow, limit > 0 ? limit : undefined)
.catch((err) => {
appRef.current?.updateRenderStatus('error');
return [];
});
const workflow = toWorkflow(viewFilters, allFields, dimComb, viewMeasures, defaultAggregated, sort, limit > 0 ? limit : undefined);
return dataQueryServer(computationFunction, workflow, limit > 0 ? limit : undefined).catch((err) => {
appRef.current?.updateRenderStatus('error');
return [];
});
});
return new Promise<void>((resolve, reject) => {
Promise.all(groupbyPromises)
Expand All @@ -172,8 +147,7 @@ const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableCompon
setIsLoading(false);
reject();
});
})

});
};

// const { leftTree, topTree, metricTable } = store;
Expand All @@ -183,40 +157,61 @@ const PivotTable: React.FC<PivotTableProps> = observer(function PivotTableCompon
<div className="flex">
<table className="border border-gray-300 border-collapse">
<thead className="border border-gray-300">
{new Array(topTreeHeaderRowNum).fill(0).map((_, i) => (
{new Array(Math.max(topTreeHeaderRowNum - 1, 0)).fill(0).map((_, i) => (
<tr className="" key={i}>
<td className="bg-zinc-100 dark:bg-zinc-800 text-gray-800 dark:text-gray-100 p-2 m-1 text-xs border border-gray-300" colSpan={dimsInRow.length + (measInRow.length > 0 ? 1 : 0)}>_</td>
<td
className="bg-zinc-100 dark:bg-zinc-800 text-gray-800 dark:text-gray-100 p-2 m-1 text-xs border border-gray-300"
colSpan={dimsInRow.length + (measInRow.length > 0 ? 1 : 0)}
>
_
</td>
</tr>
))}
{topTreeHeaderRowNum > 0 && (
<tr className="">
{dimsInRow.map((x) => (
<td
className="bg-zinc-100 dark:bg-zinc-800 text-gray-800 dark:text-gray-100 p-2 m-1 text-xs border whitespace-nowrap border-gray-300"
colSpan={1}
>
{x.name}
</td>
))}
{measInRow.length > 0 && (
<td
className="bg-zinc-100 dark:bg-zinc-800 text-gray-800 dark:text-gray-100 p-2 m-1 text-xs border border-gray-300"
colSpan={1}
>
_
</td>
)}
</tr>
)}
</thead>
{leftTree &&
<LeftTree
data={leftTree}
dimsInRow={dimsInRow}
measInRow={measInRow}
{leftTree && (
<LeftTree
data={leftTree}
dimsInRow={dimsInRow}
measInRow={measInRow}
onHeaderCollapse={commonStore.updateTableCollapsedHeader.bind(commonStore)}
/>}
/>
)}
</table>
<table className="border border-gray-300 border-collapse">
{topTree &&
<TopTree
data={topTree}
dimsInCol={dimsInColumn}
measInCol={measInColumn}
{topTree && (
<TopTree
data={topTree}
dimsInCol={dimsInColumn}
measInCol={measInColumn}
onHeaderCollapse={commonStore.updateTableCollapsedHeader.bind(commonStore)}
onTopTreeHeaderRowNumChange={(num) => setTopTreeHeaderRowNum(num)}
/>}
{metricTable &&
<MetricTable
matrix={metricTable}
meaInColumns={measInColumn}
meaInRows={measInRow}
/>}
/>
)}
{metricTable && <MetricTable matrix={metricTable} meaInColumns={measInColumn} meaInRows={measInRow} />}
</table>
</div>
</div>

);
});

export default PivotTable;
export default PivotTable;