Skip to content

Commit

Permalink
[IMP] pivot: add indentation to row headers
Browse files Browse the repository at this point in the history
This commit adds indentation to row headers in the pivot table, based
on the depth of the groupBy.

closes #4961

Task: 3965246
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
hokolomopo committed Sep 17, 2024
1 parent 20c0646 commit 966e6d2
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 440 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { deepCopy } from "../../../../helpers";
import {
NEXT_VALUE,
PREVIOUS_VALUE,
getFieldDisplayName,
} from "../../../../helpers/pivot/pivot_helpers";
import { NEXT_VALUE, PREVIOUS_VALUE } from "../../../../helpers/pivot/pivot_domain_helpers";
import { getFieldDisplayName } from "../../../../helpers/pivot/pivot_helpers";
import { Get } from "../../../../store_engine";
import { SpreadsheetStore } from "../../../../stores";
import { _t } from "../../../../translation";
Expand Down
4 changes: 3 additions & 1 deletion src/functions/module_lookup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getFullReference, range, splitReference, toXC, toZone } from "../helpers/index";
import { addRowIndentToPivotHeader } from "../helpers/pivot/pivot_helpers";
import { _t } from "../translation";
import { AddFunctionDescription, FunctionResultObject, Matrix, Maybe, Zone } from "../types";
import { CellErrorType, EvaluationError, InvalidReferenceError } from "../types/errors";
Expand Down Expand Up @@ -829,7 +830,8 @@ export const PIVOT = {
result[col].push({ value: "" });
break;
case "HEADER":
result[col].push(pivot.getPivotHeaderValueAndFormat(pivotCell.domain));
const valueAndFormat = pivot.getPivotHeaderValueAndFormat(pivotCell.domain);
result[col].push(addRowIndentToPivotHeader(pivot, pivotCell.domain, valueAndFormat));
break;
case "MEASURE_HEADER":
result[col].push(pivot.getPivotMeasureValue(pivotCell.measure, pivotCell.domain));
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/pivot/pivot_domain_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
PivotNode,
} from "../../types";
import { clip, deepCopy } from "../misc";
import { NEXT_VALUE, PREVIOUS_VALUE } from "./pivot_helpers";

export const PREVIOUS_VALUE = "(previous)";
export const NEXT_VALUE = "(next)";

