Skip to content

Commit acc370e

Browse files
committed
[FIX] Charts: fix color scale migration
in PR #6773, we changed the color scale definition from a theme string tot an object that allows custom scales. However, the migration assumed the new scale to be a triplet of strings (probably the remnants of a previous implementation idea). Furthermore, the migration did not account for charts sitting inside a carousel and not a standard figure. Task: 5363581
1 parent 98cf4fb commit acc370e

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

packages/o-spreadsheet-engine/src/migrations/migration_steps.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { BACKGROUND_CHART_COLOR, FORMULA_REF_IDENTIFIER } from "../constants";
2-
import { COLORSCHEMES } from "../helpers/color";
32
import { toXC } from "../helpers/coordinates";
43
import { getItemId } from "../helpers/data_normalization";
54
import { getUniqueText, sanitizeSheetName } from "../helpers/misc";
65
import { getMaxObjectId } from "../helpers/pivot/pivot_helpers";
76
import { DEFAULT_TABLE_CONFIG } from "../helpers/table_presets";
87
import { overlap, toZone, zoneToXc } from "../helpers/zones";
98
import { Registry } from "../registry";
10-
import { CustomizedDataSet } from "../types/chart";
9+
import { CustomizedDataSet, schemeToColorScale } from "../types/chart";
1110
import { Format } from "../types/format";
1211
import { DEFAULT_LOCALE } from "../types/locale";
1312
import { Zone } from "../types/misc";
@@ -561,7 +560,16 @@ migrationStepRegistry
561560
for (const figure of sheet.figures || []) {
562561
if (figure.tag === "chart" && figure.data.type === "geo") {
563562
if ("colorScale" in figure.data && typeof figure.data.colorScale === "string") {
564-
figure.data.colorScale = COLORSCHEMES[figure.data.colorScale];
563+
figure.data.colorScale = schemeToColorScale(figure.data.colorScale);
564+
}
565+
}
566+
if (figure.tag === "carousel") {
567+
for (const definition of Object.values<any>(figure.data.chartDefinitions) || []) {
568+
if (definition.type === "geo") {
569+
if ("colorScale" in definition && typeof definition.colorScale === "string") {
570+
definition.colorScale = schemeToColorScale(figure.data.colorScale);
571+
}
572+
}
565573
}
566574
}
567575
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ import { HighlightStore } from "./stores/highlight_store";
215215
import { ModelStore } from "./stores/model_store";
216216
import { NotificationStore } from "./stores/notification_store";
217217
import { RendererStore } from "./stores/renderer_store";
218-
import { AddFunctionDescription, isMatrix } from "./types";
218+
import { AddFunctionDescription, isMatrix, schemeToColorScale } from "./types";
219219

220220
/**
221221
* We export here all entities that needs to be accessed publicly by Odoo.
@@ -400,6 +400,7 @@ export const helpers = {
400400
isNumber,
401401
isDateTime,
402402
createCustomFields,
403+
schemeToColorScale,
403404
};
404405

405406
export const links = {

tests/model/model_import_export.test.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import {
88
import { DEFAULT_TABLE_CONFIG } from "@odoo/o-spreadsheet-engine/helpers/table_presets";
99
import { getCurrentVersion } from "@odoo/o-spreadsheet-engine/migrations/data";
1010
import { CellIsRule, Model } from "../../src";
11-
import { COLORSCHEMES, toCartesian, toZone } from "../../src/helpers";
11+
import { toCartesian, toZone } from "../../src/helpers";
1212
import {
1313
BorderDescr,
1414
ColorScaleRule,
1515
DEFAULT_LOCALE,
1616
DEFAULT_LOCALES,
1717
IconSetRule,
18+
schemeToColorScale,
1819
} from "../../src/types";
1920
import {
2021
activateSheet,
@@ -32,6 +33,7 @@ import {
3233
getEvaluatedCell,
3334
getMerges,
3435
} from "../test_helpers/getters_helpers";
36+
import { mockGeoJsonService } from "../test_helpers/helpers";
3537

3638
describe("data", () => {
3739
test("give default col size if not specified", () => {
@@ -781,24 +783,48 @@ test("migrate version 18.5.1: chartId is added to figure data", () => {
781783
expect(model.exportData().sheets[0].figures[0].data.chartId).toBe("someuuid");
782784
});
783785

784-
test("migrate version 19.1.0: colorScale is changed to a trio of color", () => {
786+
test("migrate version 19.1.0: colorScale is changed to a colorScale", () => {
787+
const getChartDefinition = (chartId) => ({
788+
chartId,
789+
type: "geo",
790+
colorScale: "reds",
791+
labelRange: "",
792+
dataSets: [],
793+
legendPosition: "top",
794+
title: { text: "Demo Geo Chart" },
795+
});
796+
const sheetId = "sh1";
785797
const data = {
786798
version: "18.5.1",
787799
sheets: [
788800
{
789-
id: "sh1",
801+
id: sheetId,
790802
figures: [
791803
{
792804
id: "someuuid",
793805
tag: "chart",
794-
data: { type: "geo", colorScale: "reds", labelRange: "", dataSets: [] },
806+
data: getChartDefinition("chartId1"),
807+
},
808+
{
809+
id: "someuuid",
810+
tag: "carousel",
811+
data: {
812+
chartDefinitions: { chartId2: getChartDefinition("chartId2") },
813+
},
795814
},
796815
],
797816
},
798817
],
799818
};
800-
const model = new Model(data);
801-
expect(model.exportData().sheets[0].figures[0].data.colorScale).toEqual(COLORSCHEMES.reds);
819+
const model = new Model(data, { external: { geoJsonService: mockGeoJsonService } });
820+
expect(model.exportData().sheets[0].figures[0].data.colorScale).toEqual(
821+
schemeToColorScale("reds")
822+
);
823+
824+
// check runtime as well for safety as geo charts depend on geo service to
825+
// build their runtime and use their color scale
826+
const chartIds = model.getters.getChartIds(sheetId);
827+
expect(model.getters.getChartRuntime(chartIds[0])).toBeDefined();
802828
});
803829

804830
describe("Import", () => {

0 commit comments

Comments
 (0)