Skip to content

Commit

Permalink
[FIX] chart_extractor: Import dataset colors from xlsx
Browse files Browse the repository at this point in the history
Previously, when exporting an xlsx file, the dataset colors were saved
according to the chart configuration. However, these colors were not
imported when the xlsx file was read. This commit addresses the issue
by ensuring that dataset colors are also imported.

closes #4799

Task: 4092106
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
dhrp-odoo committed Aug 13, 2024
1 parent 4ce633f commit a516421
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/xlsx/conversion/figure_conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function convertChartData(chartData: ExcelChartDefinition): ChartDefinition | un
return {
dataRange: convertExcelRangeToSheetXC(data.range, dataSetsHaveTitle),
label,
backgroundColor: data.backgroundColor,
};
});
// For doughnut charts, in chartJS first dataset = outer dataset, in excel first dataset = inner dataset
Expand Down
7 changes: 7 additions & 0 deletions src/xlsx/extraction/chart_extractor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toHex } from "../../helpers";
import { ExcelChartDataset, ExcelChartDefinition } from "../../types";
import { XLSXChartType, XLSX_CHART_TYPES } from "../../types/xlsx";
import { CHART_TYPE_CONVERSION_MAP, DRAWING_LEGEND_POSITION_CONVERSION_MAP } from "../conversion";
Expand Down Expand Up @@ -127,11 +128,17 @@ export class XlsxChartExtractor extends XlsxBaseExtractor {
label = { text };
}
}
const color = this.extractChildAttr(
chartDataElement,
"c:spPr a:solidFill a:srgbClr",
"val"
);
return {
label,
range: this.extractChildTextContent(chartDataElement, "c:val c:f", {
required: true,
})!,
backgroundColor: color ? `${toHex(color.asString())}` : undefined,
};
}
);
Expand Down
Binary file modified tests/__xlsx__/xlsx_demo_data.xlsx
Binary file not shown.
44 changes: 39 additions & 5 deletions tests/xlsx/xlsx_import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,18 +646,46 @@ describe("Import xlsx data", () => {
expect(figure.tag).toEqual("chart");
});

test.each([
[
"line",
[
{ dataRange: "Sheet1!B26:B35", backgroundColor: "#7030A0" },
{ dataRange: "Sheet1!C26:C35", backgroundColor: "#C65911" },
],
],
[
"bar",
[
{ dataRange: "Sheet1!B27:B35", backgroundColor: "#7030A0" },
{ dataRange: "Sheet1!C27:C35", backgroundColor: "#C65911" },
],
],
])("Can import charts %s with dataset colors", (chartType, chartDatasets) => {
const testSheet = getWorkbookSheet("jestCharts", convertedData)!;
const figure = testSheet.figures.find((figure) => figure.data.type === chartType);
const chartData = figure!.data as LineChartDefinition | BarChartDefinition;
expect(chartData.dataSets).toEqual(chartDatasets);
});

test.each([
[
"bar chart",
"bar",
"#fff",
[{ dataRange: "Sheet1!B27:B35" }, { dataRange: "Sheet1!C27:C35" }],
[
{ dataRange: "Sheet1!B27:B35", backgroundColor: "#7030A0" },
{ dataRange: "Sheet1!C27:C35", backgroundColor: "#C65911" },
],
],
[
"combo chart",
"combo",
"#fff",
[{ dataRange: "Sheet1!B27:B35" }, { dataRange: "Sheet1!C27:C35" }],
[
{ dataRange: "Sheet1!B27:B35", backgroundColor: "#1F77B4" },
{ dataRange: "Sheet1!C27:C35", backgroundColor: "#FF7F0E" },
],
],
])(
"Can import charts %s without dataset titles",
Expand All @@ -680,14 +708,20 @@ describe("Import xlsx data", () => {
"line chart",
"line",
"#CECECE",
[{ dataRange: "Sheet1!B26:B35" }, { dataRange: "Sheet1!C26:C35" }],
[
{ dataRange: "Sheet1!B26:B35", backgroundColor: "#7030A0" },
{ dataRange: "Sheet1!C26:C35", backgroundColor: "#C65911" },
],
],
["pie chart", "pie", "#fff", [{ dataRange: "Sheet1!B26:B35" }]],
["pie chart", "pie", "#fff", [{ dataRange: "Sheet1!B26:B35", backgroundColor: "#1F77B4" }]],
[
"doughnut chart",
"pie",
"#fff",
[{ dataRange: "Sheet1!B26:B35" }, { dataRange: "Sheet1!C26:C35" }],
[
{ dataRange: "Sheet1!B26:B35", backgroundColor: "#1F77B4" },
{ dataRange: "Sheet1!C26:C35", backgroundColor: "#1F77B4" },
],
],
])(
"Can import charts %s with dataset titles",
Expand Down

0 comments on commit a516421

Please sign in to comment.