Skip to content

Commit add20df

Browse files
committed
refactor: merged & simplified chart modifiers
1 parent c075f7a commit add20df

File tree

9 files changed

+456
-405
lines changed

9 files changed

+456
-405
lines changed

src/dev.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ const data = {
1919
categories: [
2020
{ label: 'item test r1', values: [10, 16, 12] },
2121
{ label: 'item test r2', values: [12, 18, 15] },
22-
// { label: 'item test r3', values: [ 14, 12, 11 ] },
23-
// { label: 'item test r4', values: [ 8, 11, 9 ] },
24-
// { label: 'item test r5', values: [ 6, 15, 7 ] },
22+
{ label: 'item test r3', values: [ 14, 12, 11 ] },
23+
{ label: 'item test r4', values: [ 8, 11, 9 ] },
24+
{ label: 'item test r5', values: [ 6, 15, 7 ] },
2525
// { label: 'item test r6', values: [ 16, 16, 9 ] },
2626
],
2727
};

src/helper/modify-helper.ts

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { XmlHelper } from './xml-helper';
2-
import { ChartData, ChartColumn, ChartBubble } from '../types/chart-types';
2+
import { ChartData, ChartColumn, ChartBubble, ChartSlot, ChartCategory } from '../types/chart-types';
33
import { FrameCoordinates, Workbook } from '../types/types';
4-
import { ModifyChartspace } from '../modify/chartspace';
5-
import { ModifyWorkbook } from '../modify/workbook';
4+
import { ModifyChart } from '../modify/chart';
65

