Skip to content

Commit

Permalink
fix: columnTree just has virtual node
Browse files Browse the repository at this point in the history
  • Loading branch information
fangsmile committed May 13, 2024
1 parent e8dbfaf commit cd47d33
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/vtable/examples/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ export const menus = [
{
path: 'pivot',
name: 'virtual-node'
},
{
path: 'pivot',
name: 'virtual-node-2'
}
]
},
Expand Down
207 changes: 207 additions & 0 deletions packages/vtable/examples/pivot/virtual-node-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import * as VTable from '../../src';
import { bindDebugTool } from '../../src/scenegraph/debug-tool';
const PivotTable = VTable.PivotTable;
const CONTAINER_ID = 'vTable';

export function createTable() {
let tableInstance;
fetch('https://lf9-dp-fe-cms-tos.byteorg.com/obj/bit-cloud/VTable/North_American_Superstore_Pivot_Chart_data.json')
.then(res => res.json())
.then(data => {
const option: VTable.PivotTableConstructorOptions = {
records: data,
rows: [
{
dimensionKey: 'Category',
title: 'Category',
headerStyle: {
textStick: true,
bgColor(arg) {
if (arg.dataValue === 'Row Totals') {
return '#a5b7fc';
}
return '#ECF1F5';
}
},
width: 'auto'
},
{
dimensionKey: 'Sub-Category',
title: 'Sub-Catogery',
headerStyle: {
textStick: true,
bgColor(arg) {
if (arg.dataValue === 'Sub Totals') {
return '#d2dcff';
}
return '#ECF1F5';
}
},
width: 'auto'
}
],
columns: [
{
dimensionKey: 'Segment-1',
title: 'Segment-custom-virtual'
}
],
columnTree: [
{
dimensionKey: 'Segment-1',
value: 'Segment-1 (virtual-node)',
virtual: true
// children: [
// {
// indicatorKey: 'Quantity',
// value: 'Quantity'
// },
// {
// indicatorKey: 'Sales',
// value: 'Sales'
// },
// {
// indicatorKey: 'Profit',
// value: 'Profit'
// }
// ]
}
],
indicators: [
{
indicatorKey: 'Quantity',
title: 'Quantity',
width: 'auto',
showSort: false,
headerStyle: {
fontWeight: 'normal'
},
style: {
padding: [16, 28, 16, 28],
color(args) {
if (args.dataValue >= 0) {
return 'black';
}
return 'red';
},
bgColor(arg) {
const rowHeaderPaths = arg.cellHeaderPaths.rowHeaderPaths;
if (rowHeaderPaths?.[1]?.value === 'Sub Totals') {
return '#d2dcff';
} else if (rowHeaderPaths?.[0]?.value === 'Row Totals') {
return '#a5b7fc';
}
return undefined;
}
}
},
{
indicatorKey: 'Sales',
title: 'Sales',
width: 'auto',
showSort: false,
headerStyle: {
fontWeight: 'normal'
},
format: rec => {
return '$' + Number(rec).toFixed(2);
},
style: {
padding: [16, 28, 16, 28],
color(args) {
if (args.dataValue >= 0) {
return 'black';
}
return 'red';
},
bgColor(arg) {
const rowHeaderPaths = arg.cellHeaderPaths.rowHeaderPaths;
if (rowHeaderPaths?.[1]?.value === 'Sub Totals') {
return '#d2dcff';
} else if (rowHeaderPaths?.[0]?.value === 'Row Totals') {
return '#a5b7fc';
}
return undefined;
}
}
},
{
indicatorKey: 'Profit',
title: 'Profit',
width: 'auto',
showSort: false,
headerStyle: {
fontWeight: 'normal'
},
format: rec => {
return '$' + Number(rec).toFixed(2);
},
style: {
padding: [16, 28, 16, 28],
color(args) {
if (args.dataValue >= 0) {
return 'black';
}
return 'red';
},
bgColor(arg) {
const rowHeaderPaths = arg.cellHeaderPaths.rowHeaderPaths;
if (rowHeaderPaths?.[1]?.value === 'Sub Totals') {
return '#d2dcff';
} else if (rowHeaderPaths?.[0]?.value === 'Row Totals') {
return '#a5b7fc';
}
return undefined;
}
}
}
],
corner: {
titleOnDimension: 'column'
},
dataConfig: {
totals: {
row: {
showGrandTotals: true,
showSubTotals: true,
showSubTotalsOnTop: true,
showGrandTotalsOnTop: true,
subTotalsDimensions: ['Category'],
grandTotalLabel: 'Row Totals',
subTotalLabel: 'Sub Totals'
},
column: {
showGrandTotals: true,
showSubTotals: false,
grandTotalLabel: 'Column Totals'
}
}
},
theme: VTable.themes.DEFAULT.extends({
headerStyle: {
bgColor: '#5071f9',
color(args) {
if (
(args.cellHeaderPaths.colHeaderPaths?.length === 1 && args.cellHeaderPaths.colHeaderPaths[0].virtual) ||
(args.cellHeaderPaths.colHeaderPaths?.length === 2 && args.cellHeaderPaths.colHeaderPaths[1].virtual)
) {
return 'red';
}
return '#fff';
}
},
cornerHeaderStyle: {
bgColor: '#5071f9',
color: '#fff'
}
}),

widthMode: 'standard'
};
tableInstance = new VTable.PivotTable(document.getElementById(CONTAINER_ID), option);
window.tableInstance = tableInstance;
})
.catch(e => {
console.log(e);
});
}
5 changes: 3 additions & 2 deletions packages/vtable/src/dataset/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
sortBy,
typeSort
} from './statistics-helper';
import { IndicatorDimensionKeyPlaceholder } from '../tools/global';
/**
* 数据处理模块
*/
Expand Down Expand Up @@ -558,7 +559,7 @@ export class Dataset {
const rowAttr = this.rows[l];
if (rowAttr in record) {
rowKey.push(record[rowAttr]);
} else {
} else if (rowAttr !== IndicatorDimensionKeyPlaceholder) {
//如果数据中缺失某个维度的值 可以认为是用户传入的汇总数据
if (
this.dataConfig?.totals?.row?.showGrandTotals &&
Expand Down Expand Up @@ -589,7 +590,7 @@ export class Dataset {
const colAttr = this.columns[n];
if (colAttr in record) {
colKey.push(record[colAttr]);
} else {
} else if (colAttr !== IndicatorDimensionKeyPlaceholder) {
//如果数据中缺失某个维度的值 可以认为是用户传入的汇总数据
if (
this.dataConfig?.totals?.column?.showGrandTotals &&
Expand Down
8 changes: 6 additions & 2 deletions packages/vtable/src/layout/layout-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export function checkHasTreeDefine(layoutMap: SimpleHeaderLayoutMap) {
export function parseColKeyRowKeyForPivotTable(table: PivotTable, options: PivotTableConstructorOptions) {
let columnDimensionTree;
let rowDimensionTree;
let isNeedResetColumnDimensionTree = false;
let isNeedResetRowDimensionTree = false;
let isNeedResetColumnDimensionTree = true;
let isNeedResetRowDimensionTree = true;
if (options.columnTree) {
columnDimensionTree = new DimensionTree(
(table.internalProps.columnTree as ITreeLayoutHeadNode[]) ?? [],
Expand All @@ -84,6 +84,8 @@ export function parseColKeyRowKeyForPivotTable(table: PivotTable, options: Pivot
options.indicators?.length >= 1
) {
isNeedResetColumnDimensionTree = true;
} else {
isNeedResetColumnDimensionTree = false;
}
}
if (options.rowTree) {
Expand All @@ -99,6 +101,8 @@ export function parseColKeyRowKeyForPivotTable(table: PivotTable, options: Pivot
options.indicators?.length >= 1
) {
isNeedResetRowDimensionTree = true;
} else {
isNeedResetRowDimensionTree = false;
}
}
const rowKeys = rowDimensionTree?.dimensionKeys?.count
Expand Down
6 changes: 4 additions & 2 deletions packages/vtable/src/layout/pivot-header-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ export class PivotHeaderLayoutMap implements LayoutMapAPI {
this.sharedVar.seqId = Math.max(this.sharedVar.seqId, this._headerObjects.length);
//生成cornerHeaderObjs及_cornerHeaderCellIds
if (this.cornerSetting.titleOnDimension === 'column') {
const colDimensionKeys = this.columnDimensionTree.dimensionKeysIncludeVirtual.valueArr();
this.cornerHeaderObjs = this._addCornerHeaders(
this.columnHeaderTitle ? [''].concat(this.colDimensionKeys as any) : this.colDimensionKeys,
this.columnHeaderTitle ? [''].concat(colDimensionKeys) : colDimensionKeys,
this.columnsDefine
);
} else if (this.cornerSetting.titleOnDimension === 'row') {
Expand All @@ -291,8 +292,9 @@ export class PivotHeaderLayoutMap implements LayoutMapAPI {
this.rowsDefine.concat(extensionRowDimensions)
);
} else {
const rowDimensionKeys = this.rowDimensionTree.dimensionKeysIncludeVirtual.valueArr();
this.cornerHeaderObjs = this._addCornerHeaders(
this.rowHeaderTitle ? [''].concat(this.rowDimensionKeys as any) : this.rowDimensionKeys,
this.rowHeaderTitle ? [''].concat(rowDimensionKeys) : rowDimensionKeys,
this.rowsDefine
);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/vtable/src/layout/tree-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class DimensionTree {
// blockStartIndexMap: Map<number, boolean> = new Map();
// blockEndIndexMap: Map<number, boolean> = new Map();
dimensionKeys: NumberMap<string> = new NumberMap<string>();
dimensionKeysIncludeVirtual: NumberMap<string> = new NumberMap<string>();
// dimensions: IDimension[] | undefined;//目前用不到这个
cache: Map<number, any> = new Map();
constructor(
Expand All @@ -101,6 +102,7 @@ export class DimensionTree {
// this.dimensions = dimensions;
this.cache.clear();
this.dimensionKeys = new NumberMap<string>();
this.dimensionKeysIncludeVirtual = new NumberMap<string>();
this.tree.children = tree as ITreeLayoutHeadNode[];
// const re = { totalLevel: 0 };
// if (updateTreeNode) this.updateTreeNode(this.tree, 0, re, this.tree);
Expand All @@ -123,6 +125,16 @@ export class DimensionTree {
) {
this.dimensionKeys.put(node.level, node.indicatorKey ? IndicatorDimensionKeyPlaceholder : node.dimensionKey);
}
if (
!this.dimensionKeysIncludeVirtual.contain(
node.indicatorKey ? IndicatorDimensionKeyPlaceholder : node.dimensionKey
)
) {
this.dimensionKeysIncludeVirtual.put(
node.level,
node.indicatorKey ? IndicatorDimensionKeyPlaceholder : node.dimensionKey
);
}
if (!node.id) {
node.id = ++this.sharedVar.seqId;
}
Expand Down

0 comments on commit cd47d33

Please sign in to comment.