Skip to content

Commit b80664e

Browse files
committed
[REF] charts: dataset with { value, format }
`data` was typed as `any`. This is not a good thing in itself. Moreover, it was only the values without the format. Also typeof is extremely permissive
1 parent 06263d9 commit b80664e

File tree

9 files changed

+210
-112
lines changed

9 files changed

+210
-112
lines changed

packages/o-spreadsheet-engine/src/helpers/cells/cell_evaluation.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,48 @@ function _createEvaluatedCell(
129129
return textCell(value, format, formattedValue);
130130
}
131131

132+
export function isNumberCell(
133+
result: FunctionResultObject | undefined
134+
): result is { value: number } {
135+
return !!result && getEvaluatedCellType(result) === CellValueType.number;
136+
}
137+
138+
export function isTextCell(result: FunctionResultObject | undefined): result is { value: string } {
139+
return !!result && getEvaluatedCellType(result) === CellValueType.text;
140+
}
141+
142+
export function isBooleanCell(
143+
result: FunctionResultObject | undefined
144+
): result is { value: boolean } {
145+
return !!result && getEvaluatedCellType(result) === CellValueType.boolean;
146+
}
147+
148+
export function isEmptyCell(result: FunctionResultObject | undefined): result is { value: null } {
149+
return !!result && getEvaluatedCellType(result) === CellValueType.empty;
150+
}
151+
152+
export function isErrorCell(result: FunctionResultObject | undefined): result is { value: string } {
153+
return !!result && getEvaluatedCellType(result) === CellValueType.error;
154+
}
155+
156+
function getEvaluatedCellType({ value, format }: FunctionResultObject): CellValueType {
157+
if (value === null) {
158+
return CellValueType.empty;
159+
} else if (isEvaluationError(value)) {
160+
return CellValueType.error;
161+
} else if (isTextFormat(format)) {
162+
return CellValueType.text;
163+
}
164+
switch (typeof value) {
165+
case "number":
166+
return CellValueType.number;
167+
case "boolean":
168+
return CellValueType.boolean;
169+
case "string":
170+
return CellValueType.text;
171+
}
172+
}
173+
132174
function textCell(
133175
value: string,
134176
format: string | undefined,

packages/o-spreadsheet-engine/src/plugins/ui_core_views/cell_evaluation/evaluation_plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ export class EvaluationPlugin extends CoreViewPlugin {
259259
/**
260260
* Return the value of each cell in the range.
261261
*/
262-
getRangeValues(range: Range): CellValue[] {
262+
getRangeValues(range: Range): EvaluatedCell[] {
263263
const sheet = this.getters.tryGetSheet(range.sheetId);
264264
if (sheet === undefined) return [];
265-
return this.mapVisiblePositions(range, (p) => this.getters.getEvaluatedCell(p).value);
265+
return this.mapVisiblePositions(range, (p) => this.getters.getEvaluatedCell(p));
266266
}
267267

268268
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Point } from "chart.js";
2-
import { Align, Color, VerticalAlign } from "../misc";
2+
import { Align, Color, FunctionResultObject, VerticalAlign } from "../misc";
33
import { XlsxHexColor } from "../xlsx";
44
import { BarChartDefinition, BarChartRuntime } from "./bar_chart";
55
import { ComboChartDefinition, ComboChartRuntime } from "./combo_chart";
@@ -93,7 +93,7 @@ export interface LabelValues {
9393

9494
export interface DatasetValues {
9595
readonly label?: string;
96-
readonly data: any[];
96+
readonly data: FunctionResultObject[];
9797
readonly hidden?: boolean;
9898
}
9999

src/helpers/figures/charts/pyramid_chart.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CoreGetters, Validator } from "@odoo/o-spreadsheet-engine";
22
import { BACKGROUND_CHART_COLOR } from "@odoo/o-spreadsheet-engine/constants";
3+
import { isNumberCell } from "@odoo/o-spreadsheet-engine/helpers/cells/cell_evaluation";
34
import { AbstractChart } from "@odoo/o-spreadsheet-engine/helpers/figures/charts/abstract_chart";
45
import {
56
chartFontColor,
@@ -197,7 +198,11 @@ export class PyramidChart extends AbstractChart {
197198
const chartData = getPyramidChartData(definition, this.dataSets, this.labelRange, getters);
198199
const { dataSetsValues } = chartData;
199200
const maxValue = Math.max(
200-
...dataSetsValues.map((dataSet) => Math.max(...dataSet.data.map(Math.abs)))
201+
...dataSetsValues.map((dataSet) =>
202+
Math.max(
203+
...dataSet.data.map((cell) => (isNumberCell(cell) ? Math.abs(cell.value) : -Infinity))
204+
)
205+
)
201206
);
202207
return {
203208
...definition,

0 commit comments

Comments
 (0)