export function getDomainOfParentRow(pivot: Pivot, domain: PivotDomain): PivotDomain {
const { colDomain, rowDomain } = domainToColRowDomain(pivot, domain);
Expand Down
32 changes: 28 additions & 4 deletions src/helpers/pivot/pivot_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import { average, countAny, max, min } from "../../functions/helper_statistical"
import { inferFormat, toBoolean, toNumber, toString } from "../../functions/helpers";
import { Registry } from "../../registries/registry";
import { _t } from "../../translation";
import { CellValue, DEFAULT_LOCALE, FunctionResultObject, Locale, Matrix } from "../../types";
import {
CellValue,
DEFAULT_LOCALE,
FunctionResultObject,
Locale,
Matrix,
Pivot,
} from "../../types";
import { EvaluationError } from "../../types/errors";
import {
Granularity,
Expand All @@ -14,6 +21,7 @@ import {
PivotField,
PivotTableCell,
} from "../../types/pivot";
import { domainToColRowDomain } from "./pivot_domain_helpers";
import { PivotRuntimeDefinition } from "./pivot_runtime_definition";
import { pivotTimeAdapter } from "./pivot_time_adapter";

Expand Down Expand Up @@ -262,9 +270,25 @@ pivotToFunctionValueRegistry
.add("boolean", (value: CellValue) => (toBoolean(value) ? "TRUE" : "FALSE"))
.add("char", (value: CellValue) => `"${toString(value).replace(/"/g, '\\"')}"`);

export const PREVIOUS_VALUE = "(previous)";
export const NEXT_VALUE = "(next)";

export function getFieldDisplayName(field: PivotDimension) {
return field.displayName + (field.granularity ? ` (${ALL_PERIODS[field.granularity]})` : "");
}

export function addRowIndentToPivotHeader(
pivot: Pivot,
domain: PivotDomain,
functionResult: FunctionResultObject
): FunctionResultObject {
const { rowDomain } = domainToColRowDomain(pivot, domain);
if (rowDomain.length === 0) {
return functionResult;
}
const indent = rowDomain.length - 1;

const format = functionResult.format || "@";

return {
...functionResult,
format: `${" ".repeat(indent)}${format}* `,
};
}
9 changes: 3 additions & 6 deletions src/helpers/pivot/pivot_presentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
import { CellErrorType, NotAvailableError } from "../../types/errors";
import { deepEquals, removeDuplicates, transpose2dPOJO } from "../misc";
import {
NEXT_VALUE,
PREVIOUS_VALUE,
domainToColRowDomain,
domainToString,
getDimensionDomain,
Expand All @@ -31,12 +33,7 @@ import {
isFieldInDomain,
replaceFieldValueInDomain,
} from "./pivot_domain_helpers";
import {
AGGREGATORS_FN,
NEXT_VALUE,
PREVIOUS_VALUE,
toNormalizedPivotValue,
} from "./pivot_helpers";
import { AGGREGATORS_FN, toNormalizedPivotValue } from "./pivot_helpers";
import { PivotParams, PivotUIConstructor } from "./pivot_registry";

const PERCENT_FORMAT = "0.00%";
Expand Down
12 changes: 6 additions & 6 deletions src/helpers/pivot/pivot_time_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const dayOfWeekAdapter: PivotTimeAdapterNotNull<number> = {
const index = (normalizedValue - 1 + (locale || DEFAULT_LOCALE).weekStart) % 7;
return {
value: DAYS[index].toString(),
format: "0",
format: "@",
};
},
toFunctionValue(normalizedValue) {
Expand Down Expand Up @@ -159,7 +159,7 @@ const monthNumberAdapter: PivotTimeAdapterNotNull<number> = {
toValueAndFormat(normalizedValue) {
return {
value: MONTHS[toNumber(normalizedValue, DEFAULT_LOCALE) - 1].toString(),
format: "0",
format: "@",
};
},
toFunctionValue(normalizedValue) {
Expand All @@ -183,7 +183,7 @@ const quarterNumberAdapter: PivotTimeAdapterNotNull<number> = {
toValueAndFormat(normalizedValue) {
return {
value: _t("Q%(quarter_number)s", { quarter_number: normalizedValue }),
format: "0",
format: "@",
};
},
toFunctionValue(normalizedValue) {
Expand Down Expand Up @@ -222,7 +222,7 @@ const hourNumberAdapter: PivotTimeAdapterNotNull<number> = {
toValueAndFormat(normalizedValue) {
return {
value: _t("%(hour_number)sh", { hour_number: normalizedValue }),
format: "0",
format: "@",
};
},
toFunctionValue(normalizedValue) {
Expand All @@ -246,7 +246,7 @@ const minuteNumberAdapter: PivotTimeAdapterNotNull<number> = {
toValueAndFormat(normalizedValue) {
return {
value: _t("%(minute_number)s'", { minute_number: normalizedValue }),
format: "0",
format: "@",
};
},
toFunctionValue(normalizedValue) {
Expand All @@ -270,7 +270,7 @@ const secondNumberAdapter: PivotTimeAdapterNotNull<number> = {
toValueAndFormat(normalizedValue) {
return {
value: _t("%(second_number)s''", { second_number: normalizedValue }),
format: "0",
format: "@",
};
},
toFunctionValue(normalizedValue) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/pivot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NEXT_VALUE, PREVIOUS_VALUE } from "../helpers/pivot/pivot_helpers";
import { NEXT_VALUE, PREVIOUS_VALUE } from "../helpers/pivot/pivot_domain_helpers";
import { CellValue } from "./cells";
import { Format } from "./format";
import { Locale } from "./locale";
Expand Down
68 changes: 34 additions & 34 deletions tests/pivots/pivot_calculated_measure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ describe("Pivot calculated measure", () => {
// prettier-ignore
expect(getEvaluatedGrid(model, "A5:C12")).toEqual(
[
["(#1) Pivot", "Total", "",],
["", "calc Customer", "calc Category"],
["Alice", "1", "2"],
["Food", "Alice", "Food"],
["Drink", "Alice", "Drink"],
["Bob", "1", "1"],
["Food", "Bob", "Food"],
["Total", "2", "2"],
["(#1) Pivot", "Total", "",],
["", "calc Customer", "calc Category"],
["Alice", "1", "2"],
[" Food", "Alice", "Food"],
[" Drink", "Alice", "Drink"],
["Bob", "1", "1"],
[" Food", "Bob", "Food"],
["Total", "2", "2"],
]
);
});
Expand Down Expand Up @@ -259,12 +259,12 @@ describe("Pivot calculated measure", () => {
// prettier-ignore
expect(getEvaluatedGrid(model, "A5:C10")).toEqual(
[
["(#1) Pivot", "10", "Total"],
["", "calc Category", "calc Category"],
["Alice", "2", "2"],
["Food", "Food", "1"],
["Drink", "Drink", "1"],
["Total", "2", "2" ],
["(#1) Pivot", "10", "Total"],
["", "calc Category", "calc Category"],
["Alice", "2", "2"],
[" Food", "Food", "1"],
[" Drink", "Drink", "1"],
["Total", "2", "2" ],
]
);
});
Expand Down Expand Up @@ -354,14 +354,14 @@ describe("Pivot calculated measure", () => {
// prettier-ignore
expect(getEvaluatedGrid(model, "A10:C17")).toEqual(
[
["(#1) Pivot", "Total", ""],
["", "Price", "Commission"],
["Alice", "30", "3"],
["Food", "10", "1"],
["Drink", "20", "2"],
["Bob", "10", "3"],
["Food", "10", "3"],
["Total", "40", "6" ],
["(#1) Pivot", "Total", ""],
["", "Price", "Commission"],
["Alice", "30", "3"],
[" Food", "10", "1"],
[" Drink", "20", "2"],
["Bob", "10", "3"],
[" Food", "10", "3"],
["Total", "40", "6" ],
]
);
});
Expand Down Expand Up @@ -550,12 +550,12 @@ describe("Pivot calculated measure", () => {
});
// prettier-ignore
expect(getEvaluatedGrid(model, "D1:F6")).toEqual([
["(#1) Pivot", "Total", ""],
["", "Price", "calculated"],
["Alice", "30", "50"], // 50 = 20 + 30
["2020", "10", "20"], // 20 = 10 + 10
["2021", "20", "30"], // 30 = 20 + 10
["Total", "30", "50"],
["(#1) Pivot", "Total", ""],
["", "Price", "calculated"],
["Alice", "30", "50"], // 50 = 20 + 30
[" 2020", "10", "20"], // 20 = 10 + 10
[" 2021", "20", "30"], // 30 = 20 + 10
["Total", "30", "50"],
]);
});

Expand Down Expand Up @@ -744,12 +744,12 @@ describe("Pivot calculated measure", () => {
});
// prettier-ignore
expect(getEvaluatedGrid(model, "D1:F6")).toEqual([
["(#1) Pivot", "Total", ""],
["", "Price", "calculated"],
["Alice", "1", "11"],
["2020", "10", "20"],
["2021", "1", "11"],
["Total", "1", "11"],
["(#1) Pivot", "Total", ""],
["", "Price", "calculated"],
["Alice", "1", "11"],
[" 2020", "10", "20"],
[" 2021", "1", "11"],
["Total", "1", "11"],
]);
});

Expand Down
Loading

0 comments on commit 966e6d2

Please sign in to comment.