fix(图表): 透视表没有列维度时自定义行总计显示错误 #12081

This commit is contained in:
wisonic 2024-10-09 15:16:49 +08:00
parent 2335d3eca4
commit 29452a1cbc
2 changed files with 26 additions and 11 deletions

View File

@ -74,7 +74,9 @@ public class TablePivotHandler extends GroupChartHandler {
var rowAxis = view.getXAxis();
var colAxis = view.getXAxisExt();
var dataMap = new HashMap<String, Object>();
var quotaIds = view.getYAxis().stream().map(ChartViewFieldDTO::getDataeaseName).collect(Collectors.toSet());
if (CollectionUtils.isEmpty(rowAxis)) {
return dataMap;
}
// 行总计列维度聚合加上自定义字段
var row = tableTotal.getRow();
if (row.isShowGrandTotals()) {
@ -116,7 +118,7 @@ public class TablePivotHandler extends GroupChartHandler {
}
// 列总计行维度聚合加上自定义字段
var col = tableTotal.getCol();
if (col.isShowGrandTotals()) {
if (col.isShowGrandTotals() && CollectionUtils.isNotEmpty(colAxis)) {
var yAxis = getCustomFields(view, col.getCalcTotals().getCfg());
if (!yAxis.isEmpty()) {
var result = getData(sqlMeta, rowAxis, yAxis, allFields, crossDs, dsMap, view, provider, needOrder);
@ -129,7 +131,7 @@ public class TablePivotHandler extends GroupChartHandler {
}
}
// 列小计行维度聚合自定义指标数 * (列维度的数量 - 1)
if (col.isShowSubTotals()) {
if (col.isShowSubTotals() && colAxis.size() >= 2) {
var yAxis = getCustomFields(view, col.getCalcSubTotals().getCfg());
if (!yAxis.isEmpty()) {
var tmpData = new ArrayList<Map<String, Object>>();
@ -173,7 +175,7 @@ public class TablePivotHandler extends GroupChartHandler {
}
}
// 行总计里面的列小计
if (row.isShowGrandTotals() && col.isShowSubTotals()) {
if (row.isShowGrandTotals() && col.isShowSubTotals() && colAxis.size() >= 2) {
var yAxis = getCustomFields(view, col.getCalcTotals().getCfg());
if (!yAxis.isEmpty()) {
var tmpData = new ArrayList<Map<String, Object>>();
@ -194,7 +196,7 @@ public class TablePivotHandler extends GroupChartHandler {
}
}
// 列总计里面的行小计
if (col.isShowGrandTotals() && row.isShowGrandTotals()) {
if (col.isShowGrandTotals() && row.isShowGrandTotals() && rowAxis.size() >= 2) {
var yAxis = getCustomFields(view, row.getCalcTotals().getCfg());
if (!yAxis.isEmpty()) {
var tmpData = new ArrayList<Map<String, Object>>();
@ -215,7 +217,7 @@ public class TablePivotHandler extends GroupChartHandler {
}
}
// 行小计和列小计相交部分
if (row.isShowSubTotals() && col.isShowSubTotals()) {
if (row.isShowSubTotals() && col.isShowSubTotals() && colAxis.size() >= 2 && rowAxis.size() >= 2) {
var yAxis = getCustomFields(view, col.getCalcTotals().getCfg());
if (!yAxis.isEmpty()) {
var tmpData = new ArrayList<List<Map<String, Object>>>();
@ -250,6 +252,14 @@ public class TablePivotHandler extends GroupChartHandler {
private Map<String, Object> buildCustomCalcResult(List<String[]> data, List<ChartViewFieldDTO> dimAxis, List<ChartViewFieldDTO> quotaAxis) {
var rootResult = new HashMap<String, Object>();
if (CollectionUtils.isEmpty(dimAxis)) {
var rowData = data.getFirst();
for (int i = 0; i < rowData.length; i++) {
var qAxis = quotaAxis.get(i);
rootResult.put(qAxis.getDataeaseName(), rowData[i]);
}
return rootResult;
}
for (int i = 0; i < data.size(); i++) {
var rowData = data.get(i);
Map<String, Object> curSubMap = rootResult;

View File

@ -509,7 +509,7 @@ function getCustomCalcResult(query, axisMap, status: TotalStatus, customCalc) {
const rowPath = getTreePath(query, row)
const colPath = getTreePath(query, col)
const path = [...rowPath, ...colPath]
const { data } = colSubTotal[subLevel]
const { data } = colSubTotal?.[subLevel]
let val
if (path.length && data) {
path.push(quotaField)
@ -526,6 +526,11 @@ function getCustomCalcResult(query, axisMap, status: TotalStatus, customCalc) {
path.push(quotaField)
val = get(rowTotal.data, path)
}
// 列维度为空行维度不为空
if (!col.length && row.length) {
const path = [query[EXTRA_FIELD]]
val = get(rowTotal.data, path)
}
return val
}
// 行小计
@ -535,7 +540,7 @@ function getCustomCalcResult(query, axisMap, status: TotalStatus, customCalc) {
const colPath = getTreePath(query, col)
const rowPath = getTreePath(query, row)
const path = [...colPath, ...rowPath]
const { data } = rowSubTotal[rowLevel]
const { data } = rowSubTotal?.[rowLevel]
let val
if (path.length && rowSubTotal) {
path.push(quotaField)
@ -547,7 +552,7 @@ function getCustomCalcResult(query, axisMap, status: TotalStatus, customCalc) {
if (status.isRowTotal && status.isColSubTotal) {
const { colSubInRowTotal } = customCalc
const colLevel = getSubLevel(query, col)
const { data } = colSubInRowTotal[colLevel]
const { data } = colSubInRowTotal?.[colLevel]
const colPath = getTreePath(query, col)
let val
if (colPath.length && colSubInRowTotal) {
@ -560,7 +565,7 @@ function getCustomCalcResult(query, axisMap, status: TotalStatus, customCalc) {
if (status.isColTotal && status.isRowSubTotal) {
const { rowSubInColTotal } = customCalc
const rowSubLevel = getSubLevel(query, row)
const data = rowSubInColTotal[rowSubLevel]?.data
const data = rowSubInColTotal?.[rowSubLevel]?.data
const path = getTreePath(query, row)
let val
if (path.length && rowSubInColTotal) {
@ -574,7 +579,7 @@ function getCustomCalcResult(query, axisMap, status: TotalStatus, customCalc) {
const { rowSubInColSub } = customCalc
const rowSubLevel = getSubLevel(query, row)
const colSubLevel = getSubLevel(query, col)
const { data } = rowSubInColSub[rowSubLevel][colSubLevel]
const { data } = rowSubInColSub?.[rowSubLevel]?.[colSubLevel]
const rowPath = getTreePath(query, row)
const colPath = getTreePath(query, col)
const path = [...rowPath, ...colPath]