Skip to content

Commit aff55de

Browse files
authored
Merge pull request #51 from singerla/feature-chart-helper
Modify chart legend and chart plot area
2 parents f97b116 + 3f12aa5 commit aff55de

File tree

9 files changed

+342
-52
lines changed

9 files changed

+342
-52
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ const automizer = new Automizer({
8080
// Higher compression levels produce smaller files.
8181
compression: 0,
8282

83+
// You can enable 'archiveType' and set mode: 'fs'.
84+
// This will extract all templates and output to disk.
85+
// It will not improve performance, but it can help debugging:
86+
// You don't have to manually extract pptx contents, which can
87+
// be annoying if you need to look inside your files.
88+
// archiveType: {
89+
// mode: 'fs',
90+
// baseDir: `${__dirname}/../__tests__/pptx-cache`,
91+
// workDir: 'tmpWorkDir',
92+
// cleanupWorkDir: true,
93+
// },
94+
8395
// use a callback function to track pptx generation process.
8496
// statusTracker: myStatusTracker,
8597
})
@@ -318,6 +330,8 @@ Take a look into [__tests__-directory](https://github.com/singerla/pptx-automize
318330
* [Use tags inside text to replace contents](https://github.com/singerla/pptx-automizer/blob/main/__tests__/replace-tagged-text.test.ts)
319331
* [Modify vertical line charts](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-chart-vertical-lines.test.ts)
320332
* [Set table cell and border styles](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-existing-table.test.ts)
333+
* [Update chart plot area coordinates](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-chart-plot-area.test.ts)
334+
* [Update chart legend](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-chart-legend.test.ts)
321335

322336

323337
### Testing
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Automizer, { modify } from '../src/index';
2+
import { ChartData } from '../src/types/chart-types';
3+
4+
const data = {
5+
series: [{ label: 'series 1' }, { label: 'series 2' }, { label: 'series 3' }],
6+
categories: [
7+
{ label: 'cat 2-1', values: [50, 50, 20] },
8+
{ label: 'cat 2-2', values: [14, 50, 20] },
9+
{ label: 'cat 2-3', values: [15, 50, 20] },
10+
{ label: 'cat 2-4', values: [26, 50, 20] },
11+
],
12+
};
13+
14+
const dataSmall = {
15+
series: [{ label: 'series 1' }],
16+
categories: [
17+
{ label: 'cat 1-1', values: [50] },
18+
{ label: 'cat 1-2', values: [14] },
19+
],
20+
};
21+
22+
test('Add slide with charts and modify existing chart legend.', async () => {
23+
const automizer = new Automizer({
24+
templateDir: `${__dirname}/pptx-templates`,
25+
outputDir: `${__dirname}/pptx-output`,
26+
});
27+
28+
const pres = automizer
29+
.loadRoot(`RootTemplate.pptx`)
30+
.load(`SlideWithCharts.pptx`, 'charts');
31+
32+
const result = await pres
33+
.addSlide('charts', 2, (slide) => {
34+
slide.modifyElement('ColumnChart', [
35+
modify.setChartData(data),
36+
modify.minimizeChartLegend(),
37+
]);
38+
slide.modifyElement('PieChart', [
39+
modify.setChartData(dataSmall),
40+
modify.removeChartLegend(),
41+
]);
42+
})
43+
.addSlide('charts', 2, (slide) => {
44+
slide.modifyElement('ColumnChart', [
45+
modify.setChartData(data),
46+
modify.setLegendPosition({
47+
w: 0.3,
48+
}),
49+
]);
50+
})
51+
.write(`modify-chart-legend.test.pptx`);
52+
53+
expect(result.charts).toBe(7);
54+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Automizer, { modify } from '../src/index';
2+
import { ChartData } from '../src/types/chart-types';
3+
4+
const data = {
5+
series: [{ label: 'series 1' }, { label: 'series 2' }, { label: 'series 3' }],
6+
categories: [
7+
{ label: 'cat 2-1', values: [50, 50, 20] },
8+
{ label: 'cat 2-2', values: [14, 50, 20] },
9+
{ label: 'cat 2-3', values: [15, 50, 20] },
10+
{ label: 'cat 2-4', values: [26, 50, 20] },
11+
],
12+
};
13+
14+
test('Add slide with charts and modify chart plot area coordinates.', async () => {
15+
const automizer = new Automizer({
16+
templateDir: `${__dirname}/pptx-templates`,
17+
outputDir: `${__dirname}/pptx-output`,
18+
});
19+
20+
const pres = automizer
21+
.loadRoot(`RootTemplate.pptx`)
22+
.load(`SlideWithCharts.pptx`, 'charts');
23+
24+
const result = await pres
25+
.addSlide('charts', 1, (slide) => {
26+
slide.modifyElement('StackedBars', [
27+
modify.setChartData(data),
28+
modify.setPlotArea({
29+
w: 0.5,
30+
h: 0.5,
31+
}),
32+
]);
33+
})
34+
.write(`modify-chart-plot-area.test.pptx`);
35+
36+
expect(result.charts).toBe(2);
37+
});

src/dev.ts

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
import { vd } from './helper/general-helper';
22
import Automizer, { modify } from './index';
33

4-
const outputName = 'create-presentation-file-proxy.test.pptx';
4+
const outputName = 'modify-chart-props.test.pptx';
55
const automizer = new Automizer({
66
templateDir: `${__dirname}/../__tests__/pptx-templates`,
77
outputDir: `${__dirname}/../__tests__/pptx-output`,
8-
// Streaming is only implemented for jszip
8+
// You can enable 'archiveType' and set mode: 'fs'
9+
// This will extract all templates and output to disk.
10+
// It will not improve performance, but it can help debugging:
11+
// You don't have to manually extract pptx contents, which can
12+
// be annoying if you need to look inside your files.
913
// archiveType: {
1014
// mode: 'fs',
1115
// baseDir: `${__dirname}/../__tests__/pptx-cache`,
1216
// workDir: outputName,
1317
// cleanupWorkDir: true,
1418
// },
15-
rootTemplate: 'RootTemplateWithImages.pptx',
16-
presTemplates: [
17-
`RootTemplate.pptx`,
18-
`SlideWithImages.pptx`,
19-
`ChartBarsStacked.pptx`,
20-
],
19+
rootTemplate: 'RootTemplate.pptx',
20+
presTemplates: [`ChartBarsStacked.pptx`],
2121
removeExistingSlides: true,
2222
cleanup: true,
23-
compression: 1,
23+
compression: 0,
2424
});
2525

2626
const run = async () => {
27-
// const pres = automizer
28-
// .loadRoot(`RootTemplateWithImages.pptx`)
29-
// .load(`RootTemplate.pptx`, 'root')
30-
// .load(`SlideWithImages.pptx`, 'images')
31-
// .load(`ChartBarsStacked.pptx`, 'charts');
32-
3327
const dataSmaller = {
3428
series: [{ label: 'series s1' }, { label: 'series s2' }],
3529
categories: [
@@ -38,41 +32,37 @@ const run = async () => {
3832
],
3933
};
4034

41-
const pres = await automizer
35+
const result = await automizer
4236
.addSlide('ChartBarsStacked.pptx', 1, (slide) => {
43-
slide.modifyElement('BarsStacked', [modify.setChartData(dataSmaller)]);
44-
slide.addElement('ChartBarsStacked.pptx', 1, 'BarsStacked', [
45-
modify.setChartData(dataSmaller),
46-
]);
47-
})
48-
.addSlide('SlideWithImages.pptx', 1)
49-
.addSlide('RootTemplate.pptx', 1, (slide) => {
50-
slide.addElement('ChartBarsStacked.pptx', 1, 'BarsStacked', [
51-
modify.setChartData(dataSmaller),
37+
slide.modifyElement('BarsStacked', [
38+
// This needs to be worked out:
39+
modify.setPlotArea({
40+
// Plot area width is a share of chart space.
41+
// We shrink it to 50% of available chart width.
42+
w: 0.4,
43+
h: 0.4,
44+
x: 0.0,
45+
y: 0.0,
46+
}),
47+
// Label area position and dimensions. Automatically sets the label visible.
48+
modify.setLegendPosition({
49+
w: 0.2,
50+
h: 1.0,
51+
x: 1.0,
52+
y: 0.0,
53+
}),
54+
// Hides the label by zeroing label area
55+
modify.minimizeChartLegend(),
56+
// We can as well set chart data to insert our custom values.
57+
// modify.setChartData(dataSmaller),
58+
59+
// Dump the shape:
60+
// modify.dump,
5261
]);
5362
})
54-
.addSlide('ChartBarsStacked.pptx', 1, (slide) => {
55-
slide.addElement('SlideWithImages.pptx', 2, 'imageJPG');
56-
slide.modifyElement('BarsStacked', [modify.setChartData(dataSmaller)]);
57-
})
58-
.addSlide('ChartBarsStacked.pptx', 1, (slide) => {
59-
slide.addElement('SlideWithImages.pptx', 2, 'imageJPG');
60-
slide.modifyElement('BarsStacked', [modify.setChartData(dataSmaller)]);
61-
});
62-
63-
const stream = await pres.stream({
64-
compressionOptions: {
65-
level: 9,
66-
},
67-
});
68-
69-
const jszip = await pres.getJSZip();
70-
const base64 = await jszip.generateAsync({ type: 'base64' });
71-
vd(stream);
72-
73-
// stream.pipe(process.stdout);
63+
.write(outputName);
7464

75-
// vd(pres.rootTemplate.content);
65+
vd('It took ' + result.duration.toPrecision(2) + 's');
7666
};
7767

7868
run().catch((error) => {

0 commit comments

Comments
 (0)