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
1 change: 1 addition & 0 deletions packages/o-spreadsheet-engine/src/types/chart/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export interface ExcelChartDefinition {
useRightAxis?: boolean;
};
readonly axesDesign?: AxesDesign;
readonly showValues?: boolean;
readonly horizontal?: boolean;
readonly isDoughnut?: boolean;
readonly pieHolePercentage?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function convertChartData(chartData: ExcelChartDefinition): ChartDefinition | un
horizontal: chartData.horizontal,
isDoughnut: chartData.isDoughnut,
pieHolePercentage: chartData.pieHolePercentage,
showValues: chartData.showValues,
};
try {
const ChartClass = chartRegistry.get(chartData.type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export class XlsxChartExtractor extends XlsxBaseExtractor {
const chartHoleSize = this.extractChildAttr(rootChartElement, "c:holeSize", "val", {
default: "0",
}).asNum();

const showValueNodes = this.querySelectorAll(rootChartElement, "c:chart c:showVal");
const showValues = [...showValueNodes].some(
(el) => el.attributes.getNamedItem("val")?.value === "1"
);
return {
title: { text: chartTitle },
type: CHART_TYPE_CONVERSION_MAP[chartType]!,
Expand Down Expand Up @@ -68,6 +71,7 @@ export class XlsxChartExtractor extends XlsxBaseExtractor {
horizontal: chartDirection === "bar",
isDoughnut: chartHoleSize > 0,
pieHolePercentage: chartHoleSize,
showValues,
};
}
)[0];
Expand All @@ -94,7 +98,10 @@ export class XlsxChartExtractor extends XlsxBaseExtractor {
const barChartGrouping = this.extractChildAttr(chartElement, "c:grouping", "val", {
default: "clustered",
}).asString();

const showValueNodes = this.querySelectorAll(chartElement, "c:chart c:showVal");
const showValues = [...showValueNodes].some(
(el) => el.attributes.getNamedItem("val")?.value === "1"
);
return {
title: { text: chartTitle },
type: "combo",
Expand Down Expand Up @@ -125,6 +132,7 @@ export class XlsxChartExtractor extends XlsxBaseExtractor {
],
stacked: barChartGrouping === "stacked",
fontColor: "000000",
showValues,
};
}

Expand Down
36 changes: 30 additions & 6 deletions packages/o-spreadsheet-engine/src/xlsx/functions/charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ function addBarChart(chart: ExcelChartDefinition): XMLString {
<c:order val="${dsIndex}"/>
${extractTrendline(dataset.trend, color)}
${extractDataSetLabel(dataset.label)}
${insertDataLabels({ showValues: chart.showValues })}
${dataShapeProperty}
${
chart.labelRange ? escapeXml/*xml*/ `<c:cat>${stringRef(chart.labelRange!)}</c:cat>` : ""
Expand Down Expand Up @@ -358,6 +359,7 @@ function addBarChart(chart: ExcelChartDefinition): XMLString {
<!-- each data marker in the series does not have a different color -->
<c:varyColors val="0"/>
${joinXmlNodes(leftDataSetsNodes)}
${insertDataLabels({ showValues: chart.showValues })}
<c:axId val="${catAxId}" />
<c:axId val="${valAxId}" />
</c:barChart>
Expand Down Expand Up @@ -448,6 +450,7 @@ function addComboChart(chart: ExcelChartDefinition): XMLString {
<c:order val="0"/>
${extractTrendline(dataSet.trend, firstColor)}
${extractDataSetLabel(dataSet.label)}
${insertDataLabels({ showValues: chart.showValues })}
${shapeProperty({
backgroundColor: firstColor,
line: { color: firstColor },
Expand Down Expand Up @@ -481,6 +484,7 @@ function addComboChart(chart: ExcelChartDefinition): XMLString {
</c:marker>
${extractTrendline(dataSet.trend, color)}
${extractDataSetLabel(dataSet.label)}
${insertDataLabels({ showValues: chart.showValues })}
${dataShapeProperty}
${chart.labelRange ? escapeXml`<c:cat>${stringRef(chart.labelRange)}</c:cat>` : ""}
<!-- x-coordinate values -->
Expand All @@ -506,6 +510,7 @@ function addComboChart(chart: ExcelChartDefinition): XMLString {
<!-- each data marker in the series does not have a different color -->
<c:varyColors val="0"/>
${barDataSetNode}
${insertDataLabels({ showValues: chart.showValues })}
<c:axId val="${useRightAxisForBarSerie ? secondaryCatAxId : catAxId}" />
<c:axId val="${useRightAxisForBarSerie ? secondaryValAxId : valAxId}" />
</c:barChart>
Expand All @@ -516,6 +521,7 @@ function addComboChart(chart: ExcelChartDefinition): XMLString {
<c:grouping val="standard"/>
<!-- each data marker in the series does not have a different color -->
<c:varyColors val="0"/>
${insertDataLabels({ showValues: chart.showValues })}
${joinXmlNodes(leftDataSetsNodes)}
<c:axId val="${catAxId}" />
<c:axId val="${valAxId}" />
Expand All @@ -530,6 +536,7 @@ function addComboChart(chart: ExcelChartDefinition): XMLString {
<c:grouping val="standard"/>
<!-- each data marker in the series does not have a different color -->
<c:varyColors val="0"/>
${insertDataLabels({ showValues: chart.showValues })}
${joinXmlNodes(rightDataSetsNodes)}
<c:axId val="${secondaryCatAxId}" />
<c:axId val="${secondaryValAxId}" />
Expand Down Expand Up @@ -589,6 +596,7 @@ function addPyramidChart(chart: ExcelChartDefinition): XMLString {
<c:order val="0"/>
<c:invertIfNegative val="0" />
${extractDataSetLabel(leftDataSet.label)}
${insertDataLabels({ showValues: chart.showValues })}
${shapeProperty({
backgroundColor: firstColor,
line: { color: firstColor },
Expand All @@ -606,6 +614,7 @@ function addPyramidChart(chart: ExcelChartDefinition): XMLString {
<c:order val="1"/>
<c:invertIfNegative val="0" />
${extractDataSetLabel(rightDataSet.label)}
${insertDataLabels({ showValues: chart.showValues })}
${shapeProperty({
backgroundColor: secondColor,
line: { color: secondColor },
Expand All @@ -623,6 +632,7 @@ function addPyramidChart(chart: ExcelChartDefinition): XMLString {
<c:grouping val="clustered"/>
<c:varyColors val="0" />
${leftBarDataSetNode}
${insertDataLabels({ showValues: chart.showValues })}
<c:gapWidth val="50" />
<c:axId val="${catAxId}" />
<c:axId val="${valAxId}" />
Expand All @@ -632,6 +642,7 @@ function addPyramidChart(chart: ExcelChartDefinition): XMLString {
<c:grouping val="clustered"/>
<c:varyColors val="0" />
${rightBarDataSetNode}
${insertDataLabels({ showValues: chart.showValues })}
<c:gapWidth val="50" />
<c:axId val="${secondaryCatAxId}" />
<c:axId val="${secondaryValAxId}" />
Expand Down Expand Up @@ -728,6 +739,7 @@ function addLineChart(chart: ExcelChartDefinition): XMLString {
</c:marker>
${extractTrendline(dataset.trend, color)}
${extractDataSetLabel(dataset.label)}
${insertDataLabels({ showValues: chart.showValues })}
${dataShapeProperty}
${
chart.labelRange ? escapeXml`<c:cat>${stringRef(chart.labelRange!)}</c:cat>` : ""
Expand Down Expand Up @@ -755,6 +767,7 @@ function addLineChart(chart: ExcelChartDefinition): XMLString {
<!-- each data marker in the series does not have a different color -->
<c:varyColors val="0"/>
${joinXmlNodes(leftDataSetsNodes)}
${insertDataLabels({ showValues: chart.showValues })}
<c:axId val="${catAxId}" />
<c:axId val="${valAxId}" />
</c:lineChart>
Expand All @@ -771,6 +784,7 @@ function addLineChart(chart: ExcelChartDefinition): XMLString {
<!-- each data marker in the series does not have a different color -->
<c:varyColors val="0"/>
${joinXmlNodes(rightDataSetsNodes)}
${insertDataLabels({ showValues: chart.showValues })}
<c:axId val="${catAxId + 1}" />
<c:axId val="${valAxId + 1}" />
</c:lineChart>
Expand Down Expand Up @@ -823,6 +837,7 @@ function addScatterChart(chart: ExcelChartDefinition): XMLString {
</c:marker>
${extractTrendline(dataset.trend, color)}
${extractDataSetLabel(dataset.label)}
${insertDataLabels({ showValues: chart.showValues })}
${
chart.labelRange
? escapeXml/*xml*/ `<c:xVal> <!-- x-coordinate values -->
Expand Down Expand Up @@ -850,6 +865,7 @@ function addScatterChart(chart: ExcelChartDefinition): XMLString {
<c:varyColors val="0"/>
<c:scatterStyle val="lineMarker"/>
${joinXmlNodes(leftDataSetsNodes)}
${insertDataLabels({ showValues: chart.showValues })}
<c:axId val="${catAxId}" />
<c:axId val="${valAxId}" />
</c:scatterChart>
Expand All @@ -866,6 +882,7 @@ function addScatterChart(chart: ExcelChartDefinition): XMLString {
<c:varyColors val="0"/>
<c:scatterStyle val="lineMarker"/>
${joinXmlNodes(rightDataSetsNodes)}
${insertDataLabels({ showValues: chart.showValues })}
<c:axId val="${catAxId + 1}" />
<c:axId val="${valAxId + 1}" />
</c:scatterChart>
Expand Down Expand Up @@ -916,6 +933,7 @@ function addRadarChart(chart: ExcelChartDefinition): XMLString {
${shapeProperty({ backgroundColor: color, line: { color } })}
</c:marker>
${extractDataSetLabel(dataset.label)}
${insertDataLabels({ showValues: chart.showValues })}
${dataShapeProperty}
${
chart.labelRange ? escapeXml`<c:cat>${stringRef(chart.labelRange!)}</c:cat>` : ""
Expand All @@ -934,6 +952,7 @@ function addRadarChart(chart: ExcelChartDefinition): XMLString {
<c:radarStyle val="marker"/>
<c:varyColors val="0"/>
${joinXmlNodes(dataSetsNodes)}
${insertDataLabels({ showValues: chart.showValues })}
<c:axId val="${catAxId}" />
<c:axId val="${valAxId}" />
</c:radarChart>
Expand Down Expand Up @@ -978,7 +997,7 @@ function addDoughnutChart(
<c:order val="${dsIndex}"/>
${extractDataSetLabel(dataset.label)}
${joinXmlNodes(dataPoints)}
${insertDataLabels({ showLeaderLines: true })}
${insertDataLabels({ showLeaderLines: true, showValues: chart.showValues })}
${chart.labelRange ? escapeXml`<c:cat>${stringRef(chart.labelRange!)}</c:cat>` : ""}
<c:val>
${numberRef(dataset.range)}
Expand All @@ -992,23 +1011,28 @@ function addDoughnutChart(
<c:holeSize val="${
chart.pieHolePercentage ?? (chart.isDoughnut ? DEFAULT_DOUGHNUT_CHART_HOLE_SIZE : 0)
}" />
${insertDataLabels()}
${joinXmlNodes(dataSetsNodes)}
${insertDataLabels({ showValues: chart.showValues })}
</c:doughnutChart>
`;
}

function insertDataLabels({ showLeaderLines } = { showLeaderLines: false }): XMLString {
function insertDataLabels(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it was done this way before but I'm not sure we should pass and object here for only two arguments

{ showLeaderLines, showValues }: { showLeaderLines?: Boolean; showValues?: Boolean } = {
showLeaderLines: false,
showValues: false,
}
): XMLString {
return escapeXml/*xml*/ `
<dLbls>
<c:dLbls>
<c:showLegendKey val="0"/>
<c:showVal val="0"/>
<c:showVal val="${showValues ? "1" : "0"}"/>
<c:showCatName val="0"/>
<c:showSerName val="0"/>
<c:showPercent val="0"/>
<c:showBubbleSize val="0"/>
<c:showLeaderLines val="${showLeaderLines ? "1" : "0"}"/>
</dLbls>
</c:dLbls>
`;
}

Expand Down
Binary file modified tests/__xlsx__/xlsx_demo_data.xlsx
Binary file not shown.
Loading