Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Datalabel Customization #99

Merged
merged 2 commits into from
Mar 25, 2024
Merged
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
34 changes: 34 additions & 0 deletions __tests__/modify-chart-datalabels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Automizer, { LabelPosition, modify } from '../src/index';

test('modify chart data label.', async () => {
const automizer = new Automizer({
templateDir: `${__dirname}/pptx-templates`,
outputDir: `${__dirname}/pptx-output`,
});

const pres = automizer
.loadRoot(`RootTemplate.pptx`)
.load(`ChartBarsStackedLabels.pptx`, 'charts');


const DataLabelAttributes = {
dLblPos: LabelPosition.Top,
showLegendKey: false,
showVal: false,
showCatName: true,
showSerName: false,
showPercent: false,
showBubbleSize: false,
showLeaderLines: false
}

const result = await pres
.addSlide('charts', 1, (slide) => {
slide.modifyElement('BarsStacked', [
modify.setDataLabelAttributes(DataLabelAttributes),
]);
})
.write(`modify-chart-datalabels.test.pptx`);

expect(result.charts).toBe(2);
});
2 changes: 1 addition & 1 deletion __tests__/modify-chart-title.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ test('modify chart title.', async () => {
})
.write(`modify-chart-title.test.pptx`);

expect(result.charts).toBe(1);
expect(result.charts).toBe(2);
});
6 changes: 3 additions & 3 deletions __tests__/modify-master-external-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ test('Load external media, modify image target on slide master', async () => {
const automizer = new Automizer({
templateDir: `${__dirname}/../__tests__/pptx-templates`,
outputDir: `${__dirname}/../__tests__/pptx-output`,
mediaDir: `${__dirname}/../__tests__/images`,
mediaDir: `${__dirname}/../__tests__/media`,
removeExistingSlides: true,
cleanup: true,
});

const pres = automizer
.loadRoot(`RootTemplateWithImages.pptx`)
.loadMedia([`test.jpg`])
.loadMedia([`test.png`])
.load(`RootTemplateWithImages.pptx`, 'base');

pres.addMaster('base', 1, (master) => {
master.modifyElement('masterImagePNG', [
ModifyImageHelper.setRelationTarget('test.jpg'),
ModifyImageHelper.setRelationTarget('test.png'),
]);
});

Expand Down
Binary file not shown.
11 changes: 11 additions & 0 deletions src/enums/chart-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum LabelPosition {
BestFit = 'bestFit',
Bottom = 'b',
Center = 'ctr',
InsideEnd = 'inEnd',
InsideBase = 'inBase',
Left = 'l',
OutsideEnd = 'outEnd',
Right = 'r',
Top = 't',
}
63 changes: 63 additions & 0 deletions src/helper/modify-chart-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ChartCategory,
ChartData,
ChartElementCoordinateShares,
ChartDataLabelAttributes,
ChartPoint,
ChartSeries,
ChartSlot,
Expand Down Expand Up @@ -446,4 +447,66 @@ export default class ModifyChartHelper {
chart.getElementsByTagName('c:title')[0].getElementsByTagName('a:t')[0].textContent = newTitle;
}
};

/**
* Specify a format for DataLabels
@param dataLabel
*
*/
static setDataLabelAttributes =
(dataLabel: ChartDataLabelAttributes): ChartModificationCallback =>
(element: XmlElement, chart: XmlDocument): void => {
const modifyXmlHelper = new ModifyXmlHelper(chart);

modifyXmlHelper.modifyAll({
'c:ser': {
children: {
'c:dLbls': {
children: {
'c:dLblPos': {
modify: [
ModifyXmlHelper.attribute('val', dataLabel.dLblPos),
],
},
'c:showLegendKey': {
modify: [
ModifyXmlHelper.booleanAttribute('val', dataLabel.showLegendKey),
],
},
'c:showVal': {
modify: [
ModifyXmlHelper.booleanAttribute('val', dataLabel.showVal),
],
},
'c:showCatName': {
modify: [
ModifyXmlHelper.booleanAttribute('val', dataLabel.showCatName),
],
},
'c:showSerName': {
modify: [
ModifyXmlHelper.booleanAttribute('val', dataLabel.showSerName),
],
},
'c:showPercent': {
modify: [
ModifyXmlHelper.booleanAttribute('val', dataLabel.showPercent),
],
},
'c:showBubbleSize': {
modify: [
ModifyXmlHelper.booleanAttribute('val', dataLabel.showBubbleSize),
],
},
'c:showLeaderLines': {
modify: [
ModifyXmlHelper.booleanAttribute('val', dataLabel.showLeaderLines),
],
},
},
},
},
},
});
};
}
17 changes: 17 additions & 0 deletions src/helper/modify-xml-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ export default class ModifyXmlHelper {
}
}

modifyAll(tags: ModificationTags, root?: XmlDocument | XmlElement): void {
root = root || this.root;

for (const tag in tags) {
const modifier = tags[tag] as Modification;

if (modifier.children) {
const elements = Array.from(root.getElementsByTagName(tag));
elements.forEach((element) => {
this.modifyAll(modifier.children, element as XmlElement);
});
} else {
this.modify({ [tag]: modifier }, root);
}
}
}

assertElement(
collection: HTMLCollectionOf<Element>,
index: number,
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { IMaster } from './interfaces/imaster';
import { ISlide } from './interfaces/islide';
import ModifyImageHelper from './helper/modify-image-helper';
import { CmToDxa, DxaToCm } from './helper/modify-helper';
import { LabelPosition } from './enums/chart-type';

const dump = ModifyHelper.dump;
const dumpChart = ModifyHelper.dumpChart;
Expand Down Expand Up @@ -73,6 +74,7 @@ const setLegendPosition = ModifyChartHelper.setLegendPosition;
const removeChartLegend = ModifyChartHelper.removeChartLegend;
const minimizeChartLegend = ModifyChartHelper.minimizeChartLegend;
const setChartTitle = ModifyChartHelper.setChartTitle;
const setDataLabelAttributes = ModifyChartHelper.setDataLabelAttributes;

export type {
ChartBubble,
Expand All @@ -90,7 +92,7 @@ export type {
Border,
ShapeValueStyle,
ShapeCoordinates,
ReplaceText,
ReplaceText
};

export {
Expand All @@ -103,6 +105,7 @@ export {
ModifyTextHelper,
ModifyColorHelper,
ModifyImageHelper,
LabelPosition
};

export const modify = {
Expand Down Expand Up @@ -133,6 +136,7 @@ export const modify = {
removeChartLegend,
minimizeChartLegend,
setChartTitle,
setDataLabelAttributes,
};
export { CmToDxa, DxaToCm };
export type {
Expand Down
12 changes: 12 additions & 0 deletions src/types/chart-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Color, ModificationTags } from './modify-types';
import { ShapeCoordinates } from './shape-types';
import { LabelPosition } from '../enums/chart-type';

export type ChartPointValue = null | number;
export type ChartValueStyle = {
Expand Down Expand Up @@ -84,6 +85,17 @@ export type ChartAxisRange = {
formatCode?: string;
sourceLinked?: boolean;
};

export type ChartDataLabelAttributes = {
dLblPos?: LabelPosition;
showLegendKey?: boolean;
showVal?: boolean;
showCatName?: boolean;
showSerName?: boolean;
showPercent?: boolean;
showBubbleSize?: boolean;
showLeaderLines?: boolean;
};
// Elements inside a chart (e.g. a legend) require shares as coordinates.
// E.g. "w: 0.5" means "half of chart width"
export type ChartElementCoordinateShares = ShapeCoordinates;