76
export const setSolidFill = (element: XMLDocument): void => {
87
element
@@ -52,102 +51,87 @@ export const setChartData = (data: ChartData) => (
5251
chart: Document,
5352
workbook: Workbook,
5453
): void => {
55-
const columns = [] as ChartColumn[];
56-
54+
55+
const slots = [] as ChartSlot[]
5756
data.series.forEach((series, s) => {
58-
columns.push({
59-
series: s,
60-
label: `${series.label}`,
61-
worksheet: (ctx, point: number, r: number) =>
62-
ctx.pattern(ctx.rowValues(r, s + 1, point)),
63-
chart: (ctx, point: number, r: number, category) => {
64-
return {
65-
'c:val': ctx.point(r, s + 1, point),
66-
'c:cat': ctx.point(r, 0, category.label),
67-
};
68-
},
69-
isStrRef: true,
70-
});
57+
slots.push({
58+
index: s,
59+
series: series,
60+
targetCol: s + 1,
61+
type: 'defaultSeries',
62+
})
7163
});
7264

73-
new ModifyChartspace(chart, data, columns).setChart();
74-
new ModifyWorkbook(workbook, data, columns).setWorkbook();
65+
new ModifyChart(chart, workbook, data, slots).modify();
66+
67+
// XmlHelper.dump(chart)
7568
};
7669

7770
export const setChartVerticalLines = (data: ChartData) => (
7871
element: XMLDocument,
7972
chart: Document,
8073
workbook: Workbook,
8174
): void => {
82-
const columns = [] as ChartColumn[];
75+
const slots = [] as ChartSlot[]
8376

84-
columns.push({
77+
slots.push({
8578
label: `Y-Values`,
86-
worksheet: (ctx, point, r: number, category) =>
87-
ctx.pattern(ctx.rowValues(r, 1, category.y)),
79+
mapData: (point: number, category: ChartCategory) => category.y,
80+
targetCol: 1,
8881
});
8982

9083
data.series.forEach((series, s) => {
91-
columns.push({
92-
series: s,
93-
label: `${series.label}`,
94-
worksheet: (ctx, point: number, r: number) =>
95-
ctx.pattern(ctx.rowValues(r, s + 2, point)),
96-
chart: (ctx, point: number, r: number, category) => {
97-
return {
98-
'c:xVal': ctx.point(r, s + 2, point),
99-
'c:yVal': ctx.point(r, 1, category.y),
100-
};
101-
},
102-
isStrRef: true,
103-
});
84+
slots.push({
85+
index: s,
86+
series: series,
87+
targetCol: s + 2,
88+
type: 'xySeries',
89+
})
10490
});
10591

106-
new ModifyChartspace(chart, data, columns).setChart();
107-
new ModifyWorkbook(workbook, data, columns).setWorkbook();
92+
new ModifyChart(chart, workbook, data, slots).modify();
10893
};
10994

11095
export const setChartBubbles = (data: ChartData) => (
11196
element: XMLDocument,
11297
chart: Document,
11398
workbook: Workbook,
11499
): void => {
115-
const columns = [] as ChartColumn[];
100+
const slots = [] as ChartSlot[]
116101

117102
data.series.forEach((series, s) => {
118103
const colId = s * 3;
119-
columns.push({
120-
series: s,
121-
label: `${series.label}`,
122-
worksheet: (ctx, point: ChartBubble, r: number) =>
123-
ctx.pattern(ctx.rowValues(r, colId + 1, point.x)),
124-
chart: (ctx, point: ChartBubble, r: number) => {
125-
return { 'c:xVal': ctx.point(r, colId + 1, point.x) };
126-
},
127-
isStrRef: true,
128-
});
129-
columns.push({
130-
series: s,
104+
slots.push({
105+
index: s,
106+
series: series,
107+
targetCol: colId + 1,
108+
type: 'customSeries',
109+
tag: 'c:xVal',
110+
mapData: (point: ChartBubble): number => point.x,
111+
})
112+
slots.push({
131113
label: `${series.label}-Y-Value`,
132-
worksheet: (ctx, point: ChartBubble, r: number) =>
133-
ctx.pattern(ctx.rowValues(r, colId + 2, point.y)),
134-
chart: (ctx, point: ChartBubble, r: number) => {
135-
return { 'c:yVal': ctx.point(r, colId + 2, point.y) };
136-
},
137-
});
138-
columns.push({
139-
series: s,
114+
index: s,
115+
series: series,
116+
targetCol: colId + 2,
117+
type: 'customSeries',
118+
tag: 'c:yVal',
119+
mapData: (point: ChartBubble): number => point.y,
120+
isStrRef: false
121+
})
122+
slots.push({
140123
label: `${series.label}-Size`,
141-
worksheet: (ctx, point: ChartBubble, r: number) =>
142-
ctx.pattern(ctx.rowValues(r, colId + 3, point.size)),
143-
chart: (ctx, point: ChartBubble, r: number) => {
144-
return { 'c:bubbleSize': ctx.point(r, colId + 3, point.size) };
145-
},
146-
});
124+
index: s,
125+
series: series,
126+
targetCol: colId + 3,
127+
type: 'customSeries',
128+
tag: 'c:bubbleSize',
129+
mapData: (point: ChartBubble): number => point.size,
130+
isStrRef: false
131+
})
147132
});
148133

149-
new ModifyChartspace(chart, data, columns).setChart();
150-
new ModifyWorkbook(workbook, data, columns).setWorkbook();
134+
new ModifyChart(chart, workbook, data, slots).modify();
151135
};
152136

153137
export const dump = (element: XMLDocument | Document): void => {

src/helper/modify-xml-helper.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
ModificationPattern,
3+
ModificationTags,
4+
} from '../types/chart-types';
5+
6+
import StringIdGenerator from './cell-id-helper';
7+
import { GeneralHelper } from './general-helper';
8+
import { XmlHelper } from './xml-helper';
9+
10+
export default class ModifyXmlHelper {
11+
root: XMLDocument;
12+
13+
constructor(root: XMLDocument) {
14+
this.root = root;
15+
}
16+
17+
modify(
18+
tags: ModificationTags,
19+
root?: XMLDocument | Element,
20+
): void {
21+
root = root || this.root;
22+
23+
for (const tag in tags) {
24+
const modifier = tags[tag] as ModificationPattern;
25+
const index = modifier.index || 0;
26+
27+
this.assertNode(root.getElementsByTagName(tag), index, tag, modifier);
28+
const element = root.getElementsByTagName(tag)[index];
29+
30+
if (GeneralHelper.propertyExists(modifier, 'modify')) {
31+
const modifies = GeneralHelper.arrayify(modifier.modify);
32+
Object.values(modifies).forEach((modifyXml) => modifyXml(element));
33+
}
34+
35+
if (GeneralHelper.propertyExists(modifier, 'children')) {
36+
this.modify(modifier.children, element);
37+
}
38+
}
39+
}
40+
41+
static text = (label: string) => (element: Element): void => {
42+
element.firstChild.textContent = String(label);
43+
};
44+
45+
static value = (value: number | string, index?: number) => (
46+
element: Element,
47+
): void => {
48+
element.getElementsByTagName('c:v')[0].firstChild.textContent = String(
49+
value,
50+
);
51+
if (index !== undefined) {
52+
element.setAttribute('idx', String(index));
53+
}
54+
};
55+
56+
static attribute = (attribute: string, value: string | number) => (
57+
element: Element,
58+
): void => {
59+
element.setAttribute(attribute, String(value));
60+
};
61+
62+
static range = (series: number, length?: number) => (element: Element): void => {
63+
const range = element.firstChild.textContent
64+
element.firstChild.textContent = StringIdGenerator.setRange(range, series, length);;
65+
};
66+
67+
assertNode(collection: HTMLCollectionOf<Element>, index: number, tag?: string, info?): void {
68+
if (!collection[index]) {
69+
if(collection[collection.length - 1] === undefined) {
70+
console.log(info)
71+
throw new Error(`Index ${index} not found at "${tag}"`)
72+
}
73+
const tplNode = collection[collection.length - 1];
74+
const newChild = tplNode.cloneNode(true);
75+
XmlHelper.insertAfter(newChild, tplNode);
76+
}
77+
}
78+
}

src/helper/xml-helper.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ export class XmlHelper {
296296
};
297297
}
298298

299+
300+
static appendSharedString(sharedStrings: Document, stringValue: string): number {
301+
const strings = sharedStrings.getElementsByTagName('sst')[0];
302+
const newLabel = sharedStrings.createTextNode(stringValue);
303+
const newText = sharedStrings.createElement('t');
304+
newText.appendChild(newLabel);
305+
306+
const newString = sharedStrings.createElement('si');
307+
newString.appendChild(newText);
308+
309+
strings.appendChild(newString);
310+
311+
return strings.getElementsByTagName('si').length - 1;
312+
}
313+
299314
static insertAfter(newNode: Node, referenceNode: Element): void {
300315
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
301316
}

0 commit comments

Comments
 (0)