Skip to content

Commit a49b763

Browse files
committed
feature: remove unneeded series / categories
1 parent b647c74 commit a49b763

File tree

7 files changed

+100
-13
lines changed

7 files changed

+100
-13
lines changed

src/helper/modify-helper.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const setChartData = (data: ChartData) => (
7272
new ModifyChart(chart, workbook, data, slots).modify();
7373

7474
// XmlHelper.dump(chart)
75+
// XmlHelper.dump(workbook.table)
7576
};
7677

7778
export const setChartVerticalLines = (data: ChartData) => (
@@ -139,6 +140,8 @@ export const setChartBubbles = (data: ChartData) => (
139140
});
140141

141142
new ModifyChart(chart, workbook, data, slots).modify();
143+
144+
// XmlHelper.dump(chart)
142145
};
143146

144147
export const setTableData = (data: TableData) => (

src/helper/modify-xml-helper.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ export default class ModifyXmlHelper {
1616

1717
for (const tag in tags) {
1818
const modifier = tags[tag] as Modification;
19+
20+
if (GeneralHelper.propertyExists(modifier, 'collection')) {
21+
const modifies = GeneralHelper.arrayify(modifier.collection);
22+
const collection = root.getElementsByTagName(tag);
23+
Object.values(modifies).forEach((modifyXml) => modifyXml(collection));
24+
return;
25+
}
26+
1927
const index = modifier.index || 0;
2028

2129
this.assertNode(root.getElementsByTagName(tag), index, tag, modifier);

src/helper/xml-helper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ export class XmlHelper {
317317
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
318318
}
319319

320+
static sliceCollection(
321+
collection: HTMLCollectionOf<Element>,
322+
length: number,
323+
): void {
324+
for (let i = collection.length; i > length; i--) {
325+
const toRemove = collection[i - 1];
326+
toRemove.parentNode.removeChild(toRemove);
327+
}
328+
}
329+
320330
static dump(element: XMLDocument | Element): void {
321331
const s = new XMLSerializer();
322332
const xmlBuffer = s.serializeToString(element);

src/modify/modify-chart.ts

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ export class ModifyChart {
4545
this.width = this.columns.length;
4646
}
4747

48-
modify() {
48+
modify(): void {
4949
this.setValues();
5050
this.setSeries();
51+
this.sliceChartSpace();
52+
5153
this.setWorkbook();
54+
this.sliceWorkbook();
5255
this.setWorkbookTable();
56+
this.sliceWorkbookTable();
5357
}
5458

5559
setColumns(slot: ChartSlot[]): ChartColumn[] {
@@ -62,9 +66,10 @@ export class ModifyChart {
6266

6367
const label = slot.label ? slot.label : series.label;
6468

65-
const mapData = slot.mapData ? slot.mapData : (point: number) => point;
69+
const mapData =
70+
slot.mapData !== undefined ? slot.mapData : (point: number) => point;
6671

67-
const isStrRef = slot.isStrRef ? slot.isStrRef : true;
72+
const isStrRef = slot.isStrRef !== undefined ? slot.isStrRef : true;
6873

6974
const worksheetCb = (
7075
point: number,
@@ -115,23 +120,22 @@ export class ModifyChart {
115120
this.columns
116121
.filter((col) => col.chart)
117122
.forEach((col) => {
118-
if(category.values[col.series] === undefined) {
119-
throw new Error(`No value for category "${category.label}" at series "${(col.label)}".`)
123+
if (category.values[col.series] === undefined) {
124+
throw new Error(
125+
`No value for category "${category.label}" at series "${col.label}".`,
126+
);
120127
}
121128

122-
this.chart.modify(
123-
this.series(
124-
col.series,
125-
col.chart(category.values[col.series], c, category),
126-
),
127-
);
129+
col.modTags = col.chart(category.values[col.series], c, category);
130+
131+
this.chart.modify(this.series(col.series, col.modTags));
128132
});
129133
});
130134
}
131135

132136
setSeries(): void {
133137
this.columns.forEach((column, colId) => {
134-
if (column.isStrRef === undefined || column.isStrRef === true) {
138+
if (column.isStrRef === true) {
135139
this.chart.modify(
136140
this.series(column.series, {
137141
...this.seriesId(column.series),
@@ -142,6 +146,23 @@ export class ModifyChart {
142146
});
143147
}
144148

149+
sliceChartSpace(): void {
150+
this.chart.modify({
151+
'c:plotArea': this.slice('c:ser', this.data.series.length),
152+
});
153+
154+
this.columns
155+
.filter((column) => column.modTags)
156+
.forEach((column) => {
157+
const sliceMod = {};
158+
159+
Object.keys(column.modTags).forEach((tag) => {
160+
sliceMod[tag] = this.slice('c:pt', this.height);
161+
});
162+
this.chart.modify(this.series(column.series, sliceMod));
163+
});
164+
}
165+
145166
setWorkbook(): void {
146167
this.workbook.modify(this.spanString());
147168
this.workbook.modify(this.rowAttributes(0, 1));
@@ -161,6 +182,28 @@ export class ModifyChart {
161182
});
162183
}
163184

185+
sliceWorkbook(): void {
186+
this.data.categories.forEach((category, c) => {
187+
const r = c + 1;
188+
this.workbook.modify({
189+
row: {
190+
index: r,
191+
...this.slice('c', this.width + 1),
192+
},
193+
});
194+
});
195+
196+
this.workbook.modify({
197+
row: {
198+
...this.slice('c', this.width + 1),
199+
},
200+
});
201+
202+
this.workbook.modify({
203+
sheetData: this.slice('row', this.height + 1),
204+
});
205+
}
206+
164207
series = (index: number, children: ModificationTags): ModificationTags => {
165208
return {
166209
'c:ser': {
@@ -311,6 +354,18 @@ export class ModifyChart {
311354
};
312355
}
313356

357+
slice(tag: string, length: number): Modification {
358+
return {
359+
children: {
360+
[tag]: {
361+
collection: (collection: HTMLCollectionOf<Element>) => {
362+
XmlHelper.sliceCollection(collection, length);
363+
},
364+
},
365+
},
366+
};
367+
}
368+
314369
spanString(): ModificationTags {
315370
return {
316371
dimension: {
@@ -365,4 +420,10 @@ export class ModifyChart {
365420
},
366421
});
367422
}
423+
424+
sliceWorkbookTable(): void {
425+
this.workbookTable.modify({
426+
table: this.slice('tableColumn', this.width + 1),
427+
});
428+
}
368429
}

src/shapes/chart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class Chart extends Shape implements IChart {
173173
await this.editTargetWorksheetRel();
174174
}
175175

176-
async getWorksheetFilePrefix(targetRelFile: string) {
176+
async getWorksheetFilePrefix(targetRelFile: string): Promise<string> {
177177
const relationTargets = await XmlHelper.getTargetsFromRelationships(
178178
this.sourceArchive,
179179
targetRelFile,

src/types/chart-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type ChartColumn = {
3434
category: ChartCategory,
3535
) => ModificationTags;
3636
isStrRef?: boolean;
37+
modTags?: ModificationTags;
3738
};
3839
export type ChartData = {
3940
series: ChartSeries[];

src/types/modify-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
export type ModifyCallback = {
22
(element: Element);
33
};
4+
export type ModifyCollectionCallback = {
5+
(collection: HTMLCollectionOf<Element>);
6+
};
47
export type Modification = {
58
index?: number;
9+
collection?: ModifyCollectionCallback | ModifyCollectionCallback;
610
children?: ModificationTags;
711
modify?: ModifyCallback | ModifyCallback[];
812
};

0 commit comments

Comments
 (0)