Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 22, 2026

问题描述

customValueOrder=0 时,多个数值列的总计表头被错误合并到一起。

错误行为:总计节点的 ID 为 root[&]总计(总计被添加到度量值层级)

期望行为:总计节点的 ID 应为 root[&]price[&]总计root[&]number[&]总计(每个度量值下分别有总计)

修复方案

  1. add-totals.ts:在添加总计时增加 currentField !== EXTRA_FIELD 的检查,避免在度量字段层级添加总计。

  2. build-gird-hierarchy.ts:修改 isFirstField 的判断逻辑,使用 fields.findIndex() 找到第一个非 EXTRA_FIELD 的维度字段索引,确保总计是在第一个维度字段处添加。

Solve the issue and close #3215

Original prompt

分析issue #3215 是否是本仓库的代码bug,如果是,请进行修复,并解释修复思路。最终提交的PullRequest的标题和描述都应该是中文的。Pull Request的描述中需要Solve the issue and close #3215


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 22, 2026 08:44
Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com>
Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com>
Copilot AI changed the title [WIP] 修复问题 #3215 中的代码错误 fix: 修复 customValueOrder=0 时多个数值列的总计表头被合并问题 Jan 22, 2026
Copilot AI requested a review from Alexzjt January 22, 2026 08:51
@Alexzjt
Copy link
Contributor

Alexzjt commented Jan 22, 2026

@copilot 你的修改是错误的,以下面的代码为例。
1、在没有customValueOrder: 0这个配置的时候,列头展示的是 总计 -> 数量、价格,即第一层是总计,第二层拆分为数量、价格.即s2.facet.getLayoutResult().colLeafNodes的前两个元素 ID是 root[&]总计[&]price、root[&]总计[&]number
2、用户认为是bug的case:在添加customValueOrder: 0之后,列头展示的是一个合并后的总计单元格,数量、价格单元格都不存在了。即s2.facet.getLayoutResult().colLeafNodes的第一个元素 ID是 root[&]总计
3、用户希望的case:在添加customValueOrder: 0之后,列头展示的是 数量 -> 总计、价格 -> 总计。即第一层是数量、价格,分别都有第二层为总计。即s2.facet.getLayoutResult().colLeafNodes的第一个元素 ID是 root[&]price[&]总计、root[&]number[&]总计

import { PivotSheet, S2DataConfig, S2Options } from '@antv/s2';
const container = document.getElementById('container');
const s2DataConfig: S2DataConfig = {
  fields: {
    rows: ['province', 'city'],
    columns: ['type', 'sub_type'],
    values: ['number', 'price'],
    valueInCols: true,
    // 自定义指标属性层级顺序, 即 fields.values 的顺序 (从 0 开始)
    customValueOrder: 0,
  },
  data: [
    {
      "number": 7789,
      "province": "浙江省",
      "city": "杭州市",
      "type": "家具",
      "sub_type": "桌子",
      "price": 1899
    },
  ],
  meta: [
    {
      field: 'number',
      name: '数量',
    },
    {
      field: "price",
      name: "价格",
    },
    {
      field: 'province',
      name: '省份',
    },
    {
      field: 'city',
      name: '城市',
    },
    {
      field: 'type',
      name: '类别',
    },
    {
      field: 'sub_type',
      name: '子类别',
    },
  ],
};

const s2Options: S2Options = {
  width: 600,
  height: 480,
  cornerExtraFieldText: "度量",
  totals: {
    col: {
      showGrandTotals: true,
      showSubTotals: true,
      reverseGrandTotalsLayout: true,
      reverseSubTotalsLayout: true,

    },
    row: {
      reverseGrandTotalsLayout: true,
      reverseSubTotalsLayout: true,
      showGrandTotals: true,
      showSubTotals: true,
      subTotalsDimensions: ['price'],
    }
  }
};

const s2 = new PivotSheet(container, s2DataConfig, s2Options);

s2.render();

请你重新思考,先查找 customValueOrder 在代码中的用法,再根据前面的案例思考并解决。

Copilot AI and others added 2 commits January 22, 2026 09:45
Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com>
Co-authored-by: Alexzjt <9548248+Alexzjt@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 22, 2026

@copilot 你的修改是错误的,以下面的代码为例。
1、在没有customValueOrder: 0这个配置的时候,列头展示的是 总计 -> 数量、价格,即第一层是总计,第二层...

感谢指出问题!我重新分析了代码逻辑,发现之前的修复确实不正确。

问题根因是当 customValueOrder=0 时,EXTRA_FIELD 被放在 columns 数组的位置 0,原代码在 addTotals 中当 isFirstField=true 时添加总计,但没有检查当前字段是否为 EXTRA_FIELD,导致总计被错误地添加到度量值层级(生成 root[&]总计),而不是在每个度量值下添加(期望 root[&]price[&]总计root[&]number[&]总计)。

修复方案(commit 45909c0):

  1. add-totals.ts:添加 currentField !== EXTRA_FIELD 检查,避免在度量字段层级添加总计
  2. build-gird-hierarchy.ts:修改 isFirstField 判断,找到第一个非 EXTRA_FIELD 的维度字段索引,确保总计在正确的层级添加

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 customValueOrder=0时,多个数值列的总计列头被合并到一起了

2 participants