Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const CAROUSEL_DEFAULT_CHART_DEFINITION: ChartDefinition = {
title: {},
stacked: false,
dataSetsHaveTitle: false,
dataSets: [],
dataSets: {},
dataSource: { dataSets: [] },
legendPosition: "top",
humanize: true,
};
Expand Down
42 changes: 42 additions & 0 deletions packages/o-spreadsheet-engine/src/helpers/cells/cell_evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,48 @@ function _createEvaluatedCell(
return textCell(value, format, formattedValue);
}

export function isNumberCell(
result: FunctionResultObject | undefined
): result is { value: number } {
return !!result && getEvaluatedCellType(result) === CellValueType.number;
}

export function isTextCell(result: FunctionResultObject | undefined): result is { value: string } {
return !!result && getEvaluatedCellType(result) === CellValueType.text;
}

export function isBooleanCell(
result: FunctionResultObject | undefined
): result is { value: boolean } {
return !!result && getEvaluatedCellType(result) === CellValueType.boolean;
}

export function isEmptyCell(result: FunctionResultObject | undefined): result is { value: null } {
return !!result && getEvaluatedCellType(result) === CellValueType.empty;
}

export function isErrorCell(result: FunctionResultObject | undefined): result is { value: string } {
return !!result && getEvaluatedCellType(result) === CellValueType.error;
}

function getEvaluatedCellType({ value, format }: FunctionResultObject): CellValueType {
if (value === null) {
return CellValueType.empty;
} else if (isEvaluationError(value)) {
return CellValueType.error;
} else if (isTextFormat(format)) {
return CellValueType.text;
}
switch (typeof value) {
case "number":
return CellValueType.number;
case "boolean":
return CellValueType.boolean;
case "string":
return CellValueType.text;
}
}

function textCell(
value: string,
format: string | undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ChartCreationContext,
ChartDefinition,
ChartType,
ChartWithDataSetDefinition,
DataSet,
ExcelChartDataset,
ExcelChartDefinition,
Expand All @@ -13,7 +14,8 @@ import { CellErrorType } from "../../../types/errors";
import { AdaptSheetName, ApplyRangeChange, RangeAdapter, UID } from "../../../types/misc";
import { Range } from "../../../types/range";
import { Validator } from "../../../types/validator";
import { toExcelDataset, toExcelLabelRange } from "./chart_common";
import { getZoneArea } from "../../zones";
import { shouldRemoveFirstLabel, toExcelDataset, toExcelLabelRange } from "./chart_common";

/**
* AbstractChart is the class from which every Chart should inherit.
Expand All @@ -24,13 +26,25 @@ export abstract class AbstractChart {
readonly title: TitleDesign;
abstract readonly type: ChartType;
protected readonly getters: CoreGetters;
readonly humanize: boolean;
readonly humanize: boolean | undefined;

static commonKeys: readonly (keyof ChartDefinition)[] = [
"type",
"title",
"background",
"humanize",
];
static dataSetKeys: readonly (keyof ChartWithDataSetDefinition)[] = [
"dataSets",
"dataSetsHaveTitle",
"labelRange",
];

constructor(definition: ChartDefinition, sheetId: UID, getters: CoreGetters) {
this.title = definition.title;
this.sheetId = sheetId;
this.getters = getters;
this.humanize = definition.humanize ?? true;
this.humanize = definition.humanize;
}

/**
Expand Down Expand Up @@ -101,15 +115,19 @@ export abstract class AbstractChart {
*/
abstract getContextCreation(): ChartCreationContext;

protected getCommonDataSetAttributesForExcel(
labelRange: Range | undefined,
dataSets: DataSet[],
shouldRemoveFirstLabel: boolean
) {
protected getCommonDataSetAttributesForExcel(labelRange: Range | undefined, dataSets: DataSet[]) {
const excelDataSets: ExcelChartDataset[] = dataSets
.map((ds: DataSet) => toExcelDataset(this.getters, ds))
.filter((ds) => ds.range !== "" && ds.range !== CellErrorType.InvalidReference);
const excelLabelRange = toExcelLabelRange(this.getters, labelRange, shouldRemoveFirstLabel);
const definition = this.getDefinition();
const datasetLength = dataSets[0] ? getZoneArea(dataSets[0].dataRange.zone) : undefined;
const labelLength = labelRange ? getZoneArea(labelRange.zone) : 0;
const _shouldRemoveFirstLabel = shouldRemoveFirstLabel(
labelLength,
datasetLength,
"dataSetsHaveTitle" in definition ? definition.dataSetsHaveTitle : false
);
const excelLabelRange = toExcelLabelRange(this.getters, labelRange, _shouldRemoveFirstLabel);
return {
dataSets: excelDataSets,
labelRange: excelLabelRange,
Expand Down
Loading