Skip to content

Commit

Permalink
fix: sorting when data is incomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
islxyqwe committed Oct 17, 2023
1 parent 73e796d commit b2af72d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
10 changes: 8 additions & 2 deletions packages/graphic-walker/src/components/pivotTable/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function insertNode(
child = {
key,
value: key,
sort: depth === layerKeys.length - 1 && sort ? nodeData[sort.fid] : key,
sort: depth === layerKeys.length - 1 && sort ? nodeData[sort.fid] ?? `_${key}` : key,
uniqueKey: uniqueKey,
fieldKey: layerKeys[depth],
children: [],
Expand Down Expand Up @@ -93,7 +93,8 @@ export function buildNestTree(
sort?: {
fid: string;
type: 'ascending' | 'descending';
}
},
dataWithoutSort?: IRow[]
): INestNode {
const tree: INestNode = {
key: ROOT_KEY,
Expand All @@ -109,6 +110,11 @@ export function buildNestTree(
for (let row of data) {
insertNode(tree, layerKeys, row, 0, collapsedKeyList, sort);
}
if (dataWithoutSort) {
for (let row of dataWithoutSort) {
insertNode(tree, layerKeys, row, 0, collapsedKeyList, { fid: '', type: sort?.type ?? 'ascending' });
}
}
if (showSummary) {
insertSummaryNode(tree);
}
Expand Down
72 changes: 47 additions & 25 deletions packages/graphic-walker/src/workers/buildPivotTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getFirsts = (item: INestNode): INestNode[] => {
return [item, ...getFirsts(item.children[0])];
}
return [item];
}
};

export function buildPivotTable(
dimsInRow: IViewField[],
Expand All @@ -31,39 +31,61 @@ export function buildPivotTable(
collapsedKeyList,
showTableSummary
);
let data = allData;
if (dimsInColumn.length > 0 ) {
const ks = dimsInColumn.map(x => x.fid);
const vs = getFirsts(tt.children[0]).map(x => x.value);
data = allData.filter(x => ks.every((k, i) => x[k] === vs[i]));
if (dimsInColumn.length > 0) {
const ks = dimsInColumn.map((x) => x.fid);
const vs = getFirsts(tt.children[0]).map((x) => x.value);
// move data of First column to first
const mentioned: IRow[] = [];
const rest: IRow[] = [];
allData.forEach((x) => (ks.every((k, i) => x[k] === vs[i]) ? mentioned.push(x) : rest.push(x)));
lt = buildNestTree(
dimsInRow.map((d) => d.fid),
mentioned,
collapsedKeyList,
showTableSummary,
sort,
rest
);
} else {
lt = buildNestTree(
dimsInRow.map((d) => d.fid),
allData,
collapsedKeyList,
showTableSummary,
sort
);
}
lt = buildNestTree(
dimsInRow.map((d) => d.fid),
data,
collapsedKeyList,
showTableSummary,
sort
);
} else {
lt = buildNestTree(
dimsInRow.map((d) => d.fid),
allData,
collapsedKeyList,
showTableSummary
);
let data = allData;
if (sort && dimsInRow.length > 0 ) {
const ks = dimsInRow.map(x => x.fid);
const vs = getFirsts(lt.children[0]).map(x => x.value);
data = allData.filter(x => ks.every((k, i) => x[k] === vs[i]));
if (sort && dimsInRow.length > 0) {
const ks = dimsInRow.map((x) => x.fid);
const vs = getFirsts(lt.children[0]).map((x) => x.value);
// move data of First row to first
const mentioned: IRow[] = [];
const rest: IRow[] = [];
allData.forEach((x) => (ks.every((k, i) => x[k] === vs[i]) ? mentioned.push(x) : rest.push(x)));
tt = buildNestTree(
dimsInColumn.map((d) => d.fid),
mentioned,
collapsedKeyList,
showTableSummary,
sort,
rest
);
} else {
tt = buildNestTree(
dimsInColumn.map((d) => d.fid),
allData,
collapsedKeyList,
showTableSummary,
sort
);
}
tt = buildNestTree(
dimsInColumn.map((d) => d.fid),
data,
collapsedKeyList,
showTableSummary,
sort?.mode === 'column' ? sort : undefined
);
}

const metric = buildMetricTableFromNestTree(lt, tt, [...allData, ...aggData]);
Expand Down

0 comments on commit b2af72d

Please sign in to comment.