fix(图表): 修复明细表分组无效

This commit is contained in:
wisonic-s
2025-08-22 15:51:23 +08:00
committed by wisonic-s
parent 32d4af75ba
commit 09fe33ede7
5 changed files with 45 additions and 50 deletions

View File

@@ -30,7 +30,8 @@ import {
getColumns,
getCustomTheme,
getLeafNodes,
mapKeyToField
mapKeyToField,
setupColumnTitle
} from '@/views/chart/components/js/panel/common/common_table'
const { t } = useI18n()
@@ -487,27 +488,6 @@ const renderTable = (chart: ChartObj) => {
})
}
const getNonLeafNodes = (tree: Array<ColumnNode>): string[] => {
const result: string[] = []
const inorderTraversal = (node: ColumnNode) => {
// 如果有子节点,则为非叶子节点
if (node.children?.length > 0) {
result.push(node.field)
// 递归处理子节点
for (let i = 0; i < node.children.length; i++) {
inorderTraversal(node.children[i] as ColumnNode)
}
}
}
// 遍历树中所有节点
tree.forEach(node => inorderTraversal(node))
return result
}
const getTreesMaxDepth = (nodes: Array<ColumnNode>): number => {
if (!nodes?.length) {
return 0
@@ -527,22 +507,6 @@ const getTreesMaxDepth = (nodes: Array<ColumnNode>): number => {
return Math.max(...rootDepths)
}
const setupColumnTitle = (nodes: Array<ColumnNode>, nameMap: Record<string, string>) => {
nodes.forEach(node => {
if (node.children) {
node.children.forEach(child => {
if (nameMap[child.field]) {
child.title = nameMap[child.field]
}
})
setupColumnTitle(node.children as Array<ColumnNode>, nameMap)
} else {
if (nameMap[node.field]) {
node.title = nameMap[node.field]
}
}
})
}
const resize = debounce((width, height) => {
if (s2) {
s2.changeSheetSize(width, height)

View File

@@ -477,8 +477,7 @@ export const DEFAULT_TABLE_HEADER: ChartTableHeaderAttr = {
isColBolder: true,
headerGroup: false,
headerGroupConfig: {
columns: [],
meta: []
columns: []
}
}
export const DEFAULT_TABLE_CELL: ChartTableCellAttr = {

View File

@@ -27,7 +27,9 @@ import {
getColumns,
drawImage,
getSummaryRow,
SummaryCell
SummaryCell,
mapKeyToField,
setupColumnTitle
} from '@/views/chart/components/js/panel/common/common_table'
const { t } = useI18n()
@@ -110,10 +112,9 @@ export class TableInfo extends S2ChartView<TableSheet> {
if (f?.hide === true) {
return
}
columns.push(ele.dataeaseName)
columns.push({ field: ele.dataeaseName, title: ele.chartShowName ?? ele.name })
meta.push({
field: ele.dataeaseName,
name: ele.chartShowName ?? ele.name,
formatter: function (value) {
if (!f) {
return value
@@ -138,18 +139,32 @@ export class TableInfo extends S2ChartView<TableSheet> {
if (headerGroup && showTableHeader !== false) {
const { headerGroupConfig } = tableHeader
if (headerGroupConfig?.columns?.length) {
const allKeys = columns.map(c => drillFieldMap[c] || c)
// 存量配置转换
if (headerGroupConfig.columns[0].key) {
mapKeyToField(headerGroupConfig.columns as unknown as ColumnNode[])
}
const nameMap =
chart.xAxis?.reduce((pre, cur) => {
pre[cur.dataeaseName] = cur.name
return pre
}, {}) || {}
if (headerGroupConfig.meta?.length) {
headerGroupConfig.meta.forEach(m => {
nameMap[m.field] = m.name
})
}
setupColumnTitle(headerGroupConfig.columns as unknown as ColumnNode[], nameMap)
const allKeys = columns.map(c => drillFieldMap[c] || c.field)
const leafNodes = getLeafNodes(headerGroupConfig.columns as ColumnNode[])
const leafKeys = leafNodes.map(c => c.key)
const leafKeys = leafNodes.map(c => c.field)
if (isEqual(leafKeys, allKeys)) {
if (Object.keys(drillFieldMap).length) {
const originField = Object.values(drillFieldMap)[0]
const drillField = Object.keys(drillFieldMap)[0]
const [drillCol] = getColumns([originField], headerGroupConfig.columns as ColumnNode[])
drillCol.key = drillField
drillCol.field = drillField
}
columns.splice(0, columns.length, ...headerGroupConfig.columns)
meta.push(...headerGroupConfig.meta)
}
}
}

View File

@@ -2157,6 +2157,23 @@ export const mapKeyToField = (nodes: Array<ColumnNode>) => {
})
}
export const setupColumnTitle = (nodes: Array<ColumnNode>, nameMap: Record<string, string>) => {
nodes.forEach(node => {
if (node.children) {
node.children.forEach(child => {
if (nameMap[child.field]) {
child.title = nameMap[child.field]
}
})
setupColumnTitle(node.children as Array<ColumnNode>, nameMap)
} else {
if (nameMap[node.field]) {
node.title = nameMap[node.field]
}
}
})
}
export const getColumns = (fields, cols: Array<ColumnNode>): Array<ColumnNode> => {
const result = []
for (let i = 0; i < cols.length; i++) {

View File

@@ -92,9 +92,9 @@ export abstract class S2ChartView<P extends SpreadSheet> extends AntVAbstractCha
break
case 'rowCell':
case 'colCell':
content = meta.field
field = find(metaConfig, item => item.field === content)
if (field) {
content = meta.value
field = metaConfig.find(item => item.field === content)
if (field?.name) {
content = field.name
}
break