Skip to content

Commit 378e3de

Browse files
committed
fix(chart): added element as first setAxisRange argument + test
1 parent 6ab4a3f commit 378e3de

File tree

3 files changed

+112
-4
lines changed

3 files changed

+112
-4
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import Automizer, { modify } from '../src/index';
2+
import { ChartData } from '../src/types/chart-types';
3+
4+
test('modify chart axis.', async () => {
5+
const automizer = new Automizer({
6+
templateDir: `${__dirname}/pptx-templates`,
7+
outputDir: `${__dirname}/pptx-output`,
8+
});
9+
10+
const pres = automizer
11+
.loadRoot(`RootTemplate.pptx`)
12+
.load(`ChartAxis.pptx`, 'charts');
13+
14+
const dataScatter = <ChartData>(<unknown>{
15+
series: [
16+
{ label: 'series s1' },
17+
{ label: 'series s2' },
18+
{ label: 'series s3' },
19+
],
20+
categories: [
21+
{
22+
label: 'r1',
23+
values: [
24+
{ x: 10, y: 20 },
25+
{ x: 9, y: 30 },
26+
{ x: 19, y: 40 },
27+
],
28+
},
29+
{
30+
label: 'r2',
31+
values: [
32+
{ x: 21, y: 11 },
33+
{ x: 8, y: 31 },
34+
{ x: 18, y: 41 },
35+
],
36+
},
37+
{
38+
label: 'r3',
39+
values: [
40+
{ x: 22, y: 28 },
41+
{ x: 7, y: 26 },
42+
{ x: 17, y: 36 },
43+
],
44+
},
45+
{
46+
label: 'r4',
47+
values: [
48+
{ x: 13, y: 13 },
49+
{ x: 16, y: 28 },
50+
{ x: 26, y: 38 },
51+
],
52+
},
53+
{
54+
label: 'r5',
55+
values: [
56+
{ x: 18, y: 24 },
57+
{ x: 15, y: 24 },
58+
{ x: 25, y: 34 },
59+
],
60+
},
61+
{
62+
label: 'r6',
63+
values: [
64+
{ x: 28, y: 34 },
65+
{ x: 25, y: 34 },
66+
{ x: 35, y: 44 },
67+
],
68+
},
69+
],
70+
});
71+
72+
const result = await pres
73+
.addSlide('charts', 1, (slide) => {
74+
slide.modifyElement('Scatter', [
75+
modify.setChartScatter(dataScatter),
76+
77+
/**
78+
* Please notice: It will only work if the value to update is not set to
79+
* "Auto" in powerpoint. Only manually scaled min/max can be altered by this.
80+
*
81+
* min: 10 will not take any effect, while max: 30 does.
82+
*/
83+
modify.setAxisRange({
84+
axisIndex: 0,
85+
min: 10,
86+
max: 30,
87+
}),
88+
89+
modify.setAxisRange({
90+
axisIndex: 1,
91+
min: 1,
92+
max: 100,
93+
}),
94+
]);
95+
})
96+
.write(`modify-chart-axis.test.pptx`);
97+
98+
expect(result.charts).toBe(2);
99+
});
48.6 KB
Binary file not shown.

src/helper/modify-chart-helper.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ModifyChart } from '../modify/modify-chart';
2-
import { Workbook } from '../types/types';
2+
import { ShapeModificationCallback, Workbook } from '../types/types';
33
import {
44
ChartAxisRange,
55
ChartBubble,
@@ -13,6 +13,7 @@ import {
1313
import ModifyXmlHelper from './modify-xml-helper';
1414
import { XmlDocument, XmlElement } from '../types/xml-types';
1515
import { XmlHelper } from './xml-helper';
16+
import { vd } from './general-helper';
1617

1718
export default class ModifyChartHelper {
1819
/**
@@ -160,7 +161,7 @@ export default class ModifyChartHelper {
160161
axisIndex: 1,
161162
min: 0,
162163
max: data.categories.length,
163-
})(chart);
164+
})(element, chart);
164165
};
165166

166167
/**
@@ -240,9 +241,15 @@ export default class ModifyChartHelper {
240241
// XmlHelper.dump(workbook.table)
241242
};
242243

244+
/**
245+
* Set range and format for chart axis.
246+
* Please notice: It will only work if the value to update is not set to
247+
* "Auto" in powerpoint. Only manually scaled min/max can be altered by this.
248+
* See `__tests__/modify-chart-axis.test.js`
249+
*/
243250
static setAxisRange =
244-
(range: ChartAxisRange) =>
245-
(chart: XmlDocument): void => {
251+
(range: ChartAxisRange): ShapeModificationCallback =>
252+
(element: XmlElement, chart: XmlDocument): void => {
246253
const axis = chart.getElementsByTagName('c:valAx')[range.axisIndex || 0];
247254
if (!axis) return;
248255

@@ -265,6 +272,8 @@ export default class ModifyChartHelper {
265272

266273
ModifyChartHelper.setAxisAttribute(scaling, 'c:min', range.min);
267274
ModifyChartHelper.setAxisAttribute(scaling, 'c:max', range.max);
275+
276+
XmlHelper.dump(chart);
268277
};
269278

270279
static setAxisAttribute = (

0 commit comments

Comments
 (0)