antvis / S2

⚡️ A practical visualization library for tabular analysis.
https://s2.antv.antgroup.com
MIT License
1.46k stars 194 forks source link

🐛小计总计功能 自定义总计函数(calcFunc)中无小计汇总信息 #2795

Closed chen610620 closed 2 months ago

chen610620 commented 2 months ago

🏷 Version

Package Version
@antv/s2 1.55.0
@antv/s2-react 1.47.0
@antv/s2-vue

Sheet Type

🖋 Description

小计总计功能 自定义总计函数(calcFunc)中无小计汇总信息。 仅有最小单元数据。 原有使用 s2: 1.47.1, s2-react: 1.40.0中, 自定义汇总函数的第二个入参 包含单元格数据+小计汇总数据。 升级到最新版本(1.55.0)后,自定义汇总函数的第二个入参, 仅包含单元格数据。

存在差异情况

⌨️ Code Snapshots

bug重现可使用官网demo修改配置进行复现:

官网示例 js配置修改如下

import { PivotSheet } from '@antv/s2';

fetch(
  'https://gw.alipayobjects.com/os/bmw-prod/4347c2dd-6554-451b-9d44-15b04e5de657.json',
)
  .then((res) => res.json())
  .then((data) => {
    const container = document.getElementById('container');
    const s2DataConfig = {
      fields: {
        rows: ['province', 'city'],
        columns: ['type'],
        values: ['price'],
      },
      meta: [
        {
          field: 'province',
          name: '省份',
        },
        {
          field: 'city',
          name: '城市',
        },
        {
          field: 'type',
          name: '商品类别',
        },
        {
          field: 'price',
          name: '价格',
        },
      ],
      data: data.filter(function (a) {
        return Object.keys(a).length>2;
      }),
    };

    const s2Options = {
      width: 600,
      height: 480,
      hierarchyType: 'tree',
      tooltip: {
        showTooltip: true,
      },
      interaction: {
        enableCopy: true,
        selectedCellsSpotlight: true,
        hoverHighlight: true,
      },
      totals: {
        row: { 
          showGrandTotals: true,
          showSubTotals: true,
          // reverseLayout: true,

          // subTotalsDimensions: ['province'],
            calcTotals: {
            calcFunc: function (a, arr) {
              console.log("bb", arr)
              return 15;
            },
          },
        },
        col: {
          showGrandTotals: true,
          showSubTotals: true,
          // reverseLayout: true,
          // reverseSubLayout: true,
          // subTotalsDimensions: ['type'],
          calcTotals: {
            calcFunc: function (a, arr) {
              console.log("aa", arr)
              return 10;
            },
          },
        },
      },
    };
    const s2 = new PivotSheet(container, s2DataConfig, s2Options);

    s2.render();
  });

🤔 Steps to Reproduce

无操作步骤, 仅打开调试控制台即可

😊 Expected Behavior

预期行为:控制台输出应包含小计数据, eg:

  {
    "type": "笔",
    "province": "浙江",
    "price": "3"
  },

😅 Current Behavior

当前仅包含单元格数据

💻 System information

Environment Info
System window 10
Browser 126.0.6478.127
lijinke666 commented 2 months ago

请 @wjgogogo 看一下

lijinke666 commented 2 months ago

https://github.com/antvis/S2/pull/2476/files#diff-9d3da27ee08082af42cbc082ff77ca8c4eb71e0075c0dbe73393811496854df3R369-R371

image

lijinke666 commented 2 months ago

calcFunc 无汇总数据是有意为之, 自定义汇总本质上是对明细数据进行汇总, 如果汇总数据混入其中的话会导致数据二次累加, 会导致 BUG, 如果你希望获取全部的数据, 可以根据 query 主动查询一次, 代替第二个参数 (ps: 什么情况需要获取汇总数据, 理论上不需要才对吧).

import { QueryDataType } from '@antv/s2';

calcFunc(query) {
+  const allData = s2.dataSet.getMultiData(query, {
+    queryType: QueryDataType.All,
+  });
  console.log(allData);
  return 15;
},

这里可能无法拿到 s2 实例, 近期我们增加第三个参数好了, 更灵活一点

chen610620 commented 2 months ago

ok, 好的。那我就主动查询一次解决问题。

chen610620 commented 2 months ago

ok, 好的。那我就主动查询一次解决问题。

lijinke666 commented 2 months ago

:tada: This issue has been resolved in version @antv/s2-v1.55.7 :tada:

The release is available on:

Your semantic-release bot :package::rocket: