Skip to content

Commit a95d9a4

Browse files
committed
[IMP] chart: explicit calendar granularities
Using a full explicit definition is better if one day we want to change the default value (without affecting existing charts) And I'm also simplifying chart classes in another branch where we no longer can do `... ?? "day_of_week"` closes #7529 Task: 0 Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
1 parent c093c6a commit a95d9a4

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

packages/o-spreadsheet-engine/src/types/chart/calendar_chart.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ export type CalendarChartGranularity = (typeof CALENDAR_CHART_GRANULARITIES)[num
1919

2020
export interface CalendarChartDefinition extends CommonChartDefinition {
2121
readonly type: "calendar";
22+
readonly horizontalGroupBy: CalendarChartGranularity;
23+
readonly verticalGroupBy: CalendarChartGranularity;
2224
readonly colorScale?: ChartColorScale;
2325
readonly missingValueColor?: Color;
24-
readonly horizontalGroupBy?: CalendarChartGranularity;
25-
readonly verticalGroupBy?: CalendarChartGranularity;
2626
}
2727

2828
export type CalendarChartRuntime = {

src/helpers/figures/charts/calendar_chart.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,10 @@ import {
5050
} from "./runtime";
5151

5252
function checkDateGranularity(definition: CalendarChartDefinition): CommandResult {
53-
if (
54-
definition.horizontalGroupBy &&
55-
!CALENDAR_CHART_GRANULARITIES.includes(definition.horizontalGroupBy)
56-
) {
53+
if (!CALENDAR_CHART_GRANULARITIES.includes(definition.horizontalGroupBy)) {
5754
return CommandResult.InvalidChartDefinition;
5855
}
59-
if (
60-
definition.verticalGroupBy &&
61-
!CALENDAR_CHART_GRANULARITIES.includes(definition.verticalGroupBy)
62-
) {
56+
if (!CALENDAR_CHART_GRANULARITIES.includes(definition.verticalGroupBy)) {
6357
return CommandResult.InvalidChartDefinition;
6458
}
6559
return CommandResult.Success;
@@ -91,8 +85,8 @@ export class CalendarChart extends AbstractChart {
9185
this.showValues = definition.showValues;
9286
this.colorScale = definition.colorScale;
9387
this.axesDesign = definition.axesDesign;
94-
this.horizontalGroupBy = definition.horizontalGroupBy ?? "day_of_week";
95-
this.verticalGroupBy = definition.verticalGroupBy ?? "month_number";
88+
this.horizontalGroupBy = definition.horizontalGroupBy;
89+
this.verticalGroupBy = definition.verticalGroupBy;
9690
this.legendPosition = definition.legendPosition;
9791
this.missingValueColor = definition.missingValueColor;
9892
}
@@ -132,6 +126,8 @@ export class CalendarChart extends AbstractChart {
132126
showValues: context.showValues,
133127
axesDesign: context.axesDesign,
134128
legendPosition,
129+
horizontalGroupBy: "day_of_week",
130+
verticalGroupBy: "month_number",
135131
};
136132
}
137133

tests/figures/chart/calendar/calendar_chart_plugin.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ describe("calendar chart", () => {
174174
legendPosition: "left",
175175
labelRange: "Sheet1!A1:A4",
176176
showValues: false,
177+
horizontalGroupBy: "day_of_week",
178+
verticalGroupBy: "month_number",
177179
axesDesign: {},
178180
});
179181
});

tests/test_helpers/commands_helpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ export function createChart(
241241
cumulative: ("cumulative" in data && data.cumulative) || false,
242242
showSubTotals: ("showSubTotals" in data && data.showSubTotals) || false,
243243
showConnectorLines: ("showConnectorLines" in data && data.showConnectorLines) || false,
244+
horizontalGroupBy: ("horizontalGroupBy" in data && data.horizontalGroupBy) || "day_of_week",
245+
verticalGroupBy: ("verticalGroupBy" in data && data.verticalGroupBy) || "month_number",
244246
};
245247
return model.dispatch("CREATE_CHART", {
246248
figureId: figureData.figureId || model.uuidGenerator.smallUuid(),
@@ -349,8 +351,8 @@ export function createCalendarChart(
349351
labelRange: data.labelRange,
350352
type: "calendar",
351353
background: data.background,
352-
horizontalGroupBy: data.horizontalGroupBy,
353-
verticalGroupBy: data.verticalGroupBy,
354+
horizontalGroupBy: data.horizontalGroupBy ?? "day_of_week",
355+
verticalGroupBy: data.verticalGroupBy ?? "month_number",
354356
legendPosition: data.legendPosition || "top",
355357
colorScale: data.colorScale,
356358
},

0 commit comments

Comments
 (